tweak code markup parser so that blocks without a language are just output in pre tags

This commit is contained in:
rick
2011-01-11 00:47:46 -08:00
parent fc84a4e989
commit 26624b70bd
2 changed files with 8 additions and 5 deletions
+6 -3
View File
@@ -347,7 +347,7 @@ module Gollum
#
# Returns the placeholder'd String data.
def extract_code(data)
data.gsub(/^``` ?(.+?)\r?\n(.+?)\r?\n```\r?$/m) do
data.gsub(/^``` ?([^\r\n]+)?\r?\n(.+?)\r?\n```\r?$/m) do
id = Digest::SHA1.hexdigest($2)
@codemap[id] = { :lang => $1, :code => $2 }
id
@@ -362,12 +362,15 @@ module Gollum
# Returns the marked up String data.
def process_code(data)
@codemap.each do |id, spec|
lang = spec[:lang]
code = spec[:code]
if code.lines.all? { |line| line =~ /\A\r?\n\Z/ || line =~ /^( |\t)/ }
code.gsub!(/^( |\t)/m, '')
end
data.gsub!(id, Gollum::Albino.new(code, lang).colorize)
if lang = spec[:lang]
data.gsub!(id, Gollum::Albino.new(code, lang).colorize)
else
data.gsub!(id, "<pre><code>#{CGI.escapeHTML(code)}</code></pre>")
end
end
data
end
+2 -2
View File
@@ -360,8 +360,8 @@ context "Markup" do
end
test "code block with no lang" do
content = "a\n\n```\n\tls -al;\n\tbooya\n```\n\nb"
output = "<p>a</p>\n\n\n\n<p>b</p>"
content = "a\n\n```\n\tls -al;\n\t<booya>\n```\n\nb"
output = "<p>a</p>\n\n<pre><code>ls -al;\n&lt;booya&gt;</code></pre>\n\n<p>b</p>"
compare(content, output)
end