Skip to content

Commit 457071e

Browse files
committed
Enable cross reference in code
Some people like to mark up method names in MarkDown style block quotes, like this: ruby/ruby#12333. Currently, no links are created in the code in the RDoc, but such words most likely refer to methods. This PR makes a word a code cross-reference if the whole word can be resolved as a reference.
1 parent 7cd125e commit 457071e

File tree

3 files changed

+62
-12
lines changed

3 files changed

+62
-12
lines changed

lib/rdoc/markup/formatter.rb

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -195,32 +195,39 @@ def in_tt?
195195
@in_tt > 0
196196
end
197197

198+
def tt_tag? attr_mask, reverse = false
199+
each_attr_tag(attr_mask, reverse) do |tag|
200+
return true if tt? tag
201+
end
202+
false
203+
end
204+
198205
##
199206
# Turns on tags for +item+ on +res+
200207

201208
def on_tags res, item
202-
attr_mask = item.turn_on
203-
return if attr_mask.zero?
204-
205-
@attr_tags.each do |tag|
206-
if attr_mask & tag.bit != 0 then
207-
res << annotate(tag.on)
208-
@in_tt += 1 if tt? tag
209-
end
209+
each_attr_tag(item.turn_on) do |tag|
210+
res << annotate(tag.on)
211+
@in_tt += 1 if tt? tag
210212
end
211213
end
212214

213215
##
214216
# Turns off tags for +item+ on +res+
215217

216218
def off_tags res, item
217-
attr_mask = item.turn_off
219+
each_attr_tag(item.turn_off, true) do |tag|
220+
@in_tt -= 1 if tt? tag
221+
res << annotate(tag.off)
222+
end
223+
end
224+
225+
def each_attr_tag attr_mask, reverse = false
218226
return if attr_mask.zero?
219227

220-
@attr_tags.reverse_each do |tag|
228+
@attr_tags.public_send(reverse ? :reverse_each : :each) do |tag|
221229
if attr_mask & tag.bit != 0 then
222-
@in_tt -= 1 if tt? tag
223-
res << annotate(tag.off)
230+
yield tag
224231
end
225232
end
226233
end

lib/rdoc/markup/to_html_crossref.rb

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,4 +172,43 @@ def link name, text, code = true
172172
end
173173
end
174174

175+
def convert_flow(flow)
176+
res = []
177+
178+
i = 0
179+
while i < flow.size
180+
item = flow[i]
181+
i += 1
182+
case item
183+
when RDoc::Markup::AttrChanger then
184+
# Make "+Class#method+" a cross reference
185+
if respond_to?(:cross_reference) and
186+
tt_tag?(item.turn_on) and
187+
String === (str = flow[i]) and
188+
RDoc::Markup::AttrChanger === flow[i+1] and
189+
tt_tag?(flow[i+1].turn_off, true) and
190+
(text = cross_reference str) != str
191+
then
192+
text = yield text, res if defined?(yield)
193+
res << text
194+
i += 2
195+
next
196+
end
197+
off_tags res, item
198+
on_tags res, item
199+
when String then
200+
text = convert_string(item)
201+
text = yield text, res if defined?(yield)
202+
res << text
203+
when RDoc::Markup::RegexpHandling then
204+
text = convert_regexp_handling(item)
205+
text = yield text, res if defined?(yield)
206+
res << text
207+
else
208+
raise "Unknown flow element: #{item.inspect}"
209+
end
210+
end
211+
212+
res.join('')
213+
end
175214
end

test/rdoc/test_rdoc_markup_to_html_crossref.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ def test_convert_CROSSREF
1515
result = @to.convert 'C1'
1616

1717
assert_equal para("<a href=\"C1.html\"><code>C1</code></a>"), result
18+
19+
result = @to.convert '+C1+'
20+
21+
assert_equal para("<a href=\"C1.html\"><code>C1</code></a>"), result
1822
end
1923

2024
def test_convert_CROSSREF_method

0 commit comments

Comments
 (0)