Skip to content

Commit e2c02eb

Browse files
committed
RDoc::CrossReference should not create HTML links
1 parent 23b6048 commit e2c02eb

File tree

5 files changed

+39
-38
lines changed

5 files changed

+39
-38
lines changed

History.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
the <tt>link:</tt> scheme which is dependent upon the exact file name.
99
Issue #53 by Simon Chiang
1010
* Pulled RDoc::CrossReference out of RDoc::Markup::ToHtmlCrossref.
11-
Cross-references can now be created without cut-and-paste.
11+
Cross-references can now be created easily for non-HTML formatters.
1212
* Bug fixes
1313
* `ri []` and other special methods now work properly. Issue #52 by
1414
ddebernardy.

lib/rdoc/cross_reference.rb

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -88,36 +88,21 @@ class RDoc::CrossReference
8888
attr_accessor :seen
8989

9090
##
91-
# Sets up the cross-reference for the given +context+ (RDoc::Context).
92-
# +from_path+ is used to create HTML links from #link.
91+
# Allows cross-references to be created based on the given +context+
92+
# (RDoc::Context).
9393

94-
def initialize context, from_path = nil
94+
def initialize context
9595
@context = context
96-
@from_path = from_path
9796

9897
@seen = {}
9998
end
10099

101-
##
102-
# Creates an HTML link to +name+ with the given +text+. +from_path+ must be
103-
# set.
104-
105-
def link name, text
106-
ref = resolve name, text
107-
108-
case ref
109-
when String then
110-
ref
111-
else
112-
"<a href=\"#{ref.as_href @from_path}\">#{text}</a>"
113-
end
114-
end
115-
116100
##
117101
# Returns a reference to +name+.
118102
#
119103
# If the reference is found and +name+ is not documented +text+ will be
120-
# returned. If +name+ is escaped +name+ is returned.
104+
# returned. If +name+ is escaped +name+ is returned. If +name+ is not
105+
# found +text+ is returned.
121106

122107
def resolve name, text
123108
return @seen[name] if @seen.include? name

lib/rdoc/markup/to_html_crossref.rb

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22
require 'rdoc/cross_reference'
33

44
##
5-
# Subclass of the RDoc::Markup::ToHtml class that supports looking up words
6-
# from a context. Those that are found will be linked.
5+
# Subclass of the RDoc::Markup::ToHtml class that supports looking up method
6+
# names, classes, etc to create links. RDoc::CrossReference is used to
7+
# generate those links based on the current context.
78

89
class RDoc::Markup::ToHtmlCrossref < RDoc::Markup::ToHtml
910

@@ -37,13 +38,14 @@ def initialize(from_path, context, show_hash, hyperlink_all = false,
3738

3839
crossref_re = hyperlink_all ? ALL_CROSSREF_REGEXP : CROSSREF_REGEXP
3940

40-
@cross_reference = RDoc::CrossReference.new context, from_path
41+
@cross_reference = RDoc::CrossReference.new context
4142

4243
@markup.add_special crossref_re, :CROSSREF
4344
@markup.add_special /rdoc:\S+\w/, :HYPERLINK
4445

45-
@show_hash = show_hash
46+
@from_path = from_path
4647
@hyperlink_all = hyperlink_all
48+
@show_hash = show_hash
4749
end
4850

4951
##
@@ -57,7 +59,7 @@ def cross_reference name, text = nil
5759

5860
text = name unless text
5961

60-
@cross_reference.link lookup, text
62+
link lookup, text
6163
end
6264

6365
##
@@ -100,5 +102,19 @@ def gen_url url, text
100102
cross_reference $', text
101103
end
102104

105+
##
106+
# Creates an HTML link to +name+ with the given +text+.
107+
108+
def link name, text
109+
ref = @cross_reference.resolve name, text
110+
111+
case ref
112+
when String then
113+
ref
114+
else
115+
"<a href=\"#{ref.as_href @from_path}\">#{text}</a>"
116+
end
117+
end
118+
103119
end
104120

test/test_rdoc_cross_reference.rb

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ class TestRDocCrossReference < XrefTestCase
77
def setup
88
super
99

10-
@xref = RDoc::CrossReference.new @c1, 'index.html'
10+
@xref = RDoc::CrossReference.new @c1
1111
end
1212

1313
def assert_ref expected, name
@@ -18,14 +18,8 @@ def refute_ref name
1818
assert_equal name, @xref.resolve(name, name)
1919
end
2020

21-
def test_link
22-
assert_equal 'n', @xref.link('n', 'n')
23-
24-
assert_equal '<a href="C1.html#method-c-m">m</a>', @xref.link('m', 'm')
25-
end
26-
2721
def test_resolve_C2
28-
@xref = RDoc::CrossReference.new @c2, 'classes/C2.html'
22+
@xref = RDoc::CrossReference.new @c2
2923

3024
refute_ref '#m'
3125

@@ -40,7 +34,7 @@ def test_resolve_C2
4034
end
4135

4236
def test_resolve_C2_C3
43-
@xref = RDoc::CrossReference.new @c2_c3, 'classes/C2/C3.html'
37+
@xref = RDoc::CrossReference.new @c2_c3
4438

4539
assert_ref @c2_c3_m, '#m'
4640

@@ -56,7 +50,7 @@ def test_resolve_C2_C3
5650
end
5751

5852
def test_resolve_C3
59-
@xref = RDoc::CrossReference.new @c3, 'classes/C3.html'
53+
@xref = RDoc::CrossReference.new @c3
6054

6155
assert_ref @c3, 'C3'
6256

@@ -72,14 +66,14 @@ def test_resolve_C3
7266
end
7367

7468
def test_resolve_C4
75-
@xref = RDoc::CrossReference.new @c4, 'classes/C4.html'
69+
@xref = RDoc::CrossReference.new @c4
7670

7771
# C4 ref inside a C4 containing a C4 should resolve to the contained class
7872
assert_ref @c4_c4, 'C4'
7973
end
8074

8175
def test_resolve_C4_C4
82-
@xref = RDoc::CrossReference.new @c4_c4, 'classes/C4/C4.html'
76+
@xref = RDoc::CrossReference.new @c4_c4
8377

8478
# A C4 reference inside a C4 class contained within a C4 class should
8579
# resolve to the inner C4 class.

test/test_rdoc_markup_to_html_crossref.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,12 @@ def test_handle_special_TIDYLINK_rdoc
5858
assert_equal '<a href="README_txt.html">tidy</a>', link
5959
end
6060

61+
def test_link
62+
assert_equal 'n', @to.link('n', 'n')
63+
64+
assert_equal '<a href="C1.html#method-c-m">m</a>', @to.link('m', 'm')
65+
end
66+
6167
def SPECIAL text
6268
@to.handle_special_CROSSREF special text
6369
end

0 commit comments

Comments
 (0)