Project

General

Profile

Patch #3749 » Wiki_TOC_The_standardised_anchors_for_redmine2845trunk.diff

Roman Musin, 2009-08-18 01:10

View differences:

test/unit/helpers/application_helper_test.rb (working copy)
end
def test_table_of_content
raw = <<-RAW
raw = <<-'RAW'
{{toc}}
h1. Title
......
h1. Another title
h2. Subtitle with x%x% redcloth3 dummy character
h2. Subtitle with "#$%'()*+,-./:;<=>?@[\]^_{|}~ incorrect characters
h2. Subtitle with russian character "абв" (utf8)
RAW
expected = '<ul class="toc">' +
'<li class="heading1"><a href="#Title">Title</a></li>' +
'<li class="heading2"><a href="#Subtitle-with-a-Wiki-link">Subtitle with a Wiki link</a></li>' +
'<li class="heading2"><a href="#Subtitle-with-another-Wiki-link">Subtitle with another Wiki link</a></li>' +
'<li class="heading2"><a href="#Subtitle-with-red-text">Subtitle with red text</a></li>' +
'<li class="heading1"><a href="#Another-title">Another title</a></li>' +
'<li class="heading1"><a href="#h-Title">Title</a></li>' +
'<li class="heading2"><a href="#h-Subtitle-with-a-Wiki-link">Subtitle with a Wiki link</a></li>' +
'<li class="heading2"><a href="#h-Subtitle-with-another-Wiki-link">Subtitle with another Wiki link</a></li>' +
'<li class="heading2"><a href="#h-Subtitle-with-red-text">Subtitle with red text</a></li>' +
'<li class="heading1"><a href="#h-Another-title">Another title</a></li>' +
'<li class="heading2"><a href="#h-Subtitle-with-redcloth3-dummy-character">Subtitle with &#38; redcloth3 dummy character</a></li>' +
'<li class="heading2"><a href="#h-Subtitle-with-incorrect-characters">Subtitle with "#$%\'()*+,-./:;<=>?@[\]^_{|}~ incorrect characters</a></li>' +
'<li class="heading2"><a href="#h-Subtitle-with-russian-character-.D0.B0.D0.B1.D0.B2-utf8">Subtitle with russian character "абв" (utf8)</a></li>' +
'</ul>'
assert textilizable(raw).gsub("\n", "").include?(expected)
lib/redmine/wiki_formatting/textile/formatter.rb (working copy)
def textile_p_withtoc(tag, atts, cite, content)
# removes wiki links from the item
toc_item = content.gsub(/(\[\[([^\]\|]*)(\|([^\]]*))?\]\])/) { $4 || $2 }
# removes styles
# eg. %{color:red}Triggers% => Triggers
toc_item.gsub! %r[%\{[^\}]*\}([^%]+)%], '\\1'
# replaces non word caracters by dashes
anchor = toc_item.gsub(%r{[^\w\s\-]}, '').gsub(%r{\s+(\-+\s*)?}, '-')
# removes all except the alphabet, digits, hyphens
# and dummy character of incoming ampersand (see redcloth3)
# for compatibility with rfc952, also replace space and repeatable hyphens
anchor = toc_item.gsub(%r{[^\w\s\-]|_|(x\%x\%)}, '').gsub(%r{\s+(\-+\s*)?}, '-')
# ID and NAME tokens must begin with a letter ([A-Za-z]) and may be followed
# by any number of letters, digits ([0-9]), hyphens ("-"), underscores ("_"),
# colons (":"), and periods ("."). (http://www.w3.org/TR/html4/types.html#h-6.2)
anchor = 'h-' + CGI.escape(anchor).gsub('%', '.')
unless anchor.blank?
if tag =~ /^h(\d)$/
@toc << [$1.to_i, anchor, toc_item]
(2-2/2)