Commit 0fe36374d88005379001460a74dc4d6359a35d16
1 parent
b73a33ed
Exists in
master
and in
4 other branches
Better language detection
Showing
1 changed file
with
9 additions
and
29 deletions
Show diff stats
lib/utils.rb
| ... | ... | @@ -22,40 +22,20 @@ module Utils |
| 22 | 22 | end |
| 23 | 23 | |
| 24 | 24 | def system_colorize(data, file_name) |
| 25 | + options = { :encoding => 'utf-8', :linenos => 'True' } | |
| 26 | + | |
| 27 | + # Try detect language with pygments | |
| 28 | + Pygments.highlight data, :filename => file_name, :options => options | |
| 29 | + rescue | |
| 30 | + # if it fails use manual detection | |
| 25 | 31 | ft = handle_file_type(file_name) |
| 26 | - Pygments.highlight(data, :lexer => ft, :options => { :encoding => 'utf-8', :linenos => 'True' }) | |
| 32 | + Pygments.highlight(data, :lexer => ft, :options => options) | |
| 27 | 33 | end |
| 28 | 34 | |
| 29 | - def handle_file_type(file_name, mime_type = nil) | |
| 35 | + def handle_file_type(file_name) | |
| 30 | 36 | case file_name |
| 31 | - when /(\.pl|\.scala|\.java|\.haml|\.jade|\.scaml|\.html|\.sass|\.scss|\.php|\.erb)$/ | |
| 32 | - $1[1..-1].to_sym | |
| 33 | - when /(\.c|\.h|\.idc)$/ | |
| 34 | - :c | |
| 35 | - when /(\.cpp|\.hpp|\.c++|\.h++|\.cc|\.hh|\.cxx|\.hxx)$/ | |
| 36 | - :cpp | |
| 37 | - when /(\.d|\.di)$/ | |
| 38 | - :d | |
| 39 | - when /(\.hs|\.lhs)$/ | |
| 40 | - :haskell | |
| 41 | - when /(\.rb|\.ru|\.rake|Rakefile|\.gemspec|\.rbx|Gemfile)$/ | |
| 37 | + when /(\.ru|Gemfile)$/ | |
| 42 | 38 | :ruby |
| 43 | - when /(\.py|\.pyw|\.sc|SConstruct|SConscript|\.tac)$/ | |
| 44 | - :python | |
| 45 | - when /(\.js|\.json)$/ | |
| 46 | - :javascript | |
| 47 | - when /(\.xml|\.xsl|\.rss|\.xslt|\.xsd|\.wsdl)$/ | |
| 48 | - :xml | |
| 49 | - when /(\.vm|\.fhtml)$/ | |
| 50 | - :velocity | |
| 51 | - when /\.sh$/ | |
| 52 | - :bash | |
| 53 | - when /\.coffee$/ | |
| 54 | - :coffeescript | |
| 55 | - when /(\.yml|\.yaml)$/ | |
| 56 | - :yaml | |
| 57 | - when /\.md$/ | |
| 58 | - :minid | |
| 59 | 39 | else |
| 60 | 40 | :text |
| 61 | 41 | end | ... | ... |