Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions lib/rdoc/markup/to_html.rb
Original file line number Diff line number Diff line change
Expand Up @@ -526,12 +526,16 @@ def accept_blank_line(blank_line)
def accept_heading(heading)
level = [6, heading.level].min

label = deduplicate_heading_id(heading.label(@code_object))
legacy_label = deduplicate_heading_id(heading.legacy_label(@code_object))
raw_label = heading.label(@code_object)
raw_legacy_label = heading.legacy_label(@code_object)
label = deduplicate_heading_id(raw_label)
# No legacy anchor is needed when the heading's own id already uses
# the legacy format (see RDoc::Text.to_anchor).
legacy_label = deduplicate_heading_id(raw_legacy_label) unless raw_legacy_label == raw_label

# Add legacy anchor before the heading for backward compatibility.
# This allows old links with label- prefix to still work.
if @output_decoration && !@pipe
if @output_decoration && !@pipe && legacy_label
@res << "\n<span id=\"#{legacy_label}\" class=\"legacy-anchor\"></span>"
end

Expand Down
11 changes: 10 additions & 1 deletion lib/rdoc/text.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
##
# For RDoc::Text#to_html

require 'cgi/escape'
require 'strscan'

module RDoc
Expand Down Expand Up @@ -210,13 +211,21 @@ def wrap(txt, line_len = 76)
# - Remove characters that aren't alphanumeric, space, or hyphen
# - Replace spaces with hyphens
#
# If nothing is left, for example because +text+ only contains symbols
# or non-ASCII characters, falls back to the legacy label format so the
# anchor is never empty.
#
# Examples:
# "Hello World" -> "hello-world"
# "Foo::Bar" -> "foobar"
# "What's New?" -> "whats-new"
# "$=" -> "label-24-3D"

module_function def to_anchor(text)
text.downcase.gsub(/[^a-z0-9 \-]/, '').gsub(' ', '-')
anchor = text.downcase.gsub(/[^a-z0-9 \-]/, '').gsub(' ', '-')
return anchor unless anchor.empty?

"label-#{CGI.escape(text).gsub('%', '-').sub(/^-/, '')}"
end

##
Expand Down
20 changes: 20 additions & 0 deletions test/rdoc/markup/to_html_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,26 @@ def test_accept_heading_duplicate_punctuation_collision
assert_match(/<h2 id="method-match-1">/, result)
end

def test_accept_heading_symbols_only
@to.start_accepting

@to.accept_heading @RM::Heading.new(3, '$=')

assert_equal "\n<h3 id=\"label-24-3D\"><a href=\"#label-24-3D\">$=</a></h3>\n", @to.res.join
end

def test_accept_heading_symbols_only_duplicate
@to.start_accepting

@to.accept_heading @RM::Heading.new(3, '$=')
@to.accept_heading @RM::Heading.new(3, '$=')

result = @to.res.join
assert_match(/<h3 id="label-24-3D">/, result)
assert_match(/<h3 id="label-24-3D-1">/, result)
refute_match(/id=""/, result)
end

def test_accept_heading_three_duplicates
@to.start_accepting

Expand Down
10 changes: 10 additions & 0 deletions test/rdoc/rdoc_text_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,16 @@ def test_flush_left_encoding
assert_equal Encoding::US_ASCII, result.encoding
end

def test_to_anchor
assert_equal 'hello-world', to_anchor('Hello World')
assert_equal 'whats-new', to_anchor("What's New?")
end

def test_to_anchor_without_alphanumerics
assert_equal 'label-24-3D', to_anchor('$=')
assert_equal 'label-E6-97-A5-E6-9C-AC-E8-AA-9E', to_anchor('日本語')
end

def test_markup_string
out = markup('hi').gsub("\n", '')

Expand Down