@@ -449,12 +449,19 module ApplicationHelper | |||||
449 | only_path = options.delete(:only_path) == false ? false : true |
|
449 | only_path = options.delete(:only_path) == false ? false : true | |
450 |
|
450 | |||
451 | text = Redmine::WikiFormatting.to_html(Setting.text_formatting, text, :object => obj, :attribute => attr) { |macro, args| exec_macro(macro, obj, args) } |
|
451 | text = Redmine::WikiFormatting.to_html(Setting.text_formatting, text, :object => obj, :attribute => attr) { |macro, args| exec_macro(macro, obj, args) } | |
452 |
|
|
452 | ||
453 | parse_non_pre_blocks(text) do |text| |
|
453 | @parsed_headings = [] | |
|
454 | text = parse_non_pre_blocks(text) do |text| | |||
454 | [:parse_inline_attachments, :parse_wiki_links, :parse_redmine_links, :parse_headings].each do |method_name| |
|
455 | [:parse_inline_attachments, :parse_wiki_links, :parse_redmine_links, :parse_headings].each do |method_name| | |
455 | send method_name, text, project, obj, attr, only_path, options |
|
456 | send method_name, text, project, obj, attr, only_path, options | |
456 | end |
|
457 | end | |
457 | end |
|
458 | end | |
|
459 | ||||
|
460 | if @parsed_headings.any? | |||
|
461 | replace_toc(text, @parsed_headings) | |||
|
462 | end | |||
|
463 | ||||
|
464 | text | |||
458 | end |
|
465 | end | |
459 |
|
466 | |||
460 | def parse_non_pre_blocks(text) |
|
467 | def parse_non_pre_blocks(text) | |
@@ -674,21 +681,26 module ApplicationHelper | |||||
674 | end |
|
681 | end | |
675 | end |
|
682 | end | |
676 |
|
683 | |||
677 | TOC_RE = /<p>\{\{([<>]?)toc\}\}<\/p>/i unless const_defined?(:TOC_RE) |
|
|||
678 | HEADING_RE = /<h(1|2|3|4)( [^>]+)?>(.+?)<\/h(1|2|3|4)>/i unless const_defined?(:HEADING_RE) |
|
684 | HEADING_RE = /<h(1|2|3|4)( [^>]+)?>(.+?)<\/h(1|2|3|4)>/i unless const_defined?(:HEADING_RE) | |
679 |
|
685 | |||
680 | # Headings and TOC |
|
686 | # Headings and TOC | |
681 |
# Adds ids and links to headings |
|
687 | # Adds ids and links to headings unless options[:headings] is set to false | |
682 | def parse_headings(text, project, obj, attr, only_path, options) |
|
688 | def parse_headings(text, project, obj, attr, only_path, options) | |
683 | headings = [] |
|
689 | return if options[:headings] == false | |
|
690 | ||||
684 | text.gsub!(HEADING_RE) do |
|
691 | text.gsub!(HEADING_RE) do | |
685 | level, attrs, content = $1.to_i, $2, $3 |
|
692 | level, attrs, content = $1.to_i, $2, $3 | |
686 | item = strip_tags(content).strip |
|
693 | item = strip_tags(content).strip | |
687 | anchor = item.gsub(%r{[^\w\s\-]}, '').gsub(%r{\s+(\-+\s*)?}, '-') |
|
694 | anchor = item.gsub(%r{[^\w\s\-]}, '').gsub(%r{\s+(\-+\s*)?}, '-') | |
688 | headings << [level, anchor, item] |
|
695 | @parsed_headings << [level, anchor, item] | |
689 | "<h#{level} #{attrs} id=\"#{anchor}\">#{content}<a href=\"##{anchor}\" class=\"wiki-anchor\">¶</a></h#{level}>" |
|
696 | "<h#{level} #{attrs} id=\"#{anchor}\">#{content}<a href=\"##{anchor}\" class=\"wiki-anchor\">¶</a></h#{level}>" | |
690 | end unless options[:headings] == false |
|
697 | end | |
691 |
|
698 | end | ||
|
699 | ||||
|
700 | TOC_RE = /<p>\{\{([<>]?)toc\}\}<\/p>/i unless const_defined?(:TOC_RE) | |||
|
701 | ||||
|
702 | # Renders the TOC with given headings | |||
|
703 | def replace_toc(text, headings) | |||
692 | text.gsub!(TOC_RE) do |
|
704 | text.gsub!(TOC_RE) do | |
693 | if headings.empty? |
|
705 | if headings.empty? | |
694 | '' |
|
706 | '' |
@@ -428,7 +428,11 Nullam commodo metus accumsan nulla. Curabitur lobortis dui id dolor. | |||||
428 | h2. Subtitle with [[Wiki|another Wiki]] link |
|
428 | h2. Subtitle with [[Wiki|another Wiki]] link | |
429 |
|
429 | |||
430 | h2. Subtitle with %{color:red}red text% |
|
430 | h2. Subtitle with %{color:red}red text% | |
431 |
|
431 | |||
|
432 | <pre> | |||
|
433 | some code | |||
|
434 | </pre> | |||
|
435 | ||||
432 | h3. Subtitle with *some* _modifiers_ |
|
436 | h3. Subtitle with *some* _modifiers_ | |
433 |
|
437 | |||
434 | h1. Another title |
|
438 | h1. Another title | |
@@ -464,7 +468,7 RAW | |||||
464 | '</ul>' |
|
468 | '</ul>' | |
465 |
|
469 | |||
466 | @project = Project.find(1) |
|
470 | @project = Project.find(1) | |
467 | assert textilizable(raw).gsub("\n", "").include?(expected) |
|
471 | assert textilizable(raw).gsub("\n", "").include?(expected), textilizable(raw) | |
468 | end |
|
472 | end | |
469 |
|
473 | |||
470 | def test_table_of_content_should_contain_included_page_headings |
|
474 | def test_table_of_content_should_contain_included_page_headings |
General Comments 0
You need to be logged in to leave comments.
Login now