-
Notifications
You must be signed in to change notification settings - Fork 440
Standardize table and heading markup elements #1389
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# frozen_string_literal: true | ||
|
||
module RDoc | ||
class Markup | ||
# Base class defining the interface for all markup elements found in documentation | ||
# @abstract | ||
class Element | ||
# @abstract | ||
#: (untyped) -> void | ||
def accept(visitor) | ||
raise NotImplementedError, "#{self.class} must implement the accept method" | ||
end | ||
|
||
# @abstract | ||
#: (PP) -> void | ||
def pretty_print(q) | ||
raise NotImplementedError, "#{self.class} must implement the pretty_print method" | ||
end | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -1,84 +1,100 @@ | ||||||||||||||||||||||||||||||
# frozen_string_literal: true | ||||||||||||||||||||||||||||||
## | ||||||||||||||||||||||||||||||
# A heading with a level (1-6) and text | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
RDoc::Markup::Heading = | ||||||||||||||||||||||||||||||
Struct.new :level, :text do | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
@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 | ||||||||||||||||||||||||||||||
markup.add_regexp_handling RDoc::CrossReference::CROSSREF_REGEXP, :CROSSREF | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
@to_html = RDoc::Markup::ToHtml.new nil | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
def @to_html.handle_regexp_CROSSREF(target) | ||||||||||||||||||||||||||||||
target.text.sub(/^\\/, '') | ||||||||||||||||||||||||||||||
module RDoc | ||||||||||||||||||||||||||||||
class Markup | ||||||||||||||||||||||||||||||
# A heading with a level (1-6) and text | ||||||||||||||||||||||||||||||
# | ||||||||||||||||||||||||||||||
# RDoc syntax: | ||||||||||||||||||||||||||||||
# = Heading 1 | ||||||||||||||||||||||||||||||
# == Heading 2 | ||||||||||||||||||||||||||||||
# === Heading 3 | ||||||||||||||||||||||||||||||
# | ||||||||||||||||||||||||||||||
# Markdown syntax: | ||||||||||||||||||||||||||||||
# # Heading 1 | ||||||||||||||||||||||||||||||
# ## Heading 2 | ||||||||||||||||||||||||||||||
# ### Heading 3 | ||||||||||||||||||||||||||||||
class Heading < Element | ||||||||||||||||||||||||||||||
#: String | ||||||||||||||||||||||||||||||
attr_reader :text | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
#: Integer | ||||||||||||||||||||||||||||||
attr_accessor :level | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
# A singleton RDoc::Markup::ToLabel formatter for headings. | ||||||||||||||||||||||||||||||
#: () -> RDoc::Markup::ToLabel | ||||||||||||||||||||||||||||||
def self.to_label | ||||||||||||||||||||||||||||||
@to_label ||= Markup::ToLabel.new | ||||||||||||||||||||||||||||||
end | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
# A singleton plain HTML formatter for headings. Used for creating labels for the Table of Contents | ||||||||||||||||||||||||||||||
#: () -> RDoc::Markup::ToHtml | ||||||||||||||||||||||||||||||
def self.to_html | ||||||||||||||||||||||||||||||
@to_html ||= begin | ||||||||||||||||||||||||||||||
markup = Markup.new | ||||||||||||||||||||||||||||||
markup.add_regexp_handling CrossReference::CROSSREF_REGEXP, :CROSSREF | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
@to_html = Markup::ToHtml.new nil | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
def @to_html.handle_regexp_CROSSREF(target) | ||||||||||||||||||||||||||||||
target.text.sub(/^\\/, '') | ||||||||||||||||||||||||||||||
end | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
@to_html | ||||||||||||||||||||||||||||||
end | ||||||||||||||||||||||||||||||
end | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
#: (Integer, String) -> void | ||||||||||||||||||||||||||||||
def initialize(level, text) | ||||||||||||||||||||||||||||||
@level = level | ||||||||||||||||||||||||||||||
@text = text | ||||||||||||||||||||||||||||||
super() | ||||||||||||||||||||||||||||||
Comment on lines
+48
to
+50
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Initializing the base object (
Suggested change
Or we may want to remove |
||||||||||||||||||||||||||||||
end | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
#: (Object) -> bool | ||||||||||||||||||||||||||||||
def ==(other) | ||||||||||||||||||||||||||||||
other.is_a?(Heading) && other.level == @level && other.text == @text | ||||||||||||||||||||||||||||||
end | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
# @override | ||||||||||||||||||||||||||||||
#: (untyped) -> void | ||||||||||||||||||||||||||||||
def accept(visitor) | ||||||||||||||||||||||||||||||
visitor.accept_heading(self) | ||||||||||||||||||||||||||||||
end | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
# An HTML-safe anchor reference for this header. | ||||||||||||||||||||||||||||||
#: () -> String | ||||||||||||||||||||||||||||||
def aref | ||||||||||||||||||||||||||||||
"label-#{self.class.to_label.convert text.dup}" | ||||||||||||||||||||||||||||||
end | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
# Creates a fully-qualified label which will include the label from +context+. This helps keep ids unique in HTML. | ||||||||||||||||||||||||||||||
#: (RDoc::Context?) -> String | ||||||||||||||||||||||||||||||
def label(context = nil) | ||||||||||||||||||||||||||||||
label = +"" | ||||||||||||||||||||||||||||||
label << "#{context.aref}-" if context&.respond_to?(:aref) | ||||||||||||||||||||||||||||||
label << aref | ||||||||||||||||||||||||||||||
label | ||||||||||||||||||||||||||||||
end | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
# HTML markup of the text of this label without the surrounding header element. | ||||||||||||||||||||||||||||||
#: () -> String | ||||||||||||||||||||||||||||||
def plain_html | ||||||||||||||||||||||||||||||
text = self.text.dup | ||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It seems that we don't need |
||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
if matched = text.match(/rdoc-image:[^:]+:(.*)/) | ||||||||||||||||||||||||||||||
text = matched[1] | ||||||||||||||||||||||||||||||
end | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
self.class.to_html.to_html(text) | ||||||||||||||||||||||||||||||
Comment on lines
+82
to
+88
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How about using more descriptive local variable name and remove
Suggested change
|
||||||||||||||||||||||||||||||
end | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
# @override | ||||||||||||||||||||||||||||||
#: (PP) -> void | ||||||||||||||||||||||||||||||
def pretty_print(q) | ||||||||||||||||||||||||||||||
q.group 2, "[head: #{level} ", ']' do | ||||||||||||||||||||||||||||||
q.pp text | ||||||||||||||||||||||||||||||
end | ||||||||||||||||||||||||||||||
end | ||||||||||||||||||||||||||||||
end | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
@to_html | ||||||||||||||||||||||||||||||
end | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
## | ||||||||||||||||||||||||||||||
# Calls #accept_heading on +visitor+ | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
def accept(visitor) | ||||||||||||||||||||||||||||||
visitor.accept_heading self | ||||||||||||||||||||||||||||||
end | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
## | ||||||||||||||||||||||||||||||
# An HTML-safe anchor reference for this header. | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
def aref | ||||||||||||||||||||||||||||||
"label-#{self.class.to_label.convert text.dup}" | ||||||||||||||||||||||||||||||
end | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
## | ||||||||||||||||||||||||||||||
# 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 | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
## | ||||||||||||||||||||||||||||||
# HTML markup of the text of this label without the surrounding header | ||||||||||||||||||||||||||||||
# element. | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
def plain_html | ||||||||||||||||||||||||||||||
text = self.text.dup | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
if matched = text.match(/rdoc-image:[^:]+:(.*)/) | ||||||||||||||||||||||||||||||
text = matched[1] | ||||||||||||||||||||||||||||||
end | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
self.class.to_html.to_html(text) | ||||||||||||||||||||||||||||||
end | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
def pretty_print(q) # :nodoc: | ||||||||||||||||||||||||||||||
q.group 2, "[head: #{level} ", ']' do | ||||||||||||||||||||||||||||||
q.pp text | ||||||||||||||||||||||||||||||
end | ||||||||||||||||||||||||||||||
end | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it should be a local variable here
@to_html
→to_html
It will be assigned to
@to_html
at@to_html ||= begin