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)
304 304
  end
305 305
  def test_table_of_content
306
    raw = <<-RAW
306
    raw = <<-'RAW'
307 307
{{toc}}
308 308
h1. Title
......
321 321
h1. Another title
322
h2. Subtitle with x%x% redcloth3 dummy character
323

  
324
h2. Subtitle with "#$%'()*+,-./:;<=>?@[\]^_{|}~ incorrect characters
325

  
326
h2. Subtitle with russian character "абв" (utf8)
327

  
322 328
RAW
323 329
    expected = '<ul class="toc">' +
324
               '<li class="heading1"><a href="#Title">Title</a></li>' +
325
               '<li class="heading2"><a href="#Subtitle-with-a-Wiki-link">Subtitle with a Wiki link</a></li>' +
326
               '<li class="heading2"><a href="#Subtitle-with-another-Wiki-link">Subtitle with another Wiki link</a></li>' +
327
               '<li class="heading2"><a href="#Subtitle-with-red-text">Subtitle with red text</a></li>' +
328
               '<li class="heading1"><a href="#Another-title">Another title</a></li>' +
330
               '<li class="heading1"><a href="#h-Title">Title</a></li>' +
331
               '<li class="heading2"><a href="#h-Subtitle-with-a-Wiki-link">Subtitle with a Wiki link</a></li>' +
332
               '<li class="heading2"><a href="#h-Subtitle-with-another-Wiki-link">Subtitle with another Wiki link</a></li>' +
333
               '<li class="heading2"><a href="#h-Subtitle-with-red-text">Subtitle with red text</a></li>' +
334
               '<li class="heading1"><a href="#h-Another-title">Another title</a></li>' +
335
               '<li class="heading2"><a href="#h-Subtitle-with-redcloth3-dummy-character">Subtitle with &#38; redcloth3 dummy character</a></li>' +
336
               '<li class="heading2"><a href="#h-Subtitle-with-incorrect-characters">Subtitle with "#$%\'()*+,-./:;<=>?@[\]^_{|}~ incorrect characters</a></li>' +
337
               '<li class="heading2"><a href="#h-Subtitle-with-russian-character-.D0.B0.D0.B1.D0.B2-utf8">Subtitle with russian character "абв" (utf8)</a></li>' +
329 338
               '</ul>'
330 339
    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(/(\[\[([^\]\|]*)(\|([^\]]*))?\]\])/) { $4 || $2 }
69

  
69 70
          # removes styles
70 71
          # eg. %{color:red}Triggers% => Triggers
71 72
          toc_item.gsub! %r[%\{[^\}]*\}([^%]+)%], '\\1'
72
          # replaces non word caracters by dashes
73
          anchor = toc_item.gsub(%r{[^\w\s\-]}, '').gsub(%r{\s+(\-+\s*)?}, '-')
73
          # removes all except the alphabet, digits, hyphens
74
          # and dummy character of incoming ampersand (see redcloth3)
75
          # for compatibility with rfc952, also replace space and repeatable hyphens
76
          anchor = toc_item.gsub(%r{[^\w\s\-]|_|(x\%x\%)}, '').gsub(%r{\s+(\-+\s*)?}, '-')
77
          # ID and NAME tokens must begin with a letter ([A-Za-z]) and may be followed
78
          # by any number of letters, digits ([0-9]), hyphens ("-"), underscores ("_"),
79
          # colons (":"), and periods ("."). (http://www.w3.org/TR/html4/types.html#h-6.2)
80
          anchor = 'h-' + CGI.escape(anchor).gsub('%', '.')
81

  
74 82
          unless anchor.blank?
75 83
            if tag =~ /^h(\d)$/
76 84
              @toc << [$1.to_i, anchor, toc_item]
(2-2/2)