mirror of
https://github.com/moparisthebest/android.moparisthebest.org
synced 2024-11-16 06:05:00 -05:00
#highlight_failed in PygmentsCode
This commit is contained in:
parent
8f7106c39b
commit
66e7fb0bfb
@ -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 $!,$@
|
||||
highlighted_code = Pygments.highlight(code, :lexer => lang, :formatter => 'html', :options => {:encoding => 'utf-8'})
|
||||
highlighted_code = highlighted_code.gsub(/{{/, '{{').gsub(/{%/, '{%')
|
||||
highlighted_code.to_s
|
||||
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
|
||||
|
Loading…
Reference in New Issue
Block a user