Class: RDoc::Markup::ToHtmlCrossref
| Relationships & Source Files | |
| Super Chains via Extension / Inclusion / Inheritance | |
|
Class Chain:
|
|
|
Instance Chain:
|
|
| Inherits: |
RDoc::Markup::ToHtml
|
| Defined in: | lib/rdoc/markup/to_html_crossref.rb |
Overview
Subclass of the ToHtml class that supports looking up method
names, classes, etc to create links. ::RDoc::CrossReference is used to
generate those links based on the current context.
Constant Summary
-
ALL_CROSSREF_REGEXP =
Internal use only
# File 'lib/rdoc/markup/to_html_crossref.rb', line 10RDoc::CrossReference::ALL_CROSSREF_REGEXP
-
CROSSREF_REGEXP =
Internal use only
# File 'lib/rdoc/markup/to_html_crossref.rb', line 11RDoc::CrossReference::CROSSREF_REGEXP
::RDoc::Text - Included
MARKUP_FORMAT, SPACE_SEPARATED_LETTER_CLASS
ToHtml - Inherited
HTML_CHARACTER_ALIASES, LIST_TYPE_TO_HTML, TO_HTML_CHARACTERS, URL_CHARACTERS_REGEXP_STR
Class Method Summary
-
.new(from_path, context, pipe: false, output_decoration: true, hyperlink_all: false, show_hash: false, autolink_excluded_words: [], warn_missing_rdoc_ref: true) ⇒ ToHtmlCrossref
constructor
Creates a new crossref resolver that generates links relative to #context which lives at
from_pathin the generated files.
ToHtml - Inherited
| .encode_fallback | Transcodes |
| .new | Creates a new formatter that will output HTML. |
Formatter - Inherited
| .gen_relative_url | Converts a target url to one that is relative to a given path. |
| .new | Creates a new |
Instance Attribute Summary
-
#context
rw
::RDoc::CodeObjectfor generating references. -
#show_hash
rw
Should we show '#' characters on method references?
ToHtml - Inherited
| #code_object | The |
| #from_path | Path to this document for relative links. |
| #in_tidylink_label? | Returns true if we are processing inside a tidy link label. |
| #in_list_entry, #list, #res | |
::RDoc::Text - Included
| #language | The language for this text. |
Instance Method Summary
-
#apply_tidylink_label_special_handling(label, url)
Applies additional special handling on top of the one defined in
ToHtml. -
#cross_reference(name, text = nil, code = true, rdoc_ref: false)
Creates a link to the reference
nameif the name exists. -
#gen_url(url, text)
Generates links for scheme URLs and allows
ToHtmlto handle other schemes. -
#handle_regexp_CROSSREF(name)
We're invoked when any text matches the CROSSREF pattern.
-
#handle_regexp_HYPERLINK(url)
Handles scheme links and allows
ToHtmlto handle other schemes. -
#handle_regexp_RDOCLINK(url)
targetis an rdoc-schemed link that will be converted into a hyperlink. - #handle_TT(code)
-
#link(name, html_string, code = true, rdoc_ref: false)
Creates an HTML link to
namewith the givenhtml_string. -
#tt_cross_reference(code)
Handles cross-reference and suppressed-crossref inside tt tag.
- #init_link_notation_regexp_handlings Internal use only
ToHtml - Inherited
| #accept_blank_line | Adds |
| #accept_block_quote | Adds |
| #accept_heading | Adds |
| #accept_list_end | Finishes consumption of |
| #accept_list_item_end | Finishes consumption of |
| #accept_list_item_start | Prepares the visitor for consuming |
| #accept_list_start | Prepares the visitor for consuming |
| #accept_paragraph | Adds |
| #accept_raw | Adds |
| #accept_rule | Adds |
| #accept_table | Adds |
| #accept_verbatim | Adds |
| #apply_tidylink_label_special_handling | Special handling for tidy link labels. |
| #convert_string | CGI-escapes |
| #deduplicate_heading_id | Returns a unique heading ID, appending -1, -2, etc. |
| #emit_inline, | |
| #end_accepting | Returns the generated output. |
| #gen_url | Generates an HTML link or image tag for the given |
| #handle_BOLD, #handle_BOLD_WORD, #handle_EM, #handle_EM_WORD, #handle_HARD_BREAK, #handle_PLAIN_TEXT, #handle_REGEXP_HANDLING_TEXT, | |
| #handle_regexp_HTML_CHARACTERS | Converts (c), (r), --, --- , ..., ...., ``, '' to HTML characters. |
| #handle_regexp_HYPERLINK |
|
| #handle_regexp_QUOTE_AFTER_WORD, #handle_regexp_QUOTE_NOT_AFTER_WORD, | |
| #handle_regexp_RDOCLINK |
|
| #handle_regexp_SUPPRESSED_CROSSREF | Converts suppressed cross-reference |
| #handle_STRIKE, #handle_TIDYLINK, #handle_TT, | |
| #html_list_name | Determines the HTML list element for |
| #init_link_notation_regexp_handlings | Adds regexp handlings about link notations. |
| #init_regexp_handlings | Adds regexp handlings. |
| #list_end_for | Returns the HTML end-tag for |
| #list_item_start | Returns the HTML tag for |
| #parsable_text_to_html | Generate syntax highlighted html for ruby-like text. |
| #parseable? | Returns true if text is valid ruby syntax. |
| #start_accepting | Prepares the visitor for HTML generation. |
| #to_html | Converts |
| #handle_inline, #handle_RDOCLINK | |
::RDoc::Text - Included
| #flush_left | Flush |
| #markup | Convert a string in markup format into HTML. |
| #normalize_comment | Strips hashes, expands tabs then flushes |
| #parse | Normalizes |
| #snippet | The first |
| #strip_hashes | Strips leading # characters from |
| #strip_newlines | Strips leading and trailing \n characters from |
| #strip_stars | Strips /* */ style comments. |
| #wrap | Wraps |
Formatter - Inherited
| #accept_document | Adds |
| #add_regexp_handling_RDOCLINK | Adds a regexp handling for links of the form rdoc-...: |
| #annotate | Allows |
| #apply_regexp_handling | Applies regexp handling to |
| #convert | Marks up |
| #convert_string | Converts a string to be fancier if desired. |
| #handle_BOLD | Called when processing bold nodes while traversing inline nodes from handle_inline. |
| #handle_BOLD_WORD | Called when processing bold word nodes while traversing inline nodes from handle_inline. |
| #handle_EM | Called when processing emphasis nodes while traversing inline nodes from handle_inline. |
| #handle_EM_WORD | Called when processing emphasis word nodes while traversing inline nodes from handle_inline. |
| #handle_HARD_BREAK | Called when processing a hard break while traversing inline nodes from handle_inline. |
| #handle_inline | Parses inline |
| #handle_PLAIN_TEXT | Called when processing plain text while traversing inline nodes from handle_inline. |
| #handle_REGEXP_HANDLING_TEXT | Called when processing regexp-handling-processed text while traversing inline nodes from handle_inline. |
| #handle_STRIKE | Called when processing strike nodes while traversing inline nodes from handle_inline. |
| #handle_TEXT | Called when processing text node while traversing inline nodes from handle_inline. |
| #handle_TIDYLINK | Called when processing tidylink nodes while traversing inline nodes from handle_inline. |
| #handle_TT | Called when processing tt nodes while traversing inline nodes from handle_inline. |
| #ignore | Use ignore in your subclass to ignore the content of a node. |
| #parse_url | Extracts and a scheme, url and an anchor id from |
| #traverse_inline_nodes | Traverses |
| #tt? | Is |
Constructor Details
.new(from_path, context, pipe: false, output_decoration: true, hyperlink_all: false, show_hash: false, autolink_excluded_words: [], warn_missing_rdoc_ref: true) ⇒ ToHtmlCrossref
Creates a new crossref resolver that generates links relative to #context
which lives at from_path in the generated files. '#' characters on
references are removed unless #show_hash is true. Only method names
preceded by '#' or '::' are linked, unless hyperlink_all is true.
# File 'lib/rdoc/markup/to_html_crossref.rb', line 30
def initialize(from_path, context, pipe: false, output_decoration: true, hyperlink_all: false, show_hash: false, autolink_excluded_words: [], warn_missing_rdoc_ref: true) raise ArgumentError, 'from_path cannot be nil' if from_path.nil? super(pipe: pipe, output_decoration: output_decoration) @context = context @from_path = from_path @hyperlink_all = hyperlink_all @show_hash = show_hash @autolink_excluded_words = autolink_excluded_words @warn_missing_rdoc_ref = warn_missing_rdoc_ref @cross_reference = RDoc::CrossReference.new @context end
Instance Attribute Details
#context (rw)
::RDoc::CodeObject for generating references
# File 'lib/rdoc/markup/to_html_crossref.rb', line 17
attr_accessor :context
#show_hash (rw)
Should we show '#' characters on method references?
# File 'lib/rdoc/markup/to_html_crossref.rb', line 22
attr_accessor :show_hash
Instance Method Details
#apply_tidylink_label_special_handling(label, url)
# File 'lib/rdoc/markup/to_html_crossref.rb', line 230
def apply_tidylink_label_special_handling(label, url) if url == "rdoc-ref:#{label}" && cross_reference(label)&.include?('<code>') "<code>#{convert_string(label)}</code>" else super end end
#cross_reference(name, text = nil, code = true, rdoc_ref: false)
Creates a link to the reference name if the name exists. If text is
given it is used as the link text, otherwise name is used.
Returns nil if the link target could not be resolved.
# File 'lib/rdoc/markup/to_html_crossref.rb', line 62
def cross_reference(name, text = nil, code = true, rdoc_ref: false) # Strip '#' for link display text (e.g. #method shows as "method" in links) display = !@show_hash && name.start_with?('#') ? name[1..] : name if !display.end_with?('+@', '-@') && match = display.match(/(.*[^#:])?@(.*)/) context_name = match[1] label = convert_string(RDoc::Text.decode_legacy_label(match[2])) text ||= "#{label} at <code>#{convert_string(context_name)}</code>" if context_name text ||= label code = false else text ||= convert_string(display) end link(name, text, code, rdoc_ref: rdoc_ref) end
#gen_url(url, text)
Generates links for scheme URLs and allows
ToHtml to handle other schemes.
# File 'lib/rdoc/markup/to_html_crossref.rb', line 143
def gen_url(url, text) if url =~ /\Ardoc-ref:/ name = $' cross_reference(name, text, name == text, rdoc_ref: true) || text else super end end
#handle_regexp_CROSSREF(name)
We're invoked when any text matches the CROSSREF pattern. If we find the
corresponding reference, generate a link. If the name we're looking for
contains no punctuation, we look for it up the module/class chain. For
example, ToHtml is found, even without the RDoc::Markup:: prefix,
because we look for it in module ::RDoc::Markup first.
# File 'lib/rdoc/markup/to_html_crossref.rb', line 86
def handle_regexp_CROSSREF(name) return convert_string(name) if in_tidylink_label? return name if @autolink_excluded_words&.include?(name) return name if name =~ /@[\w-]+\.[\w-]/ # labels that look like emails unless @hyperlink_all then # This ensures that words entirely consisting of lowercase letters will # not have cross-references generated (to suppress lots of erroneous # cross-references to "new" in text, for instance) return name if name =~ /\A[a-z]*\z/ end cross_reference(name, rdoc_ref: false) || convert_string(name) end
#handle_regexp_HYPERLINK(url)
Handles scheme links and allows ToHtml to
handle other schemes.
# File 'lib/rdoc/markup/to_html_crossref.rb', line 105
def handle_regexp_HYPERLINK(url) return convert_string(url) if in_tidylink_label? case url when /\Ardoc-ref:/ ref = $' cross_reference(ref, rdoc_ref: true) || convert_string(ref) else super end end
#handle_regexp_RDOCLINK(url)
target is an rdoc-schemed link that will be converted into a hyperlink.
For the rdoc-ref scheme the cross-reference will be looked up and the
given name will be used.
All other contents are handled by the superclass
# File 'lib/rdoc/markup/to_html_crossref.rb', line 125
def handle_regexp_RDOCLINK(url) case url when /\Ardoc-ref:/ if in_tidylink_label? convert_string(url) else ref = $' cross_reference(ref, rdoc_ref: true) || convert_string(ref) end else super end end
#handle_TT(code)
[ GitHub ]# File 'lib/rdoc/markup/to_html_crossref.rb', line 223
def handle_TT(code) emit_inline(tt_cross_reference(code) || "<code>#{convert_string(code)}</code>") end
#init_link_notation_regexp_handlings
# File 'lib/rdoc/markup/to_html_crossref.rb', line 48
def init_link_notation_regexp_handlings add_regexp_handling_RDOCLINK # The crossref must be linked before tidylink because Klass.method[:sym] # will be processed as a tidylink first and will be broken. crossref_re = @hyperlink_all ? ALL_CROSSREF_REGEXP : CROSSREF_REGEXP @markup.add_regexp_handling crossref_re, :CROSSREF end
#link(name, html_string, code = true, rdoc_ref: false)
Creates an HTML link to name with the given html_string.
html_string should be already escaped and may contain HTML tags.
Returns the link HTML string, or nil if the reference could not be resolved.
# File 'lib/rdoc/markup/to_html_crossref.rb', line 157
def link(name, html_string, code = true, rdoc_ref: false) if !(name.end_with?('+@', '-@')) and name =~ /(.*[^#:])?@/ name = $1 label = $' end ref = @cross_reference.resolve name if name # Non-text source files (C, Ruby, etc.) don't get HTML pages generated, # so don't auto-link to them. Explicit rdoc-ref: links are still allowed. if !rdoc_ref && RDoc::TopLevel === ref && !ref.text? return end if ref path = ref.as_href(@from_path) if code and RDoc::CodeObject === ref and !(RDoc::TopLevel === ref) html_string = "<code>#{html_string}</code>" end elsif name if rdoc_ref && @warn_missing_rdoc_ref puts "#{@from_path}: `rdoc-ref:#{name}` can't be resolved for `#{html_string}`" end return else # A bare label reference like @foo still produces a valid anchor link return unless label path = +"" end if label # Decode legacy labels (e.g., "What-27s+Here" -> "What's Here") # then convert to GitHub-style anchor format decoded_label = RDoc::Text.decode_legacy_label(label) formatted_label = RDoc::Text.to_anchor(decoded_label) # Case 1: Path already has an anchor (e.g., method link) # Input: C1#method@label -> path="C1.html#method-i-m" # Output: C1.html#method-i-m-label if path =~ /#/ path << "-#{formatted_label}" # Case 2: Label matches a section title # Input: C1@Section -> path="C1.html", section "Section" exists # Output: C1.html#section (uses section.aref for GitHub-style) elsif (section = ref&.sections&.find { |s| decoded_label == s.title }) path << "##{section.aref}" # Case 3: Ref has an aref (class/module context) # Input: C1@heading -> path="C1.html", ref=C1 class # Output: C1.html#class-c1-heading elsif ref.respond_to?(:aref) path << "##{ref.aref}-#{formatted_label}" # Case 4: No context, just the label (e.g., TopLevel/file) # Input: README@section -> path="README_md.html" # Output: README_md.html#section else path << "##{formatted_label}" end end "<a href=\"#{path}\">#{html_string}</a>" end
#tt_cross_reference(code)
Handles cross-reference and suppressed-crossref inside tt tag. Returns nil if code is not an existing cross-reference nor a suppressed-crossref.
# File 'lib/rdoc/markup/to_html_crossref.rb', line 240
def tt_cross_reference(code) return if in_tidylink_label? crossref_regexp = @hyperlink_all ? ALL_CROSSREF_REGEXP : CROSSREF_REGEXP # REGEXP sometimes matches a string that starts with a backslash but is not a # suppressed cross-reference (for example, `\+`), so the backslash-removed # part needs to be checked against crossref_regexp. match = crossref_regexp.match(code.delete_prefix('\\')) return unless match && match.begin(1).zero? return unless match.post_match.match?(/\A[[:punct:]\s]*\z/) # cross_reference(file_page) may return a link without code tag. # We need to check it because this method shouldn't return an html text without code tag. if code.start_with?('\\') # Remove leading backslash if crossref exists "<code>#{convert_string(code[1..])}</code>" if cross_reference(code[1..])&.include?('<code>') else html = cross_reference(code) html if html&.include?('<code>') end end