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 input.gsub /^`{3}(.+?)`{3}/m do
str = $1.to_s str = $1.to_s
str.gsub /([^\n]+)?\n(.+?)\Z/m do str.gsub /([^\n]+)?\n(.+?)\Z/m do
markup = $1 || '' markup = original_markup = $1 || ''
code = $2.to_s code = $2.to_s
opts = parse_markup(markup) opts = parse_markup(markup)
@ -41,7 +41,8 @@ module BacktickCodeBlock
begin begin
highlight(code, @options) highlight(code, @options)
rescue 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 end
end end

View File

@ -51,6 +51,7 @@ module Jekyll
TitleUrlLinkText = /(\S[\S\s]*)\s+(https?:\/\/\S+|\/\S+)\s*(.+)?/i TitleUrlLinkText = /(\S[\S\s]*)\s+(https?:\/\/\S+|\/\S+)\s*(.+)?/i
Title = /(\S[\S\s]*)/ Title = /(\S[\S\s]*)/
def initialize(tag_name, markup, tokens) def initialize(tag_name, markup, tokens)
@original_markup = markup
opts = parse_markup(markup) opts = parse_markup(markup)
@options = { @options = {
lang: opts[:lang], lang: opts[:lang],
@ -85,7 +86,8 @@ module Jekyll
code = code + context['pygments_suffix'] if context['pygments_suffix'] code = code + context['pygments_suffix'] if context['pygments_suffix']
code code
rescue 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 end
end end

View File

@ -19,6 +19,7 @@ module Jekyll
def initialize(tag_name, markup, token) def initialize(tag_name, markup, token)
super super
@cache_disabled = false @cache_disabled = false
@original_markup = markup
@cache_folder = File.expand_path "../.gist-cache", File.dirname(__FILE__) @cache_folder = File.expand_path "../.gist-cache", File.dirname(__FILE__)
opts = parse_markup(markup) opts = parse_markup(markup)
@ -56,7 +57,8 @@ module Jekyll
begin begin
code = highlight(code, @options) code = highlight(code, @options)
rescue 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
end end
code || cache code || cache

View File

@ -30,6 +30,7 @@ module Jekyll
def initialize(tag_name, markup, tokens) def initialize(tag_name, markup, tokens)
@file = nil @file = nil
@title_old = nil @title_old = nil
@original_markup = markup
opts = parse_markup(markup) opts = parse_markup(markup)
@options = { @options = {
@ -88,7 +89,8 @@ module Jekyll
begin begin
highlight(code, @options) highlight(code, @options)
rescue 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 end
end end

View File

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