Patch #3749 » Wiki_TOC_The_standardised_anchors_for_redmine2845-08stable.diff
| test/unit/helpers/application_helper_test.rb (working copy) | ||
|---|---|---|
| 294 | 294 |
end |
| 295 | 295 |
|
| 296 | 296 |
def test_table_of_content |
| 297 |
raw = <<-RAW
|
|
| 297 |
raw = <<-'RAW'
|
|
| 298 | 298 |
{{toc}}
|
| 299 | 299 | |
| 300 | 300 |
h1. Title |
| ... | ... | |
| 309 | 309 | |
| 310 | 310 |
h1. Another title |
| 311 | 311 | |
| 312 |
h2. Subtitle with x%x% redcloth3 dummy character |
|
| 313 | ||
| 314 |
h2. Subtitle with "#$%'()*+,-./:;<=>?@[\]^_{|}~ incorrect characters
|
|
| 315 | ||
| 316 |
h2. Subtitle with russian character "абв" (utf8) |
|
| 317 | ||
| 312 | 318 |
RAW |
| 313 | 319 | |
| 314 | 320 |
expected = '<ul class="toc">' + |
| 315 |
'<li class="heading1"><a href="#Title">Title</a></li>' + |
|
| 316 |
'<li class="heading2"><a href="#Subtitle">Subtitle</a></li>' + |
|
| 317 |
'<li class="heading2"><a href="#Subtitle-with-red-text">Subtitle with red text</a></li>' + |
|
| 318 |
'<li class="heading1"><a href="#Another-title">Another title</a></li>' + |
|
| 321 |
'<li class="heading1"><a href="#h-Title">Title</a></li>' + |
|
| 322 |
'<li class="heading2"><a href="#h-Subtitle">Subtitle</a></li>' + |
|
| 323 |
'<li class="heading2"><a href="#h-Subtitle-with-red-text">Subtitle with red text</a></li>' + |
|
| 324 |
'<li class="heading1"><a href="#h-Another-title">Another title</a></li>' + |
|
| 325 |
'<li class="heading2"><a href="#h-Subtitle-with-redcloth3-dummy-character">Subtitle with & redcloth3 dummy character</a></li>' + |
|
| 326 |
'<li class="heading2"><a href="#h-Subtitle-with-incorrect-characters">Subtitle with "#$%\'()*+,-./:;<=>?@[\]^_{|}~ incorrect characters</a></li>' +
|
|
| 327 |
'<li class="heading2"><a href="#h-Subtitle-with-russian-character-.D0.B0.D0.B1.D0.B2-utf8">Subtitle with russian character "абв" (utf8)</a></li>' + |
|
| 319 | 328 |
'</ul>' |
| 320 | 329 |
|
| 321 | 330 |
assert textilizable(raw).gsub("\n", "").include?(expected)
|
| lib/redmine/wiki_formatting/textile/formatter.rb (working copy) | ||
|---|---|---|
| 66 | 66 |
def textile_p_withtoc(tag, atts, cite, content) |
| 67 | 67 |
# removes wiki links from the item |
| 68 | 68 |
toc_item = content.gsub(/(\[\[|\]\])/, '') |
| 69 | ||
| 69 | 70 |
# removes styles |
| 70 | 71 |
# eg. %{color:red}Triggers% => Triggers
|
| 71 | 72 |
toc_item.gsub! %r[%\{[^\}]*\}([^%]+)%], '\\1'
|
| 72 | 73 |
|
| 73 |
# replaces non word caracters by dashes |
|
| 74 |
anchor = toc_item.gsub(%r{[^\w\s\-]}, '').gsub(%r{\s+(\-+\s*)?}, '-')
|
|
| 74 |
# removes all except the alphabet, digits, hyphens |
|
| 75 |
# and dummy character of incoming ampersand (see redcloth3) |
|
| 76 |
# for compatibility with rfc952, also replace space and repeatable hyphens |
|
| 77 |
anchor = toc_item.gsub(%r{[^\w\s\-]|_|(x\%x\%)}, '').gsub(%r{\s+(\-+\s*)?}, '-')
|
|
| 75 | 78 |
|
| 79 |
# ID and NAME tokens must begin with a letter ([A-Za-z]) and may be followed |
|
| 80 |
# by any number of letters, digits ([0-9]), hyphens ("-"), underscores ("_"),
|
|
| 81 |
# colons (":"), and periods ("."). (http://www.w3.org/TR/html4/types.html#h-6.2)
|
|
| 82 |
anchor = 'h-' + CGI.escape(anchor).gsub('%', '.')
|
|
| 83 | ||
| 84 | ||
| 76 | 85 |
unless anchor.blank? |
| 77 | 86 |
if tag =~ /^h(\d)$/ |
| 78 | 87 |
@toc << [$1.to_i, anchor, toc_item] |