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
-
CLASS_REGEXP_STR =
Internal use only
# File 'lib/rdoc/markup/to_html_crossref.rb', line 11RDoc::CrossReference::CLASS_REGEXP_STR
-
CROSSREF_REGEXP =
Internal use only
# File 'lib/rdoc/markup/to_html_crossref.rb', line 12RDoc::CrossReference::CROSSREF_REGEXP
-
METHOD_REGEXP_STR =
Internal use only
# File 'lib/rdoc/markup/to_html_crossref.rb', line 13RDoc::CrossReference::METHOD_REGEXP_STR
::RDoc::Text - Included
MARKUP_FORMAT, SPACE_SEPARATED_LETTER_CLASS, TO_HTML_CHARACTERS
ToHtml - Inherited
LIST_TYPE_TO_HTML, TIDY_LINK_SINGLE_WORD, TIDY_LINK_WITH_BRACES, TIDY_LINK_WITH_BRACES_TAIL, URL_CHARACTERS_REGEXP_STR
Class Method Summary
-
.new(options, from_path, context, markup = nil) ⇒ ToHtmlCrossref
constructor
Creates a new crossref resolver that generates links relative to #context which lives at
from_pathin the generated files.
ToHtml - Inherited
| .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. |
| #tidy_link_capturing?, #in_list_entry, #list, #res | |
::RDoc::Text - Included
| #language | The language for this text. |
Formatter - Inherited
| #in_tt? | Are we currently inside tt tags? |
Instance Method Summary
- #convert_flow(flow_items, &block)
-
#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
rdoc-ref:scheme URLs and allowsToHtmlto handle other schemes. -
#handle_regexp_CROSSREF(target)
We’re invoked when any text matches the CROSSREF pattern.
-
#handle_regexp_HYPERLINK(target)
Handles
rdoc-ref:scheme links and allowsToHtmlto handle other schemes. -
#handle_regexp_RDOCLINK(target)
targetis an rdoc-schemed link that will be converted into a hyperlink. -
#link(name, text, code = true, rdoc_ref: false)
Creates an HTML link to
namewith the giventext. -
#convert_tt_crossref(flow_items, index)
private
Detects
...spans that contain a single cross-reference candidate. - #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 |
| #convert_string | CGI-escapes |
| #end_accepting | Returns the generated output. |
| #gen_url | Generate a link to |
| #handle_regexp_HARD_BREAK |
|
| #handle_regexp_HYPERLINK |
|
| #handle_regexp_RDOCLINK |
|
| #handle_regexp_TIDYLINK | This |
| #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. |
| #init_tags | Maps attributes to HTML tags. |
| #list_end_for | Returns the HTML end-tag for |
| #list_item_start | Returns the HTML tag for |
| #parseable? | Returns true if text is valid ruby syntax. |
| #start_accepting | Prepares the visitor for HTML generation. |
| #to_html | Converts |
| #append_flow_fragment, #append_to_tidy_label, #convert_complete_tidy_link, #convert_flow, #emit_tidy_link_fragment, #extract_tidy_link_parts, #finish_tidy_link, #off_tags, #on_tags, #render_tidy_link_label, #start_tidy_link, #handle_RDOCLINK | |
::RDoc::Text - Included
| #expand_tabs | Expands tab characters in |
| #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. |
| #to_html | Converts ampersand, dashes, ellipsis, quotes, copyright and registered trademark symbols in |
| #wrap | Wraps |
Formatter - Inherited
| #accept_document | Adds |
| #add_regexp_handling_RDOCLINK | Adds a regexp handling for links of the form rdoc-…: |
| #add_regexp_handling_TIDYLINK | Adds a regexp handling for links of the form |
| #add_tag | Add a new set of tags for an attribute. |
| #annotate | Allows |
| #convert | Marks up |
| #convert_flow | Converts flow items |
| #convert_regexp_handling | Converts added regexp handlings. |
| #convert_string | Converts a string to be fancier if desired. |
| #each_attr_tag, | |
| #ignore | Use ignore in your subclass to ignore the content of a node. |
| #off_tags | Turns off tags for |
| #on_tags | Turns on tags for |
| #parse_url | Extracts and a scheme, url and an anchor id from |
| #tt? | Is |
| #tt_tag? | |
Constructor Details
.new(options, from_path, context, markup = nil) ⇒ 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 32
def initialize(, from_path, context, markup = nil) raise ArgumentError, 'from_path cannot be nil' if from_path.nil? super , markup @context = context @from_path = from_path @hyperlink_all = @options.hyperlink_all @show_hash = @options.show_hash @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 19
attr_accessor :context
#show_hash (rw)
Should we show ‘#’ characters on method references?
# File 'lib/rdoc/markup/to_html_crossref.rb', line 24
attr_accessor :show_hash
Instance Method Details
#convert_flow(flow_items, &block)
[ GitHub ]# File 'lib/rdoc/markup/to_html_crossref.rb', line 187
def convert_flow(flow_items, &block) res = [] i = 0 while i < flow_items.size item = flow_items[i] case item when RDoc::Markup::AttrChanger if !tidy_link_capturing? && (text = convert_tt_crossref(flow_items, i)) text = block.call(text, res) if block append_flow_fragment res, text i += 3 next end res, item res, item i += 1 when String text = convert_string(item) text = block.call(text, res) if block append_flow_fragment res, text i += 1 when RDoc::Markup::RegexpHandling text = convert_regexp_handling(item) text = block.call(text, res) if block append_flow_fragment res, text i += 1 else raise "Unknown flow element: #{item.inspect}" end end res.join('') end
#convert_tt_crossref(flow_items, index) (private)
Detects ... spans that contain a single cross-reference candidate. When the candidate occupies the whole span (aside from trailing punctuation), the tt markup is replaced by the resolved cross-reference.
# File 'lib/rdoc/markup/to_html_crossref.rb', line 231
def convert_tt_crossref(flow_items, index) opener = flow_items[index] return unless tt_tag?(opener.turn_on) string = flow_items[index + 1] closer = flow_items[index + 2] return unless String === string return unless RDoc::Markup::AttrChanger === closer return unless tt_tag?(closer.turn_off, true) crossref_regexp = @options.hyperlink_all ? ALL_CROSSREF_REGEXP : CROSSREF_REGEXP match = crossref_regexp.match(string) return unless match return unless match.begin(1).zero? trailing = match.post_match # Only convert when the remainder is punctuation/whitespace so other tt text stays literal. return unless trailing.match?(/\A[[:punct:]\s]*\z/) text = cross_reference(string) return if text == string text 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.
# File 'lib/rdoc/markup/to_html_crossref.rb', line 61
def cross_reference(name, text = nil, code = true, rdoc_ref: false) lookup = name name = name[1..-1] unless @show_hash if name[0, 1] == '#' if !(name.end_with?('+@', '-@')) and name =~ /(.*[^#:])?@/ text ||= [CGI.unescape($'), (" at <code>#{$1}</code>" if $~.begin(1))].join("") code = false else text ||= name end link lookup, text, code, rdoc_ref: rdoc_ref end
#gen_url(url, text)
Generates links for rdoc-ref: scheme URLs and allows ToHtml to handle other schemes.
# File 'lib/rdoc/markup/to_html_crossref.rb', line 138
def gen_url(url, text) if url =~ /\Ardoc-ref:/ name = $' cross_reference name, text, name == text, rdoc_ref: true else super end end
#handle_regexp_CROSSREF(target)
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 83
def handle_regexp_CROSSREF(target) name = target.text return name if @options.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 end
#handle_regexp_HYPERLINK(target)
Handles rdoc-ref: scheme links and allows ToHtml to handle other schemes.
# File 'lib/rdoc/markup/to_html_crossref.rb', line 104
def handle_regexp_HYPERLINK(target) url = target.text case url when /\Ardoc-ref:/ cross_reference $', rdoc_ref: true else super end end
#handle_regexp_RDOCLINK(target)
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 123
def handle_regexp_RDOCLINK(target) url = target.text case url when /\Ardoc-ref:/ cross_reference $', rdoc_ref: true else super end end
#init_link_notation_regexp_handlings
# File 'lib/rdoc/markup/to_html_crossref.rb', line 46
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 = @options.hyperlink_all ? ALL_CROSSREF_REGEXP : CROSSREF_REGEXP @markup.add_regexp_handling crossref_re, :CROSSREF add_regexp_handling_TIDYLINK end
#link(name, text, code = true, rdoc_ref: false)
Creates an HTML link to name with the given text.
# File 'lib/rdoc/markup/to_html_crossref.rb', line 150
def link(name, text, code = true, rdoc_ref: false) if !(name.end_with?('+@', '-@')) and name =~ /(.*[^#:])?@/ name = $1 label = $' end ref = @cross_reference.resolve name, text if name case ref when String then if rdoc_ref && @options.warn_missing_rdoc_ref puts "#{@from_path}: `rdoc-ref:#{name}` can't be resolved for `#{text}`" end ref else path = ref ? ref.as_href(@from_path) : +"" if code and RDoc::CodeObject === ref and !(RDoc::TopLevel === ref) text = "<code>#{CGI.escapeHTML text}</code>" end if label if path =~ /#/ path << "-label-#{label}" elsif ref&.sections&.any? { |section| label == section.title } path << "##{label}" elsif ref.respond_to?(:aref) path << "##{ref.aref}-label-#{label}" else path << "#label-#{label}" end end "<a href=\"#{path}\">#{text}</a>" end end