Skip to content

Commit cf2cdde

Browse files
committed
Add the rdoc: scheme to allow forward-compatible class and file links. Issue #53
1 parent cf861cf commit cf2cdde

File tree

5 files changed

+100
-16
lines changed

5 files changed

+100
-16
lines changed

History.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
* Minor enhancements
44
* RDoc::Parser::C now supports :doc: and :nodoc: for class comments
5+
* Added the <tt>rdoc:</tt> link scheme which links to a named reference.
6+
This can be used to create cross-generator named links unlike the
7+
<tt>link:</tt> scheme which is dependent upon the exact file name. Issue
8+
#53 by Simon Chiang
59
* Bug fixes
610
* `ri []` and other special methods now work properly. Issue #52 by
711
ddebernardy.

lib/rdoc/markup/to_html.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ def initialize markup = nil
9797

9898
def handle_special_HYPERLINK(special)
9999
url = special.text
100+
100101
gen_url url, url
101102
end
102103

lib/rdoc/markup/to_html_crossref.rb

Lines changed: 45 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ def initialize(from_path, context, show_hash, hyperlink_all = false,
112112
crossref_re = hyperlink_all ? ALL_CROSSREF_REGEXP : CROSSREF_REGEXP
113113

114114
@markup.add_special crossref_re, :CROSSREF
115+
@markup.add_special /rdoc:\S+\w/, :HYPERLINK
115116

116117
@from_path = from_path
117118
@context = context
@@ -122,28 +123,18 @@ def initialize(from_path, context, show_hash, hyperlink_all = false,
122123
end
123124

124125
##
125-
# We're invoked when any text matches the CROSSREF pattern. If we find the
126-
# corresponding reference, generate a hyperlink. If the name we're looking
127-
# for contains no punctuation, we look for it up the module/class chain.
128-
# For example, ToHtml is found, even without the <tt>RDoc::Markup::</tt>
129-
# prefix, because we look for it in module Markup first.
130-
131-
def handle_special_CROSSREF(special)
132-
name = special.text
133-
134-
unless @hyperlink_all then
135-
# This ensures that words entirely consisting of lowercase letters will
136-
# not have cross-references generated (to suppress lots of erroneous
137-
# cross-references to "new" in text, for instance)
138-
return name if name =~ /\A[a-z]*\z/
139-
end
126+
# Creates a link to the reference +name+ if the name exists. If +text+ is
127+
# given it is used as the link text, otherwise +name+ is used.
140128

129+
def cross_reference name, text = nil
141130
return @seen[name] if @seen.include? name
142131

143132
lookup = name
144133

145134
name = name[1..-1] unless @show_hash if name[0, 1] == '#'
146135

136+
text = name unless text
137+
147138
# Find class, module, or method in class or module.
148139
#
149140
# Do not, however, use an if/elsif/else chain to do so. Instead, test
@@ -187,7 +178,7 @@ def handle_special_CROSSREF(special)
187178
ref ? $' : lookup
188179
elsif ref then
189180
if ref.document_self then
190-
"<a href=\"#{ref.as_href @from_path}\">#{name}</a>"
181+
"<a href=\"#{ref.as_href @from_path}\">#{text}</a>"
191182
else
192183
name
193184
end
@@ -200,5 +191,43 @@ def handle_special_CROSSREF(special)
200191
out
201192
end
202193

194+
##
195+
# We're invoked when any text matches the CROSSREF pattern. If we find the
196+
# corresponding reference, generate a hyperlink. If the name we're looking
197+
# for contains no punctuation, we look for it up the module/class chain.
198+
# For example, ToHtml is found, even without the <tt>RDoc::Markup::</tt>
199+
# prefix, because we look for it in module Markup first.
200+
201+
def handle_special_CROSSREF(special)
202+
name = special.text
203+
204+
unless @hyperlink_all then
205+
# This ensures that words entirely consisting of lowercase letters will
206+
# not have cross-references generated (to suppress lots of erroneous
207+
# cross-references to "new" in text, for instance)
208+
return name if name =~ /\A[a-z]*\z/
209+
end
210+
211+
cross_reference name
212+
end
213+
214+
##
215+
# Handles <tt>rdoc:</tt> scheme hyperlinks
216+
217+
def handle_special_HYPERLINK special
218+
return cross_reference $' if special.text =~ /\Ardoc:/
219+
220+
super
221+
end
222+
223+
##
224+
# Generates links for <tt>rdoc:</tt> scheme URLs
225+
226+
def gen_url url, text
227+
super unless url =~ /\Ardoc:/
228+
229+
cross_reference $', text
230+
end
231+
203232
end
204233

test/test_rdoc_markup_to_html.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,14 @@ def test_gen_url
306306
@to.gen_url('link:example', 'example')
307307
end
308308

309+
def test_handle_special_HYPERLINK_link
310+
special = RDoc::Markup::Special.new 0, 'link:README.txt'
311+
312+
link = @to.handle_special_HYPERLINK special
313+
314+
assert_equal '<a href="README.txt">README.txt</a>', link
315+
end
316+
309317
def test_list_verbatim_2
310318
str = "* one\n verb1\n verb2\n* two\n"
311319

test/test_rdoc_markup_to_html_crossref.rb

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,5 +165,47 @@ def test_handle_special_CROSSREF_special
165165
@xref.convert('C2::C3;method(*)')
166166
end
167167

168+
def test_handle_special_HYPERLINK_rdoc
169+
RDoc::TopLevel.new 'README.txt'
170+
@to = RDoc::Markup::ToHtmlCrossref.new 'C2.html', @c2, true
171+
172+
link = @to.handle_special_HYPERLINK hyper 'C2::C3'
173+
174+
assert_equal '<a href="C2/C3.html">C2::C3</a>', link
175+
176+
link = @to.handle_special_HYPERLINK hyper 'C4'
177+
178+
assert_equal '<a href="C4.html">C4</a>', link
179+
180+
link = @to.handle_special_HYPERLINK hyper 'README.txt'
181+
182+
assert_equal '<a href="README_txt.html">README.txt</a>', link
183+
end
184+
185+
def test_handle_special_TIDYLINK_rdoc
186+
RDoc::TopLevel.new 'README.txt'
187+
@to = RDoc::Markup::ToHtmlCrossref.new 'C2.html', @c2, true
188+
189+
link = @to.handle_special_TIDYLINK tidy 'C2::C3'
190+
191+
assert_equal '<a href="C2/C3.html">tidy</a>', link
192+
193+
link = @to.handle_special_TIDYLINK tidy 'C4'
194+
195+
assert_equal '<a href="C4.html">tidy</a>', link
196+
197+
link = @to.handle_special_TIDYLINK tidy 'README.txt'
198+
199+
assert_equal '<a href="README_txt.html">tidy</a>', link
200+
end
201+
202+
def hyper reference
203+
RDoc::Markup::Special.new 0, "rdoc:#{reference}"
204+
end
205+
206+
def tidy reference
207+
RDoc::Markup::Special.new 0, "{tidy}[rdoc:#{reference}]"
208+
end
209+
168210
end
169211

0 commit comments

Comments
 (0)