From ecb44b50e13ad3f61d3737e697a308bd65b6205d Mon Sep 17 00:00:00 2001 From: rick Date: Thu, 3 Mar 2011 16:28:13 -0800 Subject: [PATCH] use Albino::Multi --- lib/gollum/albino.rb | 17 +++++++++++------ lib/gollum/markup.rb | 45 ++++++++++++++++++++++++++++---------------- test/test_markup.rb | 12 ------------ 3 files changed, 40 insertions(+), 34 deletions(-) diff --git a/lib/gollum/albino.rb b/lib/gollum/albino.rb index b731ae9d..db7aa73a 100644 --- a/lib/gollum/albino.rb +++ b/lib/gollum/albino.rb @@ -1,12 +1,17 @@ -require 'albino' +require 'albino/multi' -class Gollum::Albino < Albino - self.bin = ::Albino.bin - self.default_encoding = ::Albino.default_encoding +class Gollum::Albino < Albino::Multi + self.bin = ::Albino::Multi.bin def colorize(options = {}) - html = super.to_s + case out = super + when Array then out.each { |s| fix_html(s) } + else fix_html(out) + end + end + + def fix_html(html) html.sub!(%r{\Z}, "\n") html end -end \ No newline at end of file +end diff --git a/lib/gollum/markup.rb b/lib/gollum/markup.rb index 2c52d1f2..3bd60ebf 100644 --- a/lib/gollum/markup.rb +++ b/lib/gollum/markup.rb @@ -380,26 +380,39 @@ module Gollum # # Returns the marked up String data. def process_code(data) + return data if data.nil? || data.size.zero? || @codemap.size.zero? + + blocks = [] @codemap.each do |id, spec| - formatted = spec[:output] || begin - code = spec[:code] - lang = spec[:lang] + next if spec[:output] # cached - if code.lines.all? { |line| line =~ /\A\r?\n\Z/ || line =~ /^( |\t)/ } - code.gsub!(/^( |\t)/m, '') - end - - formatted = begin - lang && Gollum::Albino.colorize(code, lang) - rescue ::Albino::ShellArgumentError, ::POSIX::Spawn::TimeoutExceeded, - ::POSIX::Spawn::MaximumOutputExceeded - end - formatted ||= "
#{CGI.escapeHTML(code)}
" - update_cache(:code, id, formatted) - formatted + code = spec[:code] + if code.lines.all? { |line| line =~ /\A\r?\n\Z/ || line =~ /^( |\t)/ } + code.gsub!(/^( |\t)/m, '') end - data.gsub!(id, formatted) + + blocks << [spec[:lang], code] end + + highlighted = begin + blocks.size.zero? ? [] : Gollum::Albino.colorize(blocks) + rescue ::Albino::ShellArgumentError, ::POSIX::Spawn::TimeoutExceeded, + ::POSIX::Spawn::MaximumOutputExceeded + [] + end + + @codemap.each do |id, spec| + body = spec[:output] || begin + if (body = highlighted.shift.to_s).size > 0 + update_cache(:code, id, body) + body + else + "
#{CGI.escapeHTML(spec[:code])}
" + end + end + data.gsub!(id, body) + end + data end diff --git a/test/test_markup.rb b/test/test_markup.rb index 6c8db408..14bb4e71 100644 --- a/test/test_markup.rb +++ b/test/test_markup.rb @@ -422,18 +422,6 @@ context "Markup" do compare(content, output) end - test "code block with invalid lang" do - content = "a\n\n``` ls -al;\n\tbooya\n\tboom\n```\n\nb" - output = "

a

\n\n
booya\nboom
\n\n

b

" - compare(content, output) - end - - test "code block with no lang" do - 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 - ######################################################################### # # Various