Adds Trac-Like anchors on wiki headings (#1647).

git-svn-id: http://redmine.rubyforge.org/svn/trunk@1705 e93f8b46-1217-0410-a6f0-8f06a7374b81
This commit is contained in:
Jean-Philippe Lang 2008-07-28 17:08:16 +00:00
parent b20281f151
commit 2dbc3d2943
3 changed files with 26 additions and 14 deletions

View File

@ -65,10 +65,22 @@ module Redmine
# Patch to add 'table of content' support to RedCloth # Patch to add 'table of content' support to RedCloth
def textile_p_withtoc(tag, atts, cite, content) def textile_p_withtoc(tag, atts, cite, content)
if tag =~ /^h(\d)$/ # removes wiki links from the item
@toc << [$1.to_i, content] toc_item = content.gsub(/(\[\[|\]\])/, '')
# 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*)?}, '-')
unless anchor.blank?
if tag =~ /^h(\d)$/
@toc << [$1.to_i, anchor, toc_item]
end
atts << " id=\"#{anchor}\""
content = content + "<a href=\"##{anchor}\" class=\"wiki-anchor\">&para;</a>"
end end
content = "<a name=\"#{@toc.length}\" class=\"wiki-page\"></a>" + content
textile_p(tag, atts, cite, content) textile_p(tag, atts, cite, content)
end end
@ -82,13 +94,9 @@ module Redmine
div_class << ' right' if $1 == '>' div_class << ' right' if $1 == '>'
div_class << ' left' if $1 == '<' div_class << ' left' if $1 == '<'
out = "<ul class=\"#{div_class}\">" out = "<ul class=\"#{div_class}\">"
@toc.each_with_index do |heading, index| @toc.each do |heading|
# remove wiki links from the item level, anchor, toc_item = heading
toc_item = heading.last.gsub(/(\[\[|\]\])/, '') out << "<li class=\"heading#{level}\"><a href=\"##{anchor}\">#{toc_item}</a></li>\n"
# remove styles
# eg. %{color:red}Triggers% => Triggers
toc_item.gsub! %r[%\{[^\}]*\}([^%]+)%], '\\1'
out << "<li class=\"heading#{heading.first}\"><a href=\"##{index+1}\">#{toc_item}</a></li>\n"
end end
out << '</ul>' out << '</ul>'
out out

View File

@ -487,6 +487,10 @@ div.wiki ul.toc a {
} }
div.wiki ul.toc a:hover { color: #c61a1a; text-decoration: underline;} div.wiki ul.toc a:hover { color: #c61a1a; text-decoration: underline;}
a.wiki-anchor { display: none; margin-left: 6px; text-decoration: none; }
a.wiki-anchor:hover { color: #aaa !important; text-decoration: none; }
h1:hover a.wiki-anchor, h2:hover a.wiki-anchor, h3:hover a.wiki-anchor { display: inline; color: #ddd; }
/***** My page layout *****/ /***** My page layout *****/
.block-receiver { .block-receiver {
border:1px dashed #c0c0c0; border:1px dashed #c0c0c0;

View File

@ -215,10 +215,10 @@ h1. Another title
RAW RAW
expected = '<ul class="toc">' + expected = '<ul class="toc">' +
'<li class="heading1"><a href="#1">Title</a></li>' + '<li class="heading1"><a href="#Title">Title</a></li>' +
'<li class="heading2"><a href="#2">Subtitle</a></li>' + '<li class="heading2"><a href="#Subtitle">Subtitle</a></li>' +
'<li class="heading2"><a href="#3">Subtitle with red text</a></li>' + '<li class="heading2"><a href="#Subtitle-with-red-text">Subtitle with red text</a></li>' +
'<li class="heading1"><a href="#4">Another title</a></li>' + '<li class="heading1"><a href="#Another-title">Another title</a></li>' +
'</ul>' '</ul>'
assert textilizable(raw).gsub("\n", "").include?(expected) assert textilizable(raw).gsub("\n", "").include?(expected)