From 8bb90f87fb73e2f21a45ba213c59f31a93311b78 Mon Sep 17 00:00:00 2001 From: Etienne Massip Date: Sun, 2 Oct 2011 15:57:17 +0000 Subject: [PATCH] Prepend page title to anchor in single page wiki HTML export to make links more unique. git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@7562 e93f8b46-1217-0410-a6f0-8f06a7374b81 --- app/helpers/application_helper.rb | 4 ++- test/unit/helpers/application_helper_test.rb | 29 ++++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 4f3316224..eb29829b2 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -577,7 +577,7 @@ module ApplicationHelper else case options[:wiki_links] when :local; "#{page.present? ? Wiki.titleize(page) : ''}.html" + (anchor.present? ? "##{anchor}" : '') - when :anchor; "##{title}" # used for single-file wiki export + when :anchor; "##{page.present? ? Wiki.titleize(page) : title}" + (anchor.present? ? "_#{anchor}" : '') # used for single-file wiki export else wiki_page_id = page.present? ? Wiki.titleize(page) : nil url_for(:only_path => only_path, :controller => 'wiki', :action => 'show', :project_id => link_project, :id => wiki_page_id, :anchor => anchor) @@ -728,6 +728,8 @@ module ApplicationHelper level, attrs, content = $1.to_i, $2, $3 item = strip_tags(content).strip anchor = item.gsub(%r{[^\w\s\-]}, '').gsub(%r{\s+(\-+\s*)?}, '-') + # used for single-file wiki export + anchor = "#{obj.page.title}_#{anchor}" if options[:wiki_links] == :anchor && (obj.is_a?(WikiContent) || obj.is_a?(WikiContent::Version)) @parsed_headings << [level, anchor, item] "\n#{content}" end diff --git a/test/unit/helpers/application_helper_test.rb b/test/unit/helpers/application_helper_test.rb index 366a167ea..db18f8aa8 100644 --- a/test/unit/helpers/application_helper_test.rb +++ b/test/unit/helpers/application_helper_test.rb @@ -580,6 +580,35 @@ EXPECTED to_test.each { |text, result| assert_equal "

#{result}

", textilizable(WikiContent.generate!( :text => text, :page => page ), :text) } end + def test_wiki_links_anchor_option_should_prepend_page_title_to_href + + to_test = { + # link to a page + '[[CookBook documentation]]' => 'CookBook documentation', + '[[CookBook documentation|documentation]]' => 'documentation', + '[[CookBook documentation#One-section]]' => 'CookBook documentation', + '[[CookBook documentation#One-section|documentation]]' => 'documentation', + # page that doesn't exist + '[[Unknown page]]' => 'Unknown page', + '[[Unknown page|404]]' => '404', + '[[Unknown page#anchor]]' => 'Unknown page', + '[[Unknown page#anchor|404]]' => '404', + } + + @project = Project.find(1) + + to_test.each { |text, result| assert_equal "

#{result}

", textilizable(text, :wiki_links => :anchor) } + end + + def test_headings_in_wiki_single_page_export_should_be_prepended_with_page_title + page = WikiPage.generate!( :title => 'Page Title' ) + content = WikiContent.generate!( :text => 'h1. Some heading', :page => page ) + + expected = %|\n

Some heading

| + + assert_equal expected, textilizable(content, :text, :wiki_links => :anchor ) + end + def test_table_of_content raw = <<-RAW {{toc}} -- 2.39.5