diff options
author | Douwe Maan <douwe@gitlab.com> | 2016-01-14 17:11:50 +0300 |
---|---|---|
committer | Douwe Maan <douwe@gitlab.com> | 2016-01-14 17:11:50 +0300 |
commit | 3a1d0535992594bc77320f081d1f20b760b1c1f7 (patch) | |
tree | 01648b2b11bf9a3cdc012bda4da62eaf075650fe /lib/gitlab/highlight.rb | |
parent | c881627d114eb9c050d605e93673ef65a9da9a58 (diff) |
Remove duplication around highlighting.
Diffstat (limited to 'lib/gitlab/highlight.rb')
-rw-r--r-- | lib/gitlab/highlight.rb | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/lib/gitlab/highlight.rb b/lib/gitlab/highlight.rb new file mode 100644 index 00000000000..02e097eca3d --- /dev/null +++ b/lib/gitlab/highlight.rb @@ -0,0 +1,23 @@ +module Gitlab + class Highlight + def self.highlight(blob_name, blob_content, nowrap: true, continue: false) + formatter = rouge_formatter(nowrap: nowrap) + + lexer = Rouge::Lexer.guess(filename: blob_name, source: blob_content).new rescue Rouge::Lexers::PlainText + formatter.format(lexer.lex(blob_content, continue: continue)).html_safe + end + + private + + def self.rouge_formatter(options = {}) + options = options.reverse_merge( + nowrap: true, + cssclass: 'code highlight', + lineanchors: true, + lineanchorsid: 'LC' + ) + + Rouge::Formatters::HTMLGitlab.new(options) + end + end +end |