diff options
author | Jean-Philippe Lang <jp_lang@yahoo.fr> | 2010-11-06 17:47:27 +0000 |
---|---|---|
committer | Jean-Philippe Lang <jp_lang@yahoo.fr> | 2010-11-06 17:47:27 +0000 |
commit | 024ff96ee27aa7e61ceec25a351f6800461d5cf3 (patch) | |
tree | d0cc3a32d0990d422916caa5ff4793beb5a990e5 /app/helpers/application_helper.rb | |
parent | 666c54e86c2a8532ff9330d7ce4e9e7d3a44b173 (diff) | |
download | redmine-024ff96ee27aa7e61ceec25a351f6800461d5cf3.tar.gz redmine-024ff96ee27aa7e61ceec25a351f6800461d5cf3.zip |
Extract headings and TOC parsing from the textile formatter.
Fixes #2038 and #3707 and will allow to support TOC with other text formatters.
git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@4376 e93f8b46-1217-0410-a6f0-8f06a7374b81
Diffstat (limited to 'app/helpers/application_helper.rb')
-rw-r--r-- | app/helpers/application_helper.rb | 38 |
1 files changed, 35 insertions, 3 deletions
diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index bf4e2d5ba..0e4b0a8c3 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -1,5 +1,5 @@ -# redMine - project management software -# Copyright (C) 2006-2007 Jean-Philippe Lang +# Redmine - project management software +# Copyright (C) 2006-2010 Jean-Philippe Lang # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License @@ -451,7 +451,7 @@ module ApplicationHelper text = Redmine::WikiFormatting.to_html(Setting.text_formatting, text, :object => obj, :attribute => attr) { |macro, args| exec_macro(macro, obj, args) } parse_non_pre_blocks(text) do |text| - [:parse_inline_attachments, :parse_wiki_links, :parse_redmine_links].each do |method_name| + [:parse_inline_attachments, :parse_wiki_links, :parse_redmine_links, :parse_headings].each do |method_name| send method_name, text, project, obj, attr, only_path, options end end @@ -673,6 +673,38 @@ module ApplicationHelper leading + (link || "#{prefix}#{sep}#{identifier}") end end + + TOC_RE = /<p>\{\{([<>]?)toc\}\}<\/p>/i unless const_defined?(:TOC_RE) + HEADING_RE = /<h(1|2|3)( [^>]+)?>(.+?)<\/h(1|2|3)>/i unless const_defined?(:HEADING_RE) + + # Headings and TOC + # Adds ids and links to headings and renders the TOC if needed unless options[:headings] is set to false + def parse_headings(text, project, obj, attr, only_path, options) + headings = [] + text.gsub!(HEADING_RE) do + level, attrs, content = $1, $2, $3 + item = strip_tags(content).strip + anchor = item.gsub(%r{[^\w\s\-]}, '').gsub(%r{\s+(\-+\s*)?}, '-') + headings << [level, anchor, item] + "<h#{level} #{attrs} id=\"#{anchor}\">#{content}<a href=\"##{anchor}\" class=\"wiki-anchor\">¶</a></h#{level}>" + end unless options[:headings] == false + + text.gsub!(TOC_RE) do + if headings.empty? + '' + else + div_class = 'toc' + div_class << ' right' if $1 == '>' + div_class << ' left' if $1 == '<' + out = "<ul class=\"#{div_class}\">" + headings.each do |level, anchor, item| + out << "<li class=\"heading#{level}\"><a href=\"##{anchor}\">#{item}</a></li>\n" + end + out << '</ul>' + out + end + end + end # Same as Rails' simple_format helper without using paragraphs def simple_format_without_paragraph(text) |