From 847f08d952384b8ccb235d5ff0d104c89814ee91 Mon Sep 17 00:00:00 2001 From: bootstraponline Date: Sun, 9 Sep 2012 12:13:30 -0600 Subject: [PATCH] Use block form of gsub to avoid regexp backref interpolation The content of this commit message is from @kislyuk's comments on the below two issues. Fix #383 Fix #511 `gsub!(pattern, replacement) interpolates` regexp backreferences `gsub!(pattern) do block` doesn't http://stackoverflow.com/questions/2082457/ruby-gsub-problem-when-using-backreference-and-hashes --- lib/gollum/markup.rb | 32 ++++++++++++++++++++++++-------- test/test_markup.rb | 6 ++++++ 2 files changed, 30 insertions(+), 8 deletions(-) diff --git a/lib/gollum/markup.rb b/lib/gollum/markup.rb index e1970338..63c25c45 100644 --- a/lib/gollum/markup.rb +++ b/lib/gollum/markup.rb @@ -75,7 +75,9 @@ module Gollum data = process_toc_tags(data) data = process_tex(data) data = process_wsd(data) - data.gsub!(/

<\/p>/, '') + data.gsub!(/

<\/p>/) do + '' + end data end @@ -155,7 +157,9 @@ module Gollum def process_tex(data) @texmap.each do |id, spec| type, tex = *spec - data.gsub!(id, Gollum::Tex.to_html(tex, type)) + data.gsub!(id) do + Gollum::Tex.to_html(tex, type) + end end data end @@ -210,9 +214,13 @@ module Gollum @tagmap.each do |id, tag| # If it's preformatted, just put the tag back if is_preformatted?(data, id) - data.gsub!(id, "[[#{tag}]]") + data.gsub!(id) do + "[[#{tag}]]" + end else - data.gsub!(id, process_tag(tag).gsub('%2F', '/')) + data.gsub!(id) do + process_tag(tag).gsub('%2F', '/') + end end end data @@ -404,7 +412,9 @@ module Gollum # # Returns the marked up String data. def process_toc_tags(data) - data.gsub!("[[_TOC_]]", @toc.nil? ? '' : @toc) + data.gsub!("[[_TOC_]]") do + @toc.nil? ? '' : @toc + end data end @@ -481,7 +491,9 @@ module Gollum # regex - A regex to match whitespace def remove_leading_space(code, regex) if code.lines.all? { |line| line =~ /\A\r?\n\Z/ || line =~ regex } - code.gsub!(regex, '') + code.gsub!(regex) do + '' + end end end @@ -527,7 +539,9 @@ module Gollum "

#{CGI.escapeHTML(spec[:code])}
" end end - data.gsub!(id, body) + data.gsub!(id) do + body + end end data @@ -563,7 +577,9 @@ module Gollum @wsdmap.each do |id, spec| style = spec[:style] code = spec[:code] - data.gsub!(id, Gollum::WebSequenceDiagram.new(code, style).to_tag) + data.gsub!(id) do + Gollum::WebSequenceDiagram.new(code, style).to_tag + end end data end diff --git a/test/test_markup.rb b/test/test_markup.rb index c62ffd09..1659856a 100644 --- a/test/test_markup.rb +++ b/test/test_markup.rb @@ -184,6 +184,12 @@ context "Markup" do assert_equal "

sed -i '' 's/[[:space:]]*$//'

", page.formatted_data end + test "regexp gsub! backref (#383)" do + @wiki.write_page("Potato", :markdown, "`rot13='tr '\''A-Za-z'\'' '\''N-ZA-Mn-za-m'\'`", commit_details) + page = @wiki.page("Potato") + assert_equal "

rot13='tr '\''A-Za-z'\'' '\''N-ZA-Mn-za-m'\'

", page.formatted_data + end + test "wiki link within code block" do @wiki.write_page("Potato", :markdown, " sed -i '' 's/[[:space:]]*$//'", commit_details) page = @wiki.page("Potato")