Skip to content

Commit d4319c0

Browse files
committed
Standardize Markup::Heading
1 parent 260ac37 commit d4319c0

File tree

1 file changed

+96
-79
lines changed

1 file changed

+96
-79
lines changed

lib/rdoc/markup/heading.rb

Lines changed: 96 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1,84 +1,101 @@
11
# frozen_string_literal: true
2-
##
3-
# A heading with a level (1-6) and text
42

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+
super()
49+
50+
@level = level
51+
@text = text
52+
end
53+
54+
#: (Object) -> bool
55+
def ==(other)
56+
other.is_a?(Heading) && other.level == @level && other.text == @text
57+
end
58+
59+
# @override
60+
#: (untyped) -> void
61+
def accept(visitor)
62+
visitor.accept_heading(self)
63+
end
64+
65+
# An HTML-safe anchor reference for this header.
66+
#: () -> String
67+
def aref
68+
"label-#{self.class.to_label.convert text.dup}"
69+
end
70+
71+
# Creates a fully-qualified label which will include the label from +context+. This helps keep ids unique in HTML.
72+
#: (RDoc::Context?) -> String
73+
def label(context = nil)
74+
label = +""
75+
label << "#{context.aref}-" if context&.respond_to?(:aref)
76+
label << aref
77+
label
78+
end
79+
80+
# HTML markup of the text of this label without the surrounding header element.
81+
#: () -> String
82+
def plain_html
83+
no_image_text = text
84+
85+
if matched = no_image_text.match(/rdoc-image:[^:]+:(.*)/)
86+
no_image_text = matched[1]
87+
end
88+
89+
self.class.to_html.to_html(no_image_text)
90+
end
91+
92+
# @override
93+
#: (PP) -> void
94+
def pretty_print(q)
95+
q.group 2, "[head: #{level} ", ']' do
96+
q.pp text
97+
end
98+
end
3299
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}"
49100
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-
84101
end

0 commit comments

Comments
 (0)