2017-11-27 05:45:24 -05:00
|
|
|
# frozen_string_literal: true
|
2010-04-01 03:45:16 -04:00
|
|
|
##
|
|
|
|
# A heading with a level (1-6) and text
|
|
|
|
|
2012-12-05 17:20:15 -05:00
|
|
|
RDoc::Markup::Heading =
|
|
|
|
Struct.new :level, :text do
|
2010-04-01 03:45:16 -04:00
|
|
|
|
2012-11-26 23:28:14 -05:00
|
|
|
@to_html = nil
|
|
|
|
@to_label = nil
|
|
|
|
|
|
|
|
##
|
|
|
|
# A singleton RDoc::Markup::ToLabel formatter for headings.
|
|
|
|
|
|
|
|
def self.to_label
|
|
|
|
@to_label ||= RDoc::Markup::ToLabel.new
|
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
# A singleton plain HTML formatter for headings. Used for creating labels
|
|
|
|
# for the Table of Contents
|
|
|
|
|
|
|
|
def self.to_html
|
|
|
|
return @to_html if @to_html
|
|
|
|
|
|
|
|
markup = RDoc::Markup.new
|
2018-10-17 02:28:20 -04:00
|
|
|
markup.add_regexp_handling RDoc::CrossReference::CROSSREF_REGEXP, :CROSSREF
|
2012-11-26 23:28:14 -05:00
|
|
|
|
|
|
|
@to_html = RDoc::Markup::ToHtml.new nil
|
|
|
|
|
2018-10-17 02:28:20 -04:00
|
|
|
def @to_html.handle_regexp_CROSSREF target
|
|
|
|
target.text.sub(/^\\/, '')
|
2012-11-26 23:28:14 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
@to_html
|
|
|
|
end
|
|
|
|
|
2010-12-19 22:22:49 -05:00
|
|
|
##
|
2011-02-01 19:32:30 -05:00
|
|
|
# Calls #accept_heading on +visitor+
|
2010-12-19 22:22:49 -05:00
|
|
|
|
2010-04-01 03:45:16 -04:00
|
|
|
def accept visitor
|
|
|
|
visitor.accept_heading self
|
|
|
|
end
|
|
|
|
|
2012-11-26 23:28:14 -05:00
|
|
|
##
|
|
|
|
# An HTML-safe anchor reference for this header.
|
|
|
|
|
|
|
|
def aref
|
|
|
|
"label-#{self.class.to_label.convert text.dup}"
|
|
|
|
end
|
|
|
|
|
2013-09-18 19:33:36 -04:00
|
|
|
##
|
|
|
|
# Creates a fully-qualified label which will include the label from
|
|
|
|
# +context+. This helps keep ids unique in HTML.
|
|
|
|
|
|
|
|
def label context = nil
|
|
|
|
label = aref
|
|
|
|
|
|
|
|
label = [context.aref, label].compact.join '-' if
|
|
|
|
context and context.respond_to? :aref
|
|
|
|
|
|
|
|
label
|
|
|
|
end
|
|
|
|
|
2012-11-26 23:28:14 -05:00
|
|
|
##
|
|
|
|
# HTML markup of the text of this label without the surrounding header
|
|
|
|
# element.
|
|
|
|
|
|
|
|
def plain_html
|
|
|
|
self.class.to_html.to_html(text.dup)
|
|
|
|
end
|
|
|
|
|
2010-04-01 03:45:16 -04:00
|
|
|
def pretty_print q # :nodoc:
|
|
|
|
q.group 2, "[head: #{level} ", ']' do
|
|
|
|
q.pp text
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|