2008-02-09 22:59:08 -05:00
|
|
|
require 'rdoc/markup/to_html'
|
2011-07-30 20:19:00 -04:00
|
|
|
require 'rdoc/cross_reference'
|
2008-02-09 22:59:08 -05:00
|
|
|
|
|
|
|
##
|
2011-07-30 20:19:00 -04:00
|
|
|
# Subclass of the RDoc::Markup::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.
|
2008-02-09 22:59:08 -05:00
|
|
|
|
2008-02-12 17:23:00 -05:00
|
|
|
class RDoc::Markup::ToHtmlCrossref < RDoc::Markup::ToHtml
|
2008-02-09 22:59:08 -05:00
|
|
|
|
2011-07-30 20:19:00 -04:00
|
|
|
# :stopdoc:
|
|
|
|
ALL_CROSSREF_REGEXP = RDoc::CrossReference::ALL_CROSSREF_REGEXP
|
|
|
|
CLASS_REGEXP_STR = RDoc::CrossReference::CLASS_REGEXP_STR
|
|
|
|
CROSSREF_REGEXP = RDoc::CrossReference::CROSSREF_REGEXP
|
|
|
|
METHOD_REGEXP_STR = RDoc::CrossReference::METHOD_REGEXP_STR
|
|
|
|
# :startdoc:
|
2008-09-24 22:43:03 -04:00
|
|
|
|
2008-02-09 22:59:08 -05:00
|
|
|
##
|
2010-04-01 03:45:16 -04:00
|
|
|
# RDoc::CodeObject for generating references
|
|
|
|
|
|
|
|
attr_accessor :context
|
|
|
|
|
2010-12-19 22:22:49 -05:00
|
|
|
##
|
|
|
|
# Should we show '#' characters on method references?
|
|
|
|
|
|
|
|
attr_accessor :show_hash
|
|
|
|
|
2010-04-01 03:45:16 -04:00
|
|
|
##
|
|
|
|
# Creates a new crossref resolver that generates links relative to +context+
|
|
|
|
# which lives at +from_path+ in the generated files. '#' characters on
|
2010-12-19 22:22:49 -05:00
|
|
|
# references are removed unless +show_hash+ is true. Only method names
|
2011-07-30 20:19:00 -04:00
|
|
|
# preceded by '#' or '::' are linked, unless +hyperlink_all+ is true.
|
2008-02-09 22:59:08 -05:00
|
|
|
|
2011-06-16 00:59:24 -04:00
|
|
|
def initialize(from_path, context, show_hash, hyperlink_all = false,
|
|
|
|
markup = nil)
|
2008-07-17 20:46:16 -04:00
|
|
|
raise ArgumentError, 'from_path cannot be nil' if from_path.nil?
|
2011-06-16 00:59:24 -04:00
|
|
|
super markup
|
2008-02-09 22:59:08 -05:00
|
|
|
|
2010-12-19 22:22:49 -05:00
|
|
|
crossref_re = hyperlink_all ? ALL_CROSSREF_REGEXP : CROSSREF_REGEXP
|
|
|
|
|
2011-07-30 20:19:00 -04:00
|
|
|
@cross_reference = RDoc::CrossReference.new context
|
|
|
|
|
2010-12-19 22:22:49 -05:00
|
|
|
@markup.add_special crossref_re, :CROSSREF
|
2011-07-30 20:19:00 -04:00
|
|
|
@markup.add_special(/rdoc-ref:\S+\w/, :HYPERLINK)
|
2008-02-09 22:59:08 -05:00
|
|
|
|
2011-07-30 20:19:00 -04:00
|
|
|
@from_path = from_path
|
2010-12-19 22:22:49 -05:00
|
|
|
@hyperlink_all = hyperlink_all
|
2011-07-30 20:19:00 -04:00
|
|
|
@show_hash = show_hash
|
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
# 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.
|
|
|
|
|
|
|
|
def cross_reference name, text = nil
|
|
|
|
lookup = name
|
2008-02-09 22:59:08 -05:00
|
|
|
|
2011-07-30 20:19:00 -04:00
|
|
|
name = name[1..-1] unless @show_hash if name[0, 1] == '#'
|
|
|
|
|
|
|
|
text = name unless text
|
|
|
|
|
|
|
|
link lookup, text
|
2008-02-09 22:59:08 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
##
|
2010-04-19 01:08:28 -04:00
|
|
|
# We're invoked when any text matches the CROSSREF pattern. If we find the
|
2011-07-30 20:19:00 -04:00
|
|
|
# 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 <tt>RDoc::Markup::</tt> prefix,
|
|
|
|
# because we look for it in module Markup first.
|
2008-02-09 22:59:08 -05:00
|
|
|
|
|
|
|
def handle_special_CROSSREF(special)
|
|
|
|
name = special.text
|
|
|
|
|
2010-12-19 22:22:49 -05:00
|
|
|
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
|
2008-07-17 20:46:16 -04:00
|
|
|
|
2011-07-30 20:19:00 -04:00
|
|
|
cross_reference name
|
|
|
|
end
|
2008-02-09 22:59:08 -05:00
|
|
|
|
2011-07-30 20:19:00 -04:00
|
|
|
##
|
|
|
|
# Handles <tt>rdoc-ref:</tt> scheme links and allows RDoc::Markup::ToHtml to
|
|
|
|
# handle other schemes.
|
2010-04-19 01:08:28 -04:00
|
|
|
|
2011-07-30 20:19:00 -04:00
|
|
|
def handle_special_HYPERLINK special
|
|
|
|
return cross_reference $' if special.text =~ /\Ardoc-ref:/
|
2008-02-09 22:59:08 -05:00
|
|
|
|
2011-07-30 20:19:00 -04:00
|
|
|
super
|
|
|
|
end
|
2010-12-19 22:22:49 -05:00
|
|
|
|
2011-07-30 20:19:00 -04:00
|
|
|
##
|
|
|
|
# Generates links for <tt>rdoc-ref:</tt> scheme URLs and allows
|
|
|
|
# RDoc::Markup::ToHtml to handle other schemes.
|
2010-12-19 22:22:49 -05:00
|
|
|
|
2011-07-30 20:19:00 -04:00
|
|
|
def gen_url url, text
|
2011-08-01 01:25:13 -04:00
|
|
|
return super unless url =~ /\Ardoc-ref:/
|
2008-02-09 22:59:08 -05:00
|
|
|
|
2011-07-30 20:19:00 -04:00
|
|
|
cross_reference $', text
|
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
# Creates an HTML link to +name+ with the given +text+.
|
|
|
|
|
|
|
|
def link name, text
|
|
|
|
ref = @cross_reference.resolve name, text
|
|
|
|
|
|
|
|
case ref
|
|
|
|
when String then
|
|
|
|
ref
|
|
|
|
else
|
|
|
|
"<a href=\"#{ref.as_href @from_path}\">#{text}</a>"
|
|
|
|
end
|
2008-02-09 22:59:08 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
2010-04-01 03:45:16 -04:00
|
|
|
|