1##
2# A heading with a level (1-6) and text
3
4RDoc::Markup::Heading =
5  Struct.new :level, :text do
6
7  @to_html = nil
8  @to_label = nil
9
10  ##
11  # A singleton RDoc::Markup::ToLabel formatter for headings.
12
13  def self.to_label
14    @to_label ||= RDoc::Markup::ToLabel.new
15  end
16
17  ##
18  # A singleton plain HTML formatter for headings.  Used for creating labels
19  # for the Table of Contents
20
21  def self.to_html
22    return @to_html if @to_html
23
24    markup = RDoc::Markup.new
25    markup.add_special RDoc::CrossReference::CROSSREF_REGEXP, :CROSSREF
26
27    @to_html = RDoc::Markup::ToHtml.new nil
28
29    def @to_html.handle_special_CROSSREF special
30      special.text.sub(/^\\/, '')
31    end
32
33    @to_html
34  end
35
36  ##
37  # Calls #accept_heading on +visitor+
38
39  def accept visitor
40    visitor.accept_heading self
41  end
42
43  ##
44  # An HTML-safe anchor reference for this header.
45
46  def aref
47    "label-#{self.class.to_label.convert text.dup}"
48  end
49
50  ##
51  # HTML markup of the text of this label without the surrounding header
52  # element.
53
54  def plain_html
55    self.class.to_html.to_html(text.dup)
56  end
57
58  def pretty_print q # :nodoc:
59    q.group 2, "[head: #{level} ", ']' do
60      q.pp text
61    end
62  end
63
64end
65
66