Improved error formatting and valid syntax directions.

This commit is contained in:
Brandon Mathis 2013-03-10 00:03:30 -06:00
parent 0efbec7daa
commit bfeeecea71
5 changed files with 18 additions and 16 deletions

View File

@ -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)
@ -41,7 +41,8 @@ module BacktickCodeBlock
begin
highlight(code, @options)
rescue
highlight_failed('backtick_code_block', "``` [lang]\n code\n ```", code, @options[:lang])
markup = "```#{original_markup}"
highlight_failed("```[language] [title] [url] [link text] [linenos:false] [start:#] [mark:#,#-#]\ncode\n```", markup, code)
end
end
end

View File

@ -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],
@ -85,7 +86,8 @@ module Jekyll
code = code + context['pygments_suffix'] if context['pygments_suffix']
code
rescue
highlight_failed('code_block', 'codeblock [title] [url] [link text]', code, @options[:lang])
markup = "{% codeblock #{@original_markup} %}"
highlight_failed("{% codeblock [lang:language] [title] [url] [link text] [start:#] [mark:#,#-#] [linenos:false] %}\ncode\n{% endcodeblock %}", markup, code)
end
end
end

View File

@ -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)
@ -56,7 +57,8 @@ module Jekyll
begin
code = highlight(code, @options)
rescue
highlight_failed('gist', 'gist gist_id [filename]', code, @options[:lang], file)
markup = "{% gist #{@original_markup} %}"
highlight_failed("{% gist gist_id [filename] %}", markup, code, file)
end
end
code || cache

View File

@ -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 = {
@ -88,7 +89,8 @@ module Jekyll
begin
highlight(code, @options)
rescue
highlight_failed('include_code', 'include_code path/to/file [title]', code, @options[:lang], filepath)
markup = "{% include_code #{@original_markup} %}"
highlight_failed("{% include_code [title] [lang:language] path/to/file [start:#] [end:#] [range:#-#] [mark:#,#-#] [linenos:false] %}", markup, code, filepath)
end
end
end

View File

@ -184,18 +184,13 @@ module HighlightCode
code
end
def highlight_failed(tag, syntax, code, lang, file = nil)
def highlight_failed(syntax, markup, code, file = nil)
code_snippet = code.split("\n")[0..9].map{|l| " #{l}" }.join("\n")
fail_message = <<-MESSAGE
Syntax Error in tag '#{tag}' while parsing the following markup in lang '#{lang}':
#{"(Failing code from #{file})" if file}
#{code_snippet}
#{" ..." if code.split("\n").size > 10}
Valid syntax: #{syntax}
MESSAGE
$stderr.puts fail_message.chomp.red
fail_message = "\nSyntax 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
$stderr.puts fail_message.chomp
raise ArgumentError
end