From bfeeecea71413cd8740db06bdb89a33f8ceaa0d7 Mon Sep 17 00:00:00 2001 From: Brandon Mathis Date: Sun, 10 Mar 2013 00:03:30 -0600 Subject: [PATCH] Improved error formatting and valid syntax directions. --- plugins/backtick_code_block.rb | 5 +++-- plugins/code_block.rb | 4 +++- plugins/gist_tag.rb | 4 +++- plugins/include_code.rb | 4 +++- plugins/pygments_code.rb | 17 ++++++----------- 5 files changed, 18 insertions(+), 16 deletions(-) diff --git a/plugins/backtick_code_block.rb b/plugins/backtick_code_block.rb index 0d8c5cc..c1e6a84 100644 --- a/plugins/backtick_code_block.rb +++ b/plugins/backtick_code_block.rb @@ -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 diff --git a/plugins/code_block.rb b/plugins/code_block.rb index 21c7bd4..6ad801e 100644 --- a/plugins/code_block.rb +++ b/plugins/code_block.rb @@ -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 diff --git a/plugins/gist_tag.rb b/plugins/gist_tag.rb index 8141b0d..981f7be 100644 --- a/plugins/gist_tag.rb +++ b/plugins/gist_tag.rb @@ -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 diff --git a/plugins/include_code.rb b/plugins/include_code.rb index b44240c..b9dde9e 100644 --- a/plugins/include_code.rb +++ b/plugins/include_code.rb @@ -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 diff --git a/plugins/pygments_code.rb b/plugins/pygments_code.rb index f2c1e10..d0fe907 100644 --- a/plugins/pygments_code.rb +++ b/plugins/pygments_code.rb @@ -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