From 7203196212ae78d3723c0df4c661a3c03769135a Mon Sep 17 00:00:00 2001 From: Jean-Philippe Lang Date: Mon, 15 Mar 2010 19:54:50 +0000 Subject: [PATCH] Do not parse redmine links inside pre/code tags (#1288). git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@3589 e93f8b46-1217-0410-a6f0-8f06a7374b81 --- app/helpers/application_helper.rb | 35 +++++++++++++++++--- test/unit/helpers/application_helper_test.rb | 27 +++++++++++++++ 2 files changed, 57 insertions(+), 5 deletions(-) diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index ece92d615..b4eaa91de 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -409,12 +409,37 @@ module ApplicationHelper only_path = options.delete(:only_path) == false ? false : true text = Redmine::WikiFormatting.to_html(Setting.text_formatting, text, :object => obj, :attribute => attr) { |macro, args| exec_macro(macro, obj, args) } - - [:parse_inline_attachments, :parse_wiki_links, :parse_redmine_links].each do |method_name| - send method_name, text, project, obj, attr, only_path, options + + parse_non_pre_blocks(text) do |text| + [:parse_inline_attachments, :parse_wiki_links, :parse_redmine_links].each do |method_name| + send method_name, text, project, obj, attr, only_path, options + end end - - text + end + + def parse_non_pre_blocks(text) + s = StringScanner.new(text) + tags = [] + parsed = '' + while !s.eos? + s.scan(/(.*?)(<(\/)?(pre|code)(.*?)>|\z)/im) + text, full_tag, closing, tag = s[1], s[2], s[3], s[4] + if tags.empty? + yield text + end + parsed << text + if tag + if closing + if tags.last == tag.downcase + tags.pop + end + else + tags << tag.downcase + end + parsed << full_tag + end + end + parsed end def parse_inline_attachments(text, project, obj, attr, only_path, options) diff --git a/test/unit/helpers/application_helper_test.rb b/test/unit/helpers/application_helper_test.rb index 3be0d7dbd..011df82fe 100644 --- a/test/unit/helpers/application_helper_test.rb +++ b/test/unit/helpers/application_helper_test.rb @@ -297,6 +297,33 @@ EXPECTED assert_equal expected.gsub(%r{[\r\n\t]}, ''), textilizable(raw).gsub(%r{[\r\n\t]}, '') end + def test_pre_content_should_not_parse_wiki_and_redmine_links + raw = <<-RAW +[[CookBook documentation]] + +#1 + +
+[[CookBook documentation]]
+  
+#1
+
+RAW + + expected = <<-EXPECTED +

CookBook documentation

+

#1

+
+[[CookBook documentation]]
+
+#1
+
+EXPECTED + + @project = Project.find(1) + assert_equal expected.gsub(%r{[\r\n\t]}, ''), textilizable(raw).gsub(%r{[\r\n\t]}, '') + end + def test_syntax_highlight raw = <<-RAW

-- 
2.39.5