diff options
author | Jean-Philippe Lang <jp_lang@yahoo.fr> | 2007-09-09 10:16:59 +0000 |
---|---|---|
committer | Jean-Philippe Lang <jp_lang@yahoo.fr> | 2007-09-09 10:16:59 +0000 |
commit | f3364b9dce6f13905e94252b732b203203cded96 (patch) | |
tree | b9eed0f142ea2c0eeae7a08bcaf8bed7abd8ff7b /lib/redmine/wiki_formatting.rb | |
parent | 7d899166c6a15bc9434fbdd3737ca001362e0aec (diff) | |
download | redmine-f3364b9dce6f13905e94252b732b203203cded96.tar.gz redmine-f3364b9dce6f13905e94252b732b203203cded96.zip |
Added automatic table of content support on wiki pages, based on h1., h2. and h3. headings.
To display the page TOC, insert a line with this tag:
{{toc}} or {{<toc}} => left aligned toc
{{>toc}} => right aligned toc
git-svn-id: http://redmine.rubyforge.org/svn/trunk@718 e93f8b46-1217-0410-a6f0-8f06a7374b81
Diffstat (limited to 'lib/redmine/wiki_formatting.rb')
-rw-r--r-- | lib/redmine/wiki_formatting.rb | 31 |
1 files changed, 29 insertions, 2 deletions
diff --git a/lib/redmine/wiki_formatting.rb b/lib/redmine/wiki_formatting.rb index e9f5eb749..2d26cabd2 100644 --- a/lib/redmine/wiki_formatting.rb +++ b/lib/redmine/wiki_formatting.rb @@ -7,14 +7,15 @@ module Redmine private class TextileFormatter < RedCloth - RULES = [:inline_auto_link, :inline_auto_mailto, :textile ] - + RULES = [:inline_auto_link, :inline_auto_mailto, :textile, :inline_toc] + def initialize(*args) super self.hard_breaks=true end def to_html + @toc = [] super(*RULES).to_s end @@ -41,6 +42,32 @@ module Redmine end end + # Patch to add 'table of content' support to RedCloth + def textile_p_withtoc(tag, atts, cite, content) + if tag =~ /^h(\d)$/ + @toc << [$1.to_i, content] + end + content = "<a name=\"#{@toc.length}-#{content}\" class=\"wiki-page\"></a>" + content + textile_p(tag, atts, cite, content) + end + + alias :textile_h1 :textile_p_withtoc + alias :textile_h2 :textile_p_withtoc + alias :textile_h3 :textile_p_withtoc + + def inline_toc(text) + text.gsub!(/<p>\{\{(<>?)toc\}\}<\/p>/i) do + div_class = 'toc' + div_class << ' right' if $1 == '>' + out = "<div class=\"#{div_class}\">" + @toc.each_with_index do |heading, index| + out << "<a href=\"##{index+1}-#{heading.last}\" class=\"heading#{heading.first}\">#{heading.last}</a>" + end + out << '</div>' + out + end + end + AUTO_LINK_RE = %r{ ( # leading text <\w+.*?>| # leading HTML tag, or |