Commit b468672d authored by http://jneen.net/'s avatar http://jneen.net/

style: factor out `@lexer` into a method

parent 822304b1
...@@ -17,26 +17,30 @@ module Gitlab ...@@ -17,26 +17,30 @@ module Gitlab
@formatter = Rouge::Formatters::HTMLGitlab.new @formatter = Rouge::Formatters::HTMLGitlab.new
@repository = repository @repository = repository
@blob_name = blob_name @blob_name = blob_name
@lexer = custom_language || begin @blob_content = blob_content
Rouge::Lexer.guess(filename: blob_name, source: blob_content).new
rescue Rouge::Lexer::AmbiguousGuess => e
e.alternatives.sort_by(&:tag).first
end
end end
def highlight(text, continue: true, plain: false) def highlight(text, continue: true, plain: false)
lexer = @lexer
if plain if plain
lexer = Rouge::Lexers::PlainText hl_lexer = Rouge::Lexers::PlainText
continue = false continue = false
else
hl_lexer = self.lexer
end end
@formatter.format(lexer.lex(text, continue: continue)).html_safe @formatter.format(hl_lexer.lex(text, continue: continue)).html_safe
rescue rescue
@formatter.format(Rouge::Lexers::PlainText.lex(text)).html_safe @formatter.format(Rouge::Lexers::PlainText.lex(text)).html_safe
end end
def lexer
@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
private private
def custom_language def custom_language
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment