Skip to content
Browse files

use Albino::Multi

  • Loading branch information...
1 parent 83c0dba commit ecb44b50e13ad3f61d3737e697a308bd65b6205d @technoweenie technoweenie committed Mar 3, 2011
Showing with 38 additions and 32 deletions.
  1. +11 −6 lib/gollum/albino.rb
  2. +27 −14 lib/gollum/markup.rb
  3. +0 −12 test/test_markup.rb
View
17 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{</pre></div>\Z}, "</pre>\n</div>")
html
end
-end
+end
View
41 lib/gollum/markup.rb
@@ -380,26 +380,39 @@ def extract_code(data)
#
# 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
+ code = spec[:code]
+ if code.lines.all? { |line| line =~ /\A\r?\n\Z/ || line =~ /^( |\t)/ }
+ code.gsub!(/^( |\t)/m, '')
+ end
+
+ blocks << [spec[:lang], code]
+ end
- formatted = begin
- lang && Gollum::Albino.colorize(code, lang)
- rescue ::Albino::ShellArgumentError, ::POSIX::Spawn::TimeoutExceeded,
- ::POSIX::Spawn::MaximumOutputExceeded
+ 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
+ "<pre><code>#{CGI.escapeHTML(spec[:code])}</code></pre>"
end
- formatted ||= "<pre><code>#{CGI.escapeHTML(code)}</code></pre>"
- update_cache(:code, id, formatted)
- formatted
end
- data.gsub!(id, formatted)
+ data.gsub!(id, body)
end
+
data
end
View
12 test/test_markup.rb
@@ -422,18 +422,6 @@
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 = "<p>a</p>\n\n<pre><code>booya\nboom</code></pre>\n\n<p>b</p>"
- compare(content, output)
- end
-
- test "code block with no lang" do
- 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
-
#########################################################################
#
# Various

0 comments on commit ecb44b5

Please sign in to comment.
Something went wrong with that request. Please try again.