diff options
author | http://jneen.net/ <jneen@jneen.net> | 2016-06-11 01:42:43 +0300 |
---|---|---|
committer | http://jneen.net/ <jneen@jneen.net> | 2016-06-28 00:17:49 +0300 |
commit | f8b80f7faedef7515d05abd9a5cc315d98724ae3 (patch) | |
tree | 9f0079320c6edbf2ded9221960d2f71e1aca5053 /lib/gitlab/highlight.rb | |
parent | 0fd4b9d3e2c800e728e17e919fa7369b3322c65b (diff) |
add custom highlighting via .gitattributes
paired with @stanhu
Diffstat (limited to 'lib/gitlab/highlight.rb')
-rw-r--r-- | lib/gitlab/highlight.rb | 28 |
1 files changed, 23 insertions, 5 deletions
diff --git a/lib/gitlab/highlight.rb b/lib/gitlab/highlight.rb index 280120b0f9e..2ba934a85c5 100644 --- a/lib/gitlab/highlight.rb +++ b/lib/gitlab/highlight.rb @@ -1,7 +1,8 @@ module Gitlab class Highlight - def self.highlight(blob_name, blob_content, nowrap: true, plain: false) - new(blob_name, blob_content, nowrap: nowrap). + def self.highlight(blob_name, blob_content, + repository: nil, nowrap: true, plain: false) + new(blob_name, blob_content, nowrap: nowrap, repository: repository). highlight(blob_content, continue: false, plain: plain) end @@ -10,12 +11,29 @@ module Gitlab return [] unless blob blob.load_all_data!(repository) - highlight(file_name, blob.data).lines.map!(&:html_safe) + highlight(file_name, blob.data, repository: repository).lines.map!(&:html_safe) end - def initialize(blob_name, blob_content, nowrap: true) + attr_reader :lexer + def initialize(blob_name, blob_content, repository: nil, nowrap: true) + @blob_name = blob_name + @blob_content = blob_content + @repository = repository @formatter = rouge_formatter(nowrap: nowrap) - @lexer = Rouge::Lexer.guess(filename: blob_name, source: blob_content).new rescue Rouge::Lexers::PlainText + + @lexer = custom_language || begin + Rouge::Lexer.guess(filename: blob_name, source: blob_content).new + rescue Rouge::Lexer::AmbiguousGuess => e + e.alternatives.sort_by(&:tag).first + end + end + + def custom_language + return nil if @repository.nil? + + language_name = @repository.gitattribute(@blob_name, 'gitlab-language') + + Rouge::Lexer.find(language_name) end def highlight(text, continue: true, plain: false) |