From 82959349dd97450a166d42547083f8dfd3d1491e Mon Sep 17 00:00:00 2001 From: Douwe Maan Date: Fri, 15 Jul 2016 20:22:35 -0500 Subject: Don't fail to highlight when Rouge doesn't have a lexer --- lib/banzai/filter/syntax_highlight_filter.rb | 15 +++++----- .../banzai/filter/syntax_highlight_filter_spec.rb | 34 +++++++++++++++++----- 2 files changed, 35 insertions(+), 14 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 << %(
)
-          highlighted << formatter.format(lexer.lex(code))
-          highlighted << %(
) + 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 = "
#{code}
" end + highlighted = %(
#{code}
) + # Extracted to a method to measure it replace_parent_pre_element(node, highlighted) end diff --git a/spec/lib/banzai/filter/syntax_highlight_filter_spec.rb b/spec/lib/banzai/filter/syntax_highlight_filter_spec.rb index 48ebc81cf82..b1370bca833 100644 --- a/spec/lib/banzai/filter/syntax_highlight_filter_spec.rb +++ b/spec/lib/banzai/filter/syntax_highlight_filter_spec.rb @@ -3,15 +3,35 @@ require 'spec_helper' describe Banzai::Filter::SyntaxHighlightFilter, lib: true do include FilterSpecHelper - it 'highlights valid code blocks' do - result = filter('
def fun end')
-    expect(result.to_html).to eq("
def fun end
") + context "when no language is specified" do + it "highlights as plaintext" do + result = filter('
def fun end
') + expect(result.to_html).to eq('
def fun end
') + end end - it 'passes through invalid code blocks' do - allow_any_instance_of(Rouge::Formatter).to receive(:format).and_raise(StandardError) + context "when a valid language is specified" do + it "highlights as that language" do + result = filter('
def fun end
') + expect(result.to_html).to eq('
def fun end
') + end + end + + context "when an invalid language is specified" do + it "highlights as plaintext" do + result = filter('
This is a test
') + expect(result.to_html).to eq('
This is a test
') + end + end + + context "when Rouge formatting fails" do + before do + allow_any_instance_of(Rouge::Formatter).to receive(:format).and_raise(StandardError) + end - result = filter('
This is a test
') - expect(result.to_html).to eq('
This is a test
') + it "highlights as plaintext" do + result = filter('
This is a test
') + expect(result.to_html).to eq('
This is a test
') + end end end -- cgit v1.2.1