diff options
author | Robert Speicher <robert@gitlab.com> | 2016-07-16 01:56:05 +0000 |
---|---|---|
committer | Robert Speicher <robert@gitlab.com> | 2016-07-16 01:56:05 +0000 |
commit | 9b597fb5c1bb322416aadf9c1a41f26d7c08e10f (patch) | |
tree | ab820ba0ea8d7bd729af55401cf9bdf263885d2f /lib | |
parent | 4f25e3178b2eaa71f312e80ca183358b350c97d6 (diff) | |
parent | 82959349dd97450a166d42547083f8dfd3d1491e (diff) | |
download | gitlab-ce-9b597fb5c1bb322416aadf9c1a41f26d7c08e10f.tar.gz |
Merge branch 'syntax-highlight-unknown-language' into 'master'
Don't fail to highlight when Rouge doesn't have a lexer
Fixes issue introduced by upgrade to Rouge 2.0 (https://gitlab.com/gitlab-org/gitlab-ce/merge_requests/4691)
See merge request !5291
Diffstat (limited to 'lib')
-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 |