diff options
author | Douwe Maan <douwe@selenight.nl> | 2016-07-16 04:22:35 +0300 |
---|---|---|
committer | Douwe Maan <douwe@selenight.nl> | 2016-07-16 04:22:35 +0300 |
commit | 82959349dd97450a166d42547083f8dfd3d1491e (patch) | |
tree | 39bb13a02f589c28e2fbbd3f732d7f2eba085116 /lib/banzai | |
parent | fbd75c35fd3fd43f6680b925959ac9498e9528a5 (diff) |
Don't fail to highlight when Rouge doesn't have a lexer
Diffstat (limited to 'lib/banzai')
-rw-r--r-- | lib/banzai/filter/syntax_highlight_filter.rb | 15 |
1 files changed, 8 insertions, 7 deletions
diff --git a/lib/banzai/filter/syntax_highlight_filter.rb b/lib/banzai/filter/syntax_highlight_filter.rb index 028edef704b..91f0159f9a1 100644 --- a/lib/banzai/filter/syntax_highlight_filter.rb +++ b/lib/banzai/filter/syntax_highlight_filter.rb @@ -19,21 +19,22 @@ module Banzai language = node.attr('class') code = node.text - lexer = Rouge::Lexer.find_fancy(language) + css_classes = "code highlight" + + lexer = Rouge::Lexer.find_fancy(language) || Rouge::Lexers::PlainText formatter = Rouge::Formatters::HTML.new - css_classes = "code highlight js-syntax-highlight #{lexer.tag}" begin - highlighted = '' - highlighted << %(<pre class="#{css_classes}"><code>) - highlighted << formatter.format(lexer.lex(code)) - highlighted << %(</code></pre>) + code = formatter.format(lexer.lex(code)) + + css_classes << " js-syntax-highlight #{lexer.tag}" rescue # Gracefully handle syntax highlighter bugs/errors to ensure # users can still access an issue/comment/etc. - highlighted = "<pre>#{code}</pre>" end + highlighted = %(<pre class="#{css_classes}"><code>#{code}</code></pre>) + # Extracted to a method to measure it replace_parent_pre_element(node, highlighted) end |