|
1 | 1 | # frozen_string_literal: true |
2 | | -## |
3 | | -# A heading with a level (1-6) and text |
4 | 2 |
|
5 | | -RDoc::Markup::Heading = |
6 | | - Struct.new :level, :text do |
7 | | - |
8 | | - @to_html = nil |
9 | | - @to_label = nil |
10 | | - |
11 | | - ## |
12 | | - # A singleton RDoc::Markup::ToLabel formatter for headings. |
13 | | - |
14 | | - def self.to_label |
15 | | - @to_label ||= RDoc::Markup::ToLabel.new |
16 | | - end |
17 | | - |
18 | | - ## |
19 | | - # A singleton plain HTML formatter for headings. Used for creating labels |
20 | | - # for the Table of Contents |
21 | | - |
22 | | - def self.to_html |
23 | | - return @to_html if @to_html |
24 | | - |
25 | | - markup = RDoc::Markup.new |
26 | | - markup.add_regexp_handling RDoc::CrossReference::CROSSREF_REGEXP, :CROSSREF |
27 | | - |
28 | | - @to_html = RDoc::Markup::ToHtml.new nil |
29 | | - |
30 | | - def @to_html.handle_regexp_CROSSREF(target) |
31 | | - target.text.sub(/^\\/, '') |
| 3 | +module RDoc |
| 4 | + class Markup |
| 5 | + # A heading with a level (1-6) and text |
| 6 | + # |
| 7 | + # RDoc syntax: |
| 8 | + # = Heading 1 |
| 9 | + # == Heading 2 |
| 10 | + # === Heading 3 |
| 11 | + # |
| 12 | + # Markdown syntax: |
| 13 | + # # Heading 1 |
| 14 | + # ## Heading 2 |
| 15 | + # ### Heading 3 |
| 16 | + class Heading < Element |
| 17 | + #: String |
| 18 | + attr_reader :text |
| 19 | + |
| 20 | + #: Integer |
| 21 | + attr_accessor :level |
| 22 | + |
| 23 | + # A singleton RDoc::Markup::ToLabel formatter for headings. |
| 24 | + #: () -> RDoc::Markup::ToLabel |
| 25 | + def self.to_label |
| 26 | + @to_label ||= Markup::ToLabel.new |
| 27 | + end |
| 28 | + |
| 29 | + # A singleton plain HTML formatter for headings. Used for creating labels for the Table of Contents |
| 30 | + #: () -> RDoc::Markup::ToHtml |
| 31 | + def self.to_html |
| 32 | + @to_html ||= begin |
| 33 | + markup = Markup.new |
| 34 | + markup.add_regexp_handling CrossReference::CROSSREF_REGEXP, :CROSSREF |
| 35 | + |
| 36 | + @to_html = Markup::ToHtml.new nil |
| 37 | + |
| 38 | + def @to_html.handle_regexp_CROSSREF(target) |
| 39 | + target.text.sub(/^\\/, '') |
| 40 | + end |
| 41 | + |
| 42 | + @to_html |
| 43 | + end |
| 44 | + end |
| 45 | + |
| 46 | + #: (Integer, String) -> void |
| 47 | + def initialize(level, text) |
| 48 | + @level = level |
| 49 | + @text = text |
| 50 | + super() |
| 51 | + end |
| 52 | + |
| 53 | + #: (Object) -> bool |
| 54 | + def ==(other) |
| 55 | + other.is_a?(Heading) && other.level == @level && other.text == @text |
| 56 | + end |
| 57 | + |
| 58 | + # @override |
| 59 | + #: (untyped) -> void |
| 60 | + def accept(visitor) |
| 61 | + visitor.accept_heading(self) |
| 62 | + end |
| 63 | + |
| 64 | + # An HTML-safe anchor reference for this header. |
| 65 | + #: () -> String |
| 66 | + def aref |
| 67 | + "label-#{self.class.to_label.convert text.dup}" |
| 68 | + end |
| 69 | + |
| 70 | + # Creates a fully-qualified label which will include the label from +context+. This helps keep ids unique in HTML. |
| 71 | + #: (RDoc::Context?) -> String |
| 72 | + def label(context = nil) |
| 73 | + label = +"" |
| 74 | + label << "#{context.aref}-" if context&.respond_to?(:aref) |
| 75 | + label << aref |
| 76 | + label |
| 77 | + end |
| 78 | + |
| 79 | + # HTML markup of the text of this label without the surrounding header element. |
| 80 | + #: () -> String |
| 81 | + def plain_html |
| 82 | + text = self.text.dup |
| 83 | + |
| 84 | + if matched = text.match(/rdoc-image:[^:]+:(.*)/) |
| 85 | + text = matched[1] |
| 86 | + end |
| 87 | + |
| 88 | + self.class.to_html.to_html(text) |
| 89 | + end |
| 90 | + |
| 91 | + # @override |
| 92 | + #: (PP) -> void |
| 93 | + def pretty_print(q) |
| 94 | + q.group 2, "[head: #{level} ", ']' do |
| 95 | + q.pp text |
| 96 | + end |
| 97 | + end |
32 | 98 | end |
33 | | - |
34 | | - @to_html |
35 | | - end |
36 | | - |
37 | | - ## |
38 | | - # Calls #accept_heading on +visitor+ |
39 | | - |
40 | | - def accept(visitor) |
41 | | - visitor.accept_heading self |
42 | | - end |
43 | | - |
44 | | - ## |
45 | | - # An HTML-safe anchor reference for this header. |
46 | | - |
47 | | - def aref |
48 | | - "label-#{self.class.to_label.convert text.dup}" |
49 | 99 | end |
50 | | - |
51 | | - ## |
52 | | - # Creates a fully-qualified label which will include the label from |
53 | | - # +context+. This helps keep ids unique in HTML. |
54 | | - |
55 | | - def label(context = nil) |
56 | | - label = aref |
57 | | - |
58 | | - label = [context.aref, label].compact.join '-' if |
59 | | - context and context.respond_to? :aref |
60 | | - |
61 | | - label |
62 | | - end |
63 | | - |
64 | | - ## |
65 | | - # HTML markup of the text of this label without the surrounding header |
66 | | - # element. |
67 | | - |
68 | | - def plain_html |
69 | | - text = self.text.dup |
70 | | - |
71 | | - if matched = text.match(/rdoc-image:[^:]+:(.*)/) |
72 | | - text = matched[1] |
73 | | - end |
74 | | - |
75 | | - self.class.to_html.to_html(text) |
76 | | - end |
77 | | - |
78 | | - def pretty_print(q) # :nodoc: |
79 | | - q.group 2, "[head: #{level} ", ']' do |
80 | | - q.pp text |
81 | | - end |
82 | | - end |
83 | | - |
84 | 100 | end |
0 commit comments