From f3cbbb865239b39769c531b5667a3d58e6579cde Mon Sep 17 00:00:00 2001
From: Tom Preston-Werner
Date: Wed, 7 Jul 2010 17:33:04 -0700
Subject: [PATCH] Handle stupid 0x0D bytes because browsers are stupid.
---
lib/gollum/markup.rb | 2 +-
lib/gollum/wiki.rb | 13 +++++++++++--
test/test_markup.rb | 15 +++++++++++++++
3 files changed, 27 insertions(+), 3 deletions(-)
diff --git a/lib/gollum/markup.rb b/lib/gollum/markup.rb
index e8802a70..ba2f73d8 100644
--- a/lib/gollum/markup.rb
+++ b/lib/gollum/markup.rb
@@ -232,7 +232,7 @@ module Gollum
#
# Returns the placeholder'd String data.
def extract_code(data)
- data.gsub(/^``` ?(.+)\n(.+)\n```$/m) do
+ data.gsub(/^``` ?(.+?)\r?\n(.+?)\r?\n```\r?$/m) do
id = Digest::SHA1.hexdigest($2)
@codemap[id] = { :lang => $1, :code => $2 }
id
diff --git a/lib/gollum/wiki.rb b/lib/gollum/wiki.rb
index 4423e189..2eb893e1 100644
--- a/lib/gollum/wiki.rb
+++ b/lib/gollum/wiki.rb
@@ -67,7 +67,7 @@ module Gollum
if pcommit = @repo.commit('master')
map = tree_map(pcommit.tree)
end
- map[path] = data
+ map[path] = normalize(data)
index = tree_map_to_index(map)
parents = pcommit ? [pcommit] : []
@@ -90,7 +90,7 @@ module Gollum
pcommit = @repo.commit('master')
map = tree_map(pcommit.tree)
index = tree_map_to_index(map)
- index.add(page.path, data)
+ index.add(page.path, normalize(data))
actor = Grit::Actor.new(commit[:name], commit[:email])
index.commit(commit[:message], [pcommit], actor)
@@ -160,6 +160,15 @@ module Gollum
# Returns the String path.
attr_reader :path
+ # Normalize the data.
+ #
+ # data - The String data to be normalized.
+ #
+ # Returns the normalized data String.
+ def normalize(data)
+ data.gsub(/\r/, '')
+ end
+
# Fill an array with a list of pages.
#
# tree - The Grit::Tree to start with.
diff --git a/test/test_markup.rb b/test/test_markup.rb
index e7c023e3..41b00e30 100644
--- a/test/test_markup.rb
+++ b/test/test_markup.rb
@@ -167,6 +167,21 @@ context "Markup" do
assert_equal output, rendered
end
+ test "code blocks with carriage returns" do
+ content = "a\r\n\r\n```ruby\r\nx = 1\r\n```\r\n\r\nb"
+ output = "a
\n\n
\n\nb
\n"
+
+ index = @wiki.repo.index
+ index.add("Bilbo-Baggins.md", content)
+ index.commit("Add alpha.jpg")
+
+ page = @wiki.page("Bilbo Baggins")
+ rendered = Gollum::Markup.new(page).render
+ assert_equal output, rendered
+ end
+
def relative_image(content, output)
index = @wiki.repo.index
index.add("greek/alpha.jpg", "hi")