#highlight_failed in PygmentsCode

This commit is contained in:
Parker Moore 2013-03-10 01:44:32 +01:00
parent 8f7106c39b
commit 66e7fb0bfb

View File

@ -16,17 +16,9 @@ module HighlightCode
include TemplateWrapper
include SiteConfig
def pygments(code, lang)
begin
highlighted_code = Pygments.highlight(code, :lexer => lang, :formatter => 'html', :options => {:encoding => 'utf-8'})
highlighted_code = highlighted_code.gsub(/{{/, '{{').gsub(/{%/, '{%')
highlighted_code.to_s
rescue
fail_message = "Pygments couldn't highlight your code in lang '#{lang}':"
fail_message += "\n\n#{code}"
raise ArgumentError, fail_message.red
end
rescue
puts $!,$@
end
def highlight(code, options = {})
@ -192,4 +184,14 @@ module HighlightCode
code
end
def highlight_failed(file, code, lang)
code_snippet = code.split("\n")[0..9].map{|l| ">> #{l}" }.join("\n")
fail_message = "Pygments doesn't know how to highlight your code in lang '#{lang}'."
fail_message += "\nFailing code from #{file}:" if file
fail_message += "\n\n#{code_snippet}"
fail_message += "\n>> ..." if code.split("\n").size > 10
$stderr.puts fail_message.red
raise ArgumentError
end
end