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
This commit is contained in:
bootstraponline
2012-09-09 12:13:30 -06:00
parent 0bcd616668
commit 847f08d952
2 changed files with 30 additions and 8 deletions
+24 -8
View File
@@ -75,7 +75,9 @@ module Gollum
data = process_toc_tags(data) data = process_toc_tags(data)
data = process_tex(data) data = process_tex(data)
data = process_wsd(data) data = process_wsd(data)
data.gsub!(/<p><\/p>/, '') data.gsub!(/<p><\/p>/) do
''
end
data data
end end
@@ -155,7 +157,9 @@ module Gollum
def process_tex(data) def process_tex(data)
@texmap.each do |id, spec| @texmap.each do |id, spec|
type, tex = *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 end
data data
end end
@@ -210,9 +214,13 @@ module Gollum
@tagmap.each do |id, tag| @tagmap.each do |id, tag|
# If it's preformatted, just put the tag back # If it's preformatted, just put the tag back
if is_preformatted?(data, id) if is_preformatted?(data, id)
data.gsub!(id, "[[#{tag}]]") data.gsub!(id) do
"[[#{tag}]]"
end
else else
data.gsub!(id, process_tag(tag).gsub('%2F', '/')) data.gsub!(id) do
process_tag(tag).gsub('%2F', '/')
end
end end
end end
data data
@@ -404,7 +412,9 @@ module Gollum
# #
# Returns the marked up String data. # Returns the marked up String data.
def process_toc_tags(data) def process_toc_tags(data)
data.gsub!("[[_TOC_]]", @toc.nil? ? '' : @toc) data.gsub!("[[_TOC_]]") do
@toc.nil? ? '' : @toc
end
data data
end end
@@ -481,7 +491,9 @@ module Gollum
# regex - A regex to match whitespace # regex - A regex to match whitespace
def remove_leading_space(code, regex) def remove_leading_space(code, regex)
if code.lines.all? { |line| line =~ /\A\r?\n\Z/ || line =~ regex } if code.lines.all? { |line| line =~ /\A\r?\n\Z/ || line =~ regex }
code.gsub!(regex, '') code.gsub!(regex) do
''
end
end end
end end
@@ -527,7 +539,9 @@ module Gollum
"<pre><code>#{CGI.escapeHTML(spec[:code])}</code></pre>" "<pre><code>#{CGI.escapeHTML(spec[:code])}</code></pre>"
end end
end end
data.gsub!(id, body) data.gsub!(id) do
body
end
end end
data data
@@ -563,7 +577,9 @@ module Gollum
@wsdmap.each do |id, spec| @wsdmap.each do |id, spec|
style = spec[:style] style = spec[:style]
code = spec[:code] 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 end
data data
end end
+6
View File
@@ -184,6 +184,12 @@ context "Markup" do
assert_equal "<p><code>sed -i '' 's/[[:space:]]*$//'</code></p>", page.formatted_data assert_equal "<p><code>sed -i '' 's/[[:space:]]*$//'</code></p>", page.formatted_data
end 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 "<p><code>rot13='tr '\''A-Za-z'\'' '\''N-ZA-Mn-za-m'\'</code></p>", page.formatted_data
end
test "wiki link within code block" do test "wiki link within code block" do
@wiki.write_page("Potato", :markdown, " sed -i '' 's/[[:space:]]*$//'", commit_details) @wiki.write_page("Potato", :markdown, " sed -i '' 's/[[:space:]]*$//'", commit_details)
page = @wiki.page("Potato") page = @wiki.page("Potato")