From eb37b17486386d805f474847495d4db04ec6da07 Mon Sep 17 00:00:00 2001 From: Jonathan Roes Date: Fri, 11 May 2012 22:49:26 -0400 Subject: [PATCH 1/2] Remove links in code, fixes #128. --- lib/gollum/markup.rb | 16 ++++++++++++++-- test/test_markup.rb | 19 ++++++++++++++++--- 2 files changed, 30 insertions(+), 5 deletions(-) diff --git a/lib/gollum/markup.rb b/lib/gollum/markup.rb index 06463be7..f4a92b4f 100644 --- a/lib/gollum/markup.rb +++ b/lib/gollum/markup.rb @@ -53,9 +53,12 @@ module Gollum end data = process_tags(data) data = process_code(data, encoding) + + doc = Nokogiri::HTML::DocumentFragment.parse(data) + doc = remove_links_in_code(doc) + if sanitize || block_given? - doc = Nokogiri::HTML::DocumentFragment.parse(data) - doc = sanitize.clean_node!(doc) if sanitize + doc = sanitize.clean_node!(doc) if sanitize yield doc if block_given? data = doc.to_html end @@ -353,6 +356,15 @@ module Gollum end end + def remove_links_in_code(doc) + doc.css('code > a').each do |link| + link.before("[[#{link.content}]]") + link.remove + end + + doc + end + ######################################################################### # # Code diff --git a/test/test_markup.rb b/test/test_markup.rb index 4a9ced06..2a2b2d0c 100644 --- a/test/test_markup.rb +++ b/test/test_markup.rb @@ -173,7 +173,20 @@ context "Markup" do @wiki.write_page("Potato", :mediawiki, "a [[Potato|Potato Heaad]] ", commit_details) page = @wiki.page("Potato") output = page.formatted_data - assert_equal normal("

\na Potato Heaad

"), normal(output) + assert_equal normal("

\na Potato Heaad

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

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

", 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") + assert_equal "
sed -i '' 's/[[:space:]]*$//'\n
", page.formatted_data end ######################################################################### @@ -598,11 +611,11 @@ np.array([[2,2],[1,3]],np.float) # Asciidoc ######################################################################### - test "asciidoc header" do + test "asciidoc header" do compare("= Book Title\n\n== Heading", '

Heading

', 'asciidoc') end - test "internal links with asciidoc" do + test "internal links with asciidoc" do compare("= Book Title\n\n[[anid]]\n== Heading", '

Heading

', 'asciidoc') end From f9899033aade07bacb2617257f07742ca8994144 Mon Sep 17 00:00:00 2001 From: Jonathan Roes Date: Sat, 12 May 2012 17:29:23 -0400 Subject: [PATCH 2/2] Before replacing tags, ensure the SHA1 is not within preformatted content. --- lib/gollum/markup.rb | 33 ++++++++++++++++++++------------- test/test_markup.rb | 6 ++++++ 2 files changed, 26 insertions(+), 13 deletions(-) diff --git a/lib/gollum/markup.rb b/lib/gollum/markup.rb index f4a92b4f..3bac70d1 100644 --- a/lib/gollum/markup.rb +++ b/lib/gollum/markup.rb @@ -54,10 +54,8 @@ module Gollum data = process_tags(data) data = process_code(data, encoding) - doc = Nokogiri::HTML::DocumentFragment.parse(data) - doc = remove_links_in_code(doc) - if sanitize || block_given? + doc = Nokogiri::HTML::DocumentFragment.parse(data) doc = sanitize.clean_node!(doc) if sanitize yield doc if block_given? data = doc.to_html @@ -156,11 +154,29 @@ module Gollum # Returns the marked up String data. def process_tags(data) @tagmap.each do |id, tag| - data.gsub!(id, process_tag(tag)) + # If it's preformatted, just put the tag back + if is_preformatted?(data, id) + data.gsub!(id, "[[#{tag}]]") + else + data.gsub!(id, process_tag(tag)) + end end data end + # Find `id` within `data` and determine if it's within + # preformatted tags. + # + # data - The String data (with placeholders). + # id - The String SHA1 hash. + PREFORMATTED_TAGS = %w(code tt) + def is_preformatted?(data, id) + doc = Nokogiri::HTML::DocumentFragment.parse(data) + node = doc.search("[text()*='#{id}']").first + node && (PREFORMATTED_TAGS.include?(node.name) || + node.ancestors.any? { |a| PREFORMATTED_TAGS.include?(a.name) }) + end + # Process a single tag into its final HTML form. # # tag - The String tag contents (the stuff inside the double @@ -356,15 +372,6 @@ module Gollum end end - def remove_links_in_code(doc) - doc.css('code > a').each do |link| - link.before("[[#{link.content}]]") - link.remove - end - - doc - end - ######################################################################### # # Code diff --git a/test/test_markup.rb b/test/test_markup.rb index 2a2b2d0c..b14e597e 100644 --- a/test/test_markup.rb +++ b/test/test_markup.rb @@ -189,6 +189,12 @@ context "Markup" do assert_equal "
sed -i '' 's/[[:space:]]*$//'\n
", page.formatted_data end + test "piped wiki link within code block" do + @wiki.write_page("Potato", :markdown, "`make a link [[home|sweet home]]`", commit_details) + page = @wiki.page("Potato") + assert_equal "

make a link [[home|sweet home]]

", page.formatted_data + end + ######################################################################### # # Images