Class | Generators::HyperlinkHtml |
In: |
lib/rdoc/generators/html_generator.rb
|
Parent: | SM::ToHtml |
Subclass of the SM::ToHtml class that supports looking up words in the AllReferences list. Those that are found (like AllReferences in this comment) will be hyperlinked
We need to record the html path of our caller so we can generate correct relative paths for any hyperlinks that we find
# File lib/rdoc/generators/html_generator.rb, line 92 92: def initialize(from_path, context) 93: super() 94: @from_path = from_path 95: 96: @parent_name = context.parent_name 97: @parent_name += "::" if @parent_name 98: @context = context 99: end
Generate a hyperlink for url, labeled with text. Handle the special cases for img: and link: described under handle_special_HYPEDLINK
# File lib/rdoc/generators/html_generator.rb, line 140 140: def gen_url(url, text) 141: if url =~ /([A-Za-z]+):(.*)/ 142: type = $1 143: path = $2 144: else 145: type = "http" 146: path = url 147: url = "http://#{url}" 148: end 149: 150: if type == "link" 151: if path[0,1] == '#' # is this meaningful? 152: url = path 153: else 154: url = HTMLGenerator.gen_url(@from_path, path) 155: end 156: end 157: 158: if (type == "http" || type == "link") && 159: url =~ /\.(gif|png|jpg|jpeg|bmp)$/ 160: 161: "<img src=\"#{url}\" />" 162: else 163: "<a href=\"#{url}\">#{text.sub(%r{^#{type}:/*}, '')}</a>" 164: end 165: end
We‘re invoked when any text matches the CROSSREF pattern (defined in MarkUp). If we fine the corresponding reference, generate a hyperlink. If the name we‘re looking for contains no punctuation, we look for it up the module/class chain. For example, HyperlinkHtml is found, even without the Generators:: prefix, because we look for it in module Generators first.
# File lib/rdoc/generators/html_generator.rb, line 108 108: def handle_special_CROSSREF(special) 109: name = special.text 110: if name[0,1] == '#' 111: lookup = name[1..-1] 112: name = lookup unless Options.instance.show_hash 113: else 114: lookup = name 115: end 116: 117: # Find class, module, or method in class or module. 118: if /([A-Z]\w*)[.\#](\w+[!?=]?)/ =~ lookup 119: container = $1 120: method = $2 121: ref = @context.find_symbol(container, method) 122: elsif /([A-Za-z]\w*)[.\#](\w+(\([\.\w+\*\/\+\-\=\<\>]+\))?)/ =~ lookup 123: container = $1 124: method = $2 125: ref = @context.find_symbol(container, method) 126: else 127: ref = @context.find_symbol(lookup) 128: end 129: 130: if ref and ref.document_self 131: "<a href=\"#{ref.as_href(@from_path)}\">#{name}</a>" 132: else 133: name 134: end 135: end
And we‘re invoked with a potential external hyperlink mailto: just gets inserted. http: links are checked to see if they reference an image. If so, that image gets inserted using an <img> tag. Otherwise a conventional <a href> is used. We also support a special type of hyperlink, link:, which is a reference to a local file whose path is relative to the —op directory.
# File lib/rdoc/generators/html_generator.rb, line 174 174: def handle_special_HYPERLINK(special) 175: url = special.text 176: gen_url(url, url) 177: end
HEre‘s a hypedlink where the label is different to the URL
<label>[url]
# File lib/rdoc/generators/html_generator.rb, line 183 183: def handle_special_TIDYLINK(special) 184: text = special.text 185: # unless text =~ /(\S+)\[(.*?)\]/ 186: unless text =~ /\{(.*?)\}\[(.*?)\]/ or text =~ /(\S+)\[(.*?)\]/ 187: return text 188: end 189: label = $1 190: url = $2 191: gen_url(url, label) 192: end