Class: Gitlab::Highlight

Inherits:
Object
  • Object
show all
Defined in:
lib/gitlab/highlight.rb

Constant Summary collapse

TIMEOUT_BACKGROUND =
30.seconds
TIMEOUT_FOREGROUND =
1.5.seconds
MAXIMUM_TEXT_HIGHLIGHT_SIZE =
512.kilobytes

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(blob_name, blob_content, language: nil) ⇒ Highlight

Returns a new instance of Highlight.


16
17
18
19
20
21
# File 'lib/gitlab/highlight.rb', line 16

def initialize(blob_name, blob_content, language: nil)
  @formatter = Rouge::Formatters::HTMLGitlab
  @language = language
  @blob_name = blob_name
  @blob_content = blob_content
end

Instance Attribute Details

#blob_nameObject (readonly)

Returns the value of attribute blob_name


14
15
16
# File 'lib/gitlab/highlight.rb', line 14

def blob_name
  @blob_name
end

Class Method Details

.highlight(blob_name, blob_content, language: nil, plain: false) ⇒ Object


9
10
11
12
# File 'lib/gitlab/highlight.rb', line 9

def self.highlight(blob_name, blob_content, language: nil, plain: false)
  new(blob_name, blob_content, language: language)
    .highlight(blob_content, continue: false, plain: plain)
end

Instance Method Details

#highlight(text, continue: true, plain: false) ⇒ Object


23
24
25
26
27
28
29
# File 'lib/gitlab/highlight.rb', line 23

def highlight(text, continue: true, plain: false)
  plain ||= text.length > MAXIMUM_TEXT_HIGHLIGHT_SIZE

  highlighted_text = highlight_text(text, continue: continue, plain: plain)
  highlighted_text = link_dependencies(text, highlighted_text) if blob_name
  highlighted_text
end

#lexerObject


31
32
33
34
35
36
37
# File 'lib/gitlab/highlight.rb', line 31

def lexer
  @lexer ||= custom_language || begin
    Rouge::Lexer.guess(filename: @blob_name, source: @blob_content).new
                                rescue Rouge::Guesser::Ambiguous => e
                                  e.alternatives.min_by(&:tag)
  end
end