Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJacob Vosmaer <contact@jacobvosmaer.nl>2016-02-01 15:51:05 +0300
committerJacob Vosmaer <contact@jacobvosmaer.nl>2016-02-01 15:51:05 +0300
commit51574d779cac5b97ffb7603db783a26bfd6da1e6 (patch)
treecd75ea27e009328a788abbaf0f60c6a4f46dfc44 /lib/gitlab/highlight.rb
parent64c8ee47c96d9245081abdf1b9d4ec39cdfc5883 (diff)
parentda8e0f86595299740a344309cb5963854b61c4a6 (diff)
Merge branch 'master' of https://gitlab.com/gitlab-org/gitlab-ce into lazy-blobs
Diffstat (limited to 'lib/gitlab/highlight.rb')
-rw-r--r--lib/gitlab/highlight.rb39
1 files changed, 39 insertions, 0 deletions
diff --git a/lib/gitlab/highlight.rb b/lib/gitlab/highlight.rb
new file mode 100644
index 00000000000..cac76442321
--- /dev/null
+++ b/lib/gitlab/highlight.rb
@@ -0,0 +1,39 @@
+module Gitlab
+ class Highlight
+ def self.highlight(blob_name, blob_content, nowrap: true)
+ new(blob_name, blob_content, nowrap: nowrap).highlight(blob_content, continue: false)
+ end
+
+ def self.highlight_lines(repository, ref, file_name)
+ blob = repository.blob_at(ref, file_name)
+ return [] unless blob
+
+ blob.load_all_data!(repository)
+ highlight(file_name, blob.data).lines.map!(&:html_safe)
+ end
+
+ def initialize(blob_name, blob_content, nowrap: true)
+ @formatter = rouge_formatter(nowrap: nowrap)
+ @lexer = Rouge::Lexer.guess(filename: blob_name, source: blob_content).new rescue Rouge::Lexers::PlainText
+ end
+
+ def highlight(text, continue: true)
+ @formatter.format(@lexer.lex(text, continue: continue)).html_safe
+ rescue
+ @formatter.format(Rouge::Lexers::PlainText.lex(text)).html_safe
+ end
+
+ private
+
+ def rouge_formatter(options = {})
+ options = options.reverse_merge(
+ nowrap: true,
+ cssclass: 'code highlight',
+ lineanchors: true,
+ lineanchorsid: 'LC'
+ )
+
+ Rouge::Formatters::HTMLGitlab.new(options)
+ end
+ end
+end