From c5e4935e85d820c01267a6e8e333232d7add4288 Mon Sep 17 00:00:00 2001 From: rick Date: Tue, 11 Jan 2011 00:47:46 -0800 Subject: [PATCH] tweak code markup parser so that blocks without a language are just output in pre tags --- lib/gollum/markup.rb | 9 ++++++--- test/test_markup.rb | 4 ++-- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/lib/gollum/markup.rb b/lib/gollum/markup.rb index b57e5adf..7a521ed8 100644 --- a/lib/gollum/markup.rb +++ b/lib/gollum/markup.rb @@ -359,7 +359,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) cached = check_cache(:code, id) @codemap[id] = cached ? @@ -379,12 +379,15 @@ module Gollum def process_code(data) @codemap.each do |id, spec| formatted = spec[:output] || begin - lang = spec[:lang] code = spec[:code] if code.lines.all? { |line| line =~ /\A\r?\n\Z/ || line =~ /^( |\t)/ } code.gsub!(/^( |\t)/m, '') end - formatted = Gollum::Albino.new(code, lang).colorize + formatted = if lang = spec[:lang] + Gollum::Albino.new(code, lang).colorize + else + "
#{CGI.escapeHTML(code)}
" + end update_cache(:code, id, formatted) formatted end diff --git a/test/test_markup.rb b/test/test_markup.rb index 83f71e2f..7d729aba 100644 --- a/test/test_markup.rb +++ b/test/test_markup.rb @@ -394,8 +394,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 = "

a

\n\n\n\n

b

" + content = "a\n\n```\n\tls -al;\n\t\n```\n\nb" + output = "

a

\n\n
ls -al;\n<booya>
\n\n

b

" compare(content, output) end