From 1306ad83b9f4ba22c609e900e3939630a22398b0 Mon Sep 17 00:00:00 2001 From: Parker Moore Date: Wed, 6 Mar 2013 01:09:40 +0100 Subject: [PATCH 01/10] Fail loudly when a pygments highlight fails. #990. --- plugins/pygments_code.rb | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/plugins/pygments_code.rb b/plugins/pygments_code.rb index 0ec5872..0744b37 100644 --- a/plugins/pygments_code.rb +++ b/plugins/pygments_code.rb @@ -15,9 +15,15 @@ module HighlightCode include TemplateWrapper include SiteConfig def pygments(code, lang) - highlighted_code = Pygments.highlight(code, :lexer => lang, :formatter => 'html', :options => {:encoding => 'utf-8'}) - highlighted_code = highlighted_code.gsub(/{{/, '{{').gsub(/{%/, '{%') - highlighted_code.to_s + 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}\n" + raise ArgumentError, fail_message + end rescue puts $!,$@ end From e6faef62578789fe73edab63133a13f46c5313be Mon Sep 17 00:00:00 2001 From: Parker Moore Date: Wed, 6 Mar 2013 01:29:24 +0100 Subject: [PATCH 02/10] Added .gitattributes file to keep CRLF's out of the codebase. --- .gitattributes | 1 + 1 file changed, 1 insertion(+) create mode 100644 .gitattributes diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..176a458 --- /dev/null +++ b/.gitattributes @@ -0,0 +1 @@ +* text=auto From 8f7106c39bc39710721433762d7b42164d10c403 Mon Sep 17 00:00:00 2001 From: Parker Moore Date: Sun, 10 Mar 2013 00:24:52 +0100 Subject: [PATCH 03/10] Colorizing fail message in Pygments code highlighting. --- plugins/pygments_code.rb | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/plugins/pygments_code.rb b/plugins/pygments_code.rb index 0744b37..3c18aad 100644 --- a/plugins/pygments_code.rb +++ b/plugins/pygments_code.rb @@ -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) @@ -21,8 +22,8 @@ module HighlightCode highlighted_code.to_s rescue fail_message = "Pygments couldn't highlight your code in lang '#{lang}':" - fail_message += "\n\n#{code}\n" - raise ArgumentError, fail_message + fail_message += "\n\n#{code}" + raise ArgumentError, fail_message.red end rescue puts $!,$@ From 66e7fb0bfbf4c4a0392004321d11ce4aa1e151fc Mon Sep 17 00:00:00 2001 From: Parker Moore Date: Sun, 10 Mar 2013 01:44:32 +0100 Subject: [PATCH 04/10] #highlight_failed in PygmentsCode --- plugins/pygments_code.rb | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/plugins/pygments_code.rb b/plugins/pygments_code.rb index 3c18aad..4b0aecb 100644 --- a/plugins/pygments_code.rb +++ b/plugins/pygments_code.rb @@ -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 From 6c5852953204cbead9c3a3df559b9ac465ff14ee Mon Sep 17 00:00:00 2001 From: Parker Moore Date: Sun, 10 Mar 2013 01:45:05 +0100 Subject: [PATCH 05/10] Rescuing errors issued by Pygments and raising our own with helpful info --- plugins/backtick_code_block.rb | 7 ++++++- plugins/code_block.rb | 14 +++++++++----- plugins/gist_tag.rb | 6 +++++- plugins/include_code.rb | 6 +++++- 4 files changed, 25 insertions(+), 8 deletions(-) diff --git a/plugins/backtick_code_block.rb b/plugins/backtick_code_block.rb index 301d573..957ca8a 100644 --- a/plugins/backtick_code_block.rb +++ b/plugins/backtick_code_block.rb @@ -37,7 +37,12 @@ module BacktickCodeBlock else @options[:lang] ||= 'plain' end - highlight(code, @options) + + begin + highlight(code, @options) + rescue + highlight_failed(nil, code, @options[:lang]) + end end end end diff --git a/plugins/code_block.rb b/plugins/code_block.rb index 4ac596d..94feba2 100644 --- a/plugins/code_block.rb +++ b/plugins/code_block.rb @@ -78,11 +78,15 @@ 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 + highlight_failed(nil, code, @options[:lang]) + end end end end diff --git a/plugins/gist_tag.rb b/plugins/gist_tag.rb index 483d1c8..50e9b17 100644 --- a/plugins/gist_tag.rb +++ b/plugins/gist_tag.rb @@ -53,7 +53,11 @@ 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 + highlight_failed(file, code, @options[:lang]) + end end code || cache else diff --git a/plugins/include_code.rb b/plugins/include_code.rb index 9ac2104..39b51e0 100644 --- a/plugins/include_code.rb +++ b/plugins/include_code.rb @@ -85,7 +85,11 @@ module Jekyll code = filepath.read code = get_range(code, @options[:start], @options[:end]) - highlight(code, @options) + begin + highlight(code, @options) + rescue + highlight_failed(@file, code, @options[:lang]) + end end end end From b456865c8ce5ddd59670974d646587822ac5deda Mon Sep 17 00:00:00 2001 From: Parker Moore Date: Sun, 10 Mar 2013 02:06:02 +0100 Subject: [PATCH 06/10] #highlight_failed now accepts tag name, and proper syntax --- plugins/backtick_code_block.rb | 2 +- plugins/code_block.rb | 2 +- plugins/gist_tag.rb | 2 +- plugins/include_code.rb | 2 +- plugins/pygments_code.rb | 19 ++++++++++++------- source/downloads/code/lolz.fsh | 1 + 6 files changed, 17 insertions(+), 11 deletions(-) create mode 100644 source/downloads/code/lolz.fsh diff --git a/plugins/backtick_code_block.rb b/plugins/backtick_code_block.rb index 957ca8a..0d8c5cc 100644 --- a/plugins/backtick_code_block.rb +++ b/plugins/backtick_code_block.rb @@ -41,7 +41,7 @@ module BacktickCodeBlock begin highlight(code, @options) rescue - highlight_failed(nil, code, @options[:lang]) + highlight_failed('backtick_code_block', "``` [lang]\n code\n ```", code, @options[:lang]) end end end diff --git a/plugins/code_block.rb b/plugins/code_block.rb index 94feba2..21c7bd4 100644 --- a/plugins/code_block.rb +++ b/plugins/code_block.rb @@ -85,7 +85,7 @@ module Jekyll code = code + context['pygments_suffix'] if context['pygments_suffix'] code rescue - highlight_failed(nil, code, @options[:lang]) + highlight_failed('code_block', 'codeblock [title] [url] [link text]', code, @options[:lang]) end end end diff --git a/plugins/gist_tag.rb b/plugins/gist_tag.rb index 50e9b17..8141b0d 100644 --- a/plugins/gist_tag.rb +++ b/plugins/gist_tag.rb @@ -56,7 +56,7 @@ module Jekyll begin code = highlight(code, @options) rescue - highlight_failed(file, code, @options[:lang]) + highlight_failed('gist', 'gist gist_id [filename]', code, @options[:lang], file) end end code || cache diff --git a/plugins/include_code.rb b/plugins/include_code.rb index 39b51e0..b44240c 100644 --- a/plugins/include_code.rb +++ b/plugins/include_code.rb @@ -88,7 +88,7 @@ module Jekyll begin highlight(code, @options) rescue - highlight_failed(@file, code, @options[:lang]) + highlight_failed('include_code', 'include_code path/to/file [title]', code, @options[:lang], filepath) end end end diff --git a/plugins/pygments_code.rb b/plugins/pygments_code.rb index 4b0aecb..f2c1e10 100644 --- a/plugins/pygments_code.rb +++ b/plugins/pygments_code.rb @@ -184,13 +184,18 @@ 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 + def highlight_failed(tag, syntax, code, lang, 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 raise ArgumentError end diff --git a/source/downloads/code/lolz.fsh b/source/downloads/code/lolz.fsh new file mode 100644 index 0000000..4d190cb --- /dev/null +++ b/source/downloads/code/lolz.fsh @@ -0,0 +1 @@ +Hi hi hi lolz From 0efbec7daa24365104df050c892f2447d31b24ee Mon Sep 17 00:00:00 2001 From: Parker Moore Date: Sun, 10 Mar 2013 02:28:37 +0100 Subject: [PATCH 07/10] Remove stray test file. --- source/downloads/code/lolz.fsh | 1 - 1 file changed, 1 deletion(-) delete mode 100644 source/downloads/code/lolz.fsh diff --git a/source/downloads/code/lolz.fsh b/source/downloads/code/lolz.fsh deleted file mode 100644 index 4d190cb..0000000 --- a/source/downloads/code/lolz.fsh +++ /dev/null @@ -1 +0,0 @@ -Hi hi hi lolz From bfeeecea71413cd8740db06bdb89a33f8ceaa0d7 Mon Sep 17 00:00:00 2001 From: Brandon Mathis Date: Sun, 10 Mar 2013 00:03:30 -0600 Subject: [PATCH 08/10] 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 From d0e59172f3b4ec036948a4649f8aa82c5c16e453 Mon Sep 17 00:00:00 2001 From: Brandon Mathis Date: Sun, 10 Mar 2013 00:17:55 -0600 Subject: [PATCH 09/10] Updated error message for gist syntax --- plugins/gist_tag.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/gist_tag.rb b/plugins/gist_tag.rb index 981f7be..553014a 100644 --- a/plugins/gist_tag.rb +++ b/plugins/gist_tag.rb @@ -58,7 +58,7 @@ module Jekyll code = highlight(code, @options) rescue markup = "{% gist #{@original_markup} %}" - highlight_failed("{% gist gist_id [filename] %}", markup, code, file) + highlight_failed("{% gist gist_id [filename] [lang:language] [title:title] [start:#] [end:#] [range:#-#] [mark:#,#-#] [linenos:false] %}", markup, code, file) end end code || cache From d52bf53cc79337b9ce766c0eb306729a7e52c290 Mon Sep 17 00:00:00 2001 From: Parker Moore Date: Sun, 10 Mar 2013 21:47:07 +0100 Subject: [PATCH 10/10] Pygments error messages are now printed alongside ours. --- plugins/backtick_code_block.rb | 4 ++-- plugins/code_block.rb | 4 ++-- plugins/gist_tag.rb | 4 ++-- plugins/include_code.rb | 4 ++-- plugins/pygments_code.rb | 7 ++++--- 5 files changed, 12 insertions(+), 11 deletions(-) diff --git a/plugins/backtick_code_block.rb b/plugins/backtick_code_block.rb index c1e6a84..3f5db1d 100644 --- a/plugins/backtick_code_block.rb +++ b/plugins/backtick_code_block.rb @@ -40,9 +40,9 @@ module BacktickCodeBlock begin highlight(code, @options) - rescue + rescue MentosError => e markup = "```#{original_markup}" - highlight_failed("```[language] [title] [url] [link text] [linenos:false] [start:#] [mark:#,#-#]\ncode\n```", markup, code) + highlight_failed(e, "```[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 6ad801e..90b0d0e 100644 --- a/plugins/code_block.rb +++ b/plugins/code_block.rb @@ -85,9 +85,9 @@ module Jekyll code = context['pygments_prefix'] + code if context['pygments_prefix'] code = code + context['pygments_suffix'] if context['pygments_suffix'] code - rescue + rescue MentosError => e markup = "{% codeblock #{@original_markup} %}" - highlight_failed("{% codeblock [lang:language] [title] [url] [link text] [start:#] [mark:#,#-#] [linenos:false] %}\ncode\n{% endcodeblock %}", markup, code) + highlight_failed(e, "{% 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 553014a..331cc76 100644 --- a/plugins/gist_tag.rb +++ b/plugins/gist_tag.rb @@ -56,9 +56,9 @@ module Jekyll code = get_range(code, @options[:start], @options[:end]) begin code = highlight(code, @options) - rescue + rescue MentosError => e markup = "{% gist #{@original_markup} %}" - highlight_failed("{% gist gist_id [filename] [lang:language] [title:title] [start:#] [end:#] [range:#-#] [mark:#,#-#] [linenos:false] %}", markup, code, file) + highlight_failed(e, "{% gist gist_id [filename] [lang:language] [title:title] [start:#] [end:#] [range:#-#] [mark:#,#-#] [linenos:false] %}", markup, code, file) end end code || cache diff --git a/plugins/include_code.rb b/plugins/include_code.rb index b9dde9e..6315cf7 100644 --- a/plugins/include_code.rb +++ b/plugins/include_code.rb @@ -88,9 +88,9 @@ module Jekyll code = get_range(code, @options[:start], @options[:end]) begin highlight(code, @options) - rescue + rescue MentosError => e markup = "{% include_code #{@original_markup} %}" - highlight_failed("{% include_code [title] [lang:language] path/to/file [start:#] [end:#] [range:#-#] [mark:#,#-#] [linenos:false] %}", markup, code, filepath) + highlight_failed(e, "{% 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 d0fe907..b06b153 100644 --- a/plugins/pygments_code.rb +++ b/plugins/pygments_code.rb @@ -184,12 +184,13 @@ module HighlightCode code end - def highlight_failed(syntax, markup, code, file = nil) + def highlight_failed(error, syntax, markup, code, file = nil) code_snippet = code.split("\n")[0..9].map{|l| " #{l}" }.join("\n") - fail_message = "\nSyntax Error while parsing the following markup#{ " in #{file}" if file}:\n\n".red + 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 += "\nValid Syntax:\n\n#{syntax}\n".yellow + fail_message += "\nPygments Error:\n\n#{error.message}".red $stderr.puts fail_message.chomp raise ArgumentError end