mirror of
https://github.com/moparisthebest/android.moparisthebest.org
synced 2024-11-16 06:05:00 -05:00
Merge pull request #1103 from imathis/pygments-loud-fail
Fail loudly when a pygments highlight fails
This commit is contained in:
commit
a48153f99b
1
.gitattributes
vendored
Normal file
1
.gitattributes
vendored
Normal file
@ -0,0 +1 @@
|
||||
* text=auto
|
@ -10,7 +10,7 @@ module BacktickCodeBlock
|
||||
input.gsub /^`{3}(.+?)`{3}/m do
|
||||
str = $1.to_s
|
||||
str.gsub /([^\n]+)?\n(.+?)\Z/m do
|
||||
markup = $1 || ''
|
||||
markup = original_markup = $1 || ''
|
||||
code = $2.to_s
|
||||
|
||||
opts = parse_markup(markup)
|
||||
@ -37,7 +37,13 @@ module BacktickCodeBlock
|
||||
else
|
||||
@options[:lang] ||= 'plain'
|
||||
end
|
||||
highlight(code, @options)
|
||||
|
||||
begin
|
||||
highlight(code, @options)
|
||||
rescue MentosError => e
|
||||
markup = "```#{original_markup}"
|
||||
highlight_failed(e, "```[language] [title] [url] [link text] [linenos:false] [start:#] [mark:#,#-#]\ncode\n```", markup, code)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -51,6 +51,7 @@ module Jekyll
|
||||
TitleUrlLinkText = /(\S[\S\s]*)\s+(https?:\/\/\S+|\/\S+)\s*(.+)?/i
|
||||
Title = /(\S[\S\s]*)/
|
||||
def initialize(tag_name, markup, tokens)
|
||||
@original_markup = markup
|
||||
opts = parse_markup(markup)
|
||||
@options = {
|
||||
lang: opts[:lang],
|
||||
@ -78,11 +79,16 @@ module Jekyll
|
||||
end
|
||||
|
||||
def render(context)
|
||||
code = super.strip
|
||||
code = highlight(code, @options)
|
||||
code = context['pygments_prefix'] + code if context['pygments_prefix']
|
||||
code = code + context['pygments_suffix'] if context['pygments_suffix']
|
||||
code
|
||||
begin
|
||||
code = super.strip
|
||||
code = highlight(code, @options)
|
||||
code = context['pygments_prefix'] + code if context['pygments_prefix']
|
||||
code = code + context['pygments_suffix'] if context['pygments_suffix']
|
||||
code
|
||||
rescue MentosError => e
|
||||
markup = "{% codeblock #{@original_markup} %}"
|
||||
highlight_failed(e, "{% codeblock [lang:language] [title] [url] [link text] [start:#] [mark:#,#-#] [linenos:false] %}\ncode\n{% endcodeblock %}", markup, code)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -19,6 +19,7 @@ module Jekyll
|
||||
def initialize(tag_name, markup, token)
|
||||
super
|
||||
@cache_disabled = false
|
||||
@original_markup = markup
|
||||
@cache_folder = File.expand_path "../.gist-cache", File.dirname(__FILE__)
|
||||
|
||||
opts = parse_markup(markup)
|
||||
@ -53,7 +54,12 @@ module Jekyll
|
||||
unless cache
|
||||
code = get_gist_from_web(gist, file)
|
||||
code = get_range(code, @options[:start], @options[:end])
|
||||
code = highlight(code, @options)
|
||||
begin
|
||||
code = highlight(code, @options)
|
||||
rescue MentosError => e
|
||||
markup = "{% gist #{@original_markup} %}"
|
||||
highlight_failed(e, "{% gist gist_id [filename] [lang:language] [title:title] [start:#] [end:#] [range:#-#] [mark:#,#-#] [linenos:false] %}", markup, code, file)
|
||||
end
|
||||
end
|
||||
code || cache
|
||||
else
|
||||
|
@ -30,6 +30,7 @@ module Jekyll
|
||||
def initialize(tag_name, markup, tokens)
|
||||
@file = nil
|
||||
@title_old = nil
|
||||
@original_markup = markup
|
||||
|
||||
opts = parse_markup(markup)
|
||||
@options = {
|
||||
@ -85,7 +86,12 @@ module Jekyll
|
||||
|
||||
code = filepath.read
|
||||
code = get_range(code, @options[:start], @options[:end])
|
||||
highlight(code, @options)
|
||||
begin
|
||||
highlight(code, @options)
|
||||
rescue MentosError => e
|
||||
markup = "{% include_code #{@original_markup} %}"
|
||||
highlight_failed(e, "{% include_code [title] [lang:language] path/to/file [start:#] [end:#] [range:#-#] [mark:#,#-#] [linenos:false] %}", markup, code, filepath)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -7,6 +7,7 @@ begin # Make it easy for folks to use rubypython if they like
|
||||
require 'rubypython'
|
||||
rescue LoadError # rubypython is not installed
|
||||
end
|
||||
require File.expand_path('../../lib/colors.rb', __FILE__)
|
||||
|
||||
PYGMENTS_CACHE_DIR = File.expand_path('../../.pygments-cache', __FILE__)
|
||||
FileUtils.mkdir_p(PYGMENTS_CACHE_DIR)
|
||||
@ -18,8 +19,6 @@ module HighlightCode
|
||||
highlighted_code = Pygments.highlight(code, :lexer => lang, :formatter => 'html', :options => {:encoding => 'utf-8'})
|
||||
highlighted_code = highlighted_code.gsub(/{{/, '{{').gsub(/{%/, '{%')
|
||||
highlighted_code.to_s
|
||||
rescue
|
||||
puts $!,$@
|
||||
end
|
||||
|
||||
def highlight(code, options = {})
|
||||
@ -185,4 +184,15 @@ module HighlightCode
|
||||
code
|
||||
end
|
||||
|
||||
def highlight_failed(error, syntax, markup, code, file = nil)
|
||||
code_snippet = code.split("\n")[0..9].map{|l| " #{l}" }.join("\n")
|
||||
fail_message = "\nPygments Error while parsing the following markup#{" in #{file}" if file}:\n\n".red
|
||||
fail_message += " #{markup}\n#{code_snippet}\n"
|
||||
fail_message += "#{" ..." if code.split("\n").size > 10}\n"
|
||||
fail_message += "\nValid Syntax:\n\n#{syntax}\n".yellow
|
||||
fail_message += "\nPygments Error:\n\n#{error.message}".red
|
||||
$stderr.puts fail_message.chomp
|
||||
raise ArgumentError
|
||||
end
|
||||
|
||||
end
|
||||
|
Loading…
Reference in New Issue
Block a user