summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorhttp://jneen.net/ <jneen@jneen.net>2016-07-14 11:44:59 -0700
committerhttp://jneen.net/ <jneen@jneen.net>2016-07-14 11:44:59 -0700
commitb468672d7634b13b2a2bea5685a5e1cbc9d82c23 (patch)
tree2eb928d8750243e2600530e27f4a984630657894
parent822304b19f81386a36eb7175f7384e2a8c5fd5e1 (diff)
downloadgitlab-ce-b468672d7634b13b2a2bea5685a5e1cbc9d82c23.tar.gz
style: factor out `@lexer` into a method
-rw-r--r--lib/gitlab/highlight.rb22
1 files changed, 13 insertions, 9 deletions
diff --git a/lib/gitlab/highlight.rb b/lib/gitlab/highlight.rb
index 57b7fc139f8..a9e429a29f4 100644
--- a/lib/gitlab/highlight.rb
+++ b/lib/gitlab/highlight.rb
@@ -17,26 +17,30 @@ module Gitlab
@formatter = Rouge::Formatters::HTMLGitlab.new
@repository = repository
@blob_name = blob_name
- @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
+ @blob_content = blob_content
end
def highlight(text, continue: true, plain: false)
- lexer = @lexer
-
if plain
- lexer = Rouge::Lexers::PlainText
+ hl_lexer = Rouge::Lexers::PlainText
continue = false
+ else
+ hl_lexer = self.lexer
end
- @formatter.format(lexer.lex(text, continue: continue)).html_safe
+ @formatter.format(hl_lexer.lex(text, continue: continue)).html_safe
rescue
@formatter.format(Rouge::Lexers::PlainText.lex(text)).html_safe
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
def custom_language