Skip to content

Commit 712afd9

Browse files
committed
Fix a bug with syntax highlighting
We need to refer to a lexer by alias, even if the lookup matched on filename. Fixes brightcove-archive#466.
1 parent 0e050ba commit 712afd9

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

‎lib/syntax_highlighter.rb‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
require "bundler/setup"
22
require "pathological"
33

4-
require "pygments"
54
require "lib/logging"
65
require "lib/ruby_extensions"
6+
require "methodchain"
7+
require "pygments"
78

89
class SyntaxHighlighter
910
WEEK = 60*60*24*7
@@ -36,7 +37,7 @@ def colorize_blob(repo_name, file_type, blob)
3637
end
3738

3839
def self.pygmentize(file_type, text)
39-
file_type = "text" if Pygments::Lexer.find(file_type).nil?
40+
file_type = Pygments::Lexer.find(file_type).then { |lexer| lexer[:aliases][0] rescue nil } || "text"
4041
Pygments.highlight(text, :lexer => file_type, :options => {
4142
:encoding => "utf-8", :nowrap => true, :stripnl => false, :stripall => false
4243
})
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
require File.expand_path(File.join(File.dirname(__FILE__), "../unit_test_helper.rb"))
2+
require "lib/syntax_highlighter.rb"
3+
4+
class SyntaxHighlighterTest < Scope::TestCase
5+
context "pygmentize" do
6+
should "handle a language by alias" do
7+
highlighted = SyntaxHighlighter.pygmentize("ruby", "foo")
8+
assert_match /span/, highlighted
9+
end
10+
11+
should "handle a language by filename" do
12+
highlighted = SyntaxHighlighter.pygmentize("lisp", "foo")
13+
assert_match /span/, highlighted
14+
end
15+
16+
should "return plaintext for an unrecognized language" do
17+
highlighted = SyntaxHighlighter.pygmentize("foobar", "foo")
18+
assert_equal "foo", highlighted
19+
end
20+
end
21+
end

0 commit comments

Comments
 (0)