diff options
| author | Bob Van Landuyt <bob@gitlab.com> | 2019-08-06 16:42:14 +0000 |
|---|---|---|
| committer | Bob Van Landuyt <bob@gitlab.com> | 2019-08-06 16:42:14 +0000 |
| commit | d61dab914756854c0f5bef50306be77212ee15b4 (patch) | |
| tree | 7e2396450980c36fefab904e8917a15ab034a343 /lib | |
| parent | 550e0d45c8e2237c68d834ca00cfc866fc78bb01 (diff) | |
| parent | 46631e102366bd40bdb449b87fae3f10e992ee68 (diff) | |
| download | gitlab-ce-d61dab914756854c0f5bef50306be77212ee15b4.tar.gz | |
Merge branch '65152-selective-highlight' into 'master'
Support selective highlighting of lines
See merge request gitlab-org/gitlab-ce!31361
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/gitlab/highlight.rb | 11 | ||||
| -rw-r--r-- | lib/rouge/formatters/html_gitlab.rb | 4 |
2 files changed, 8 insertions, 7 deletions
diff --git a/lib/gitlab/highlight.rb b/lib/gitlab/highlight.rb index 381f1dd4e55..1f49a26f0a2 100644 --- a/lib/gitlab/highlight.rb +++ b/lib/gitlab/highlight.rb @@ -6,15 +6,16 @@ module Gitlab TIMEOUT_FOREGROUND = 3.seconds MAXIMUM_TEXT_HIGHLIGHT_SIZE = 1.megabyte - def self.highlight(blob_name, blob_content, language: nil, plain: false) - new(blob_name, blob_content, language: language) + def self.highlight(blob_name, blob_content, since: nil, language: nil, plain: false) + new(blob_name, blob_content, since: since, language: language) .highlight(blob_content, continue: false, plain: plain) end attr_reader :blob_name - def initialize(blob_name, blob_content, language: nil) + def initialize(blob_name, blob_content, since: nil, language: nil) @formatter = Rouge::Formatters::HTMLGitlab + @since = since @language = language @blob_name = blob_name @blob_content = blob_content @@ -53,13 +54,13 @@ module Gitlab end def highlight_plain(text) - @formatter.format(Rouge::Lexers::PlainText.lex(text)).html_safe + @formatter.format(Rouge::Lexers::PlainText.lex(text), since: @since).html_safe end def highlight_rich(text, continue: true) tag = lexer.tag tokens = lexer.lex(text, continue: continue) - Timeout.timeout(timeout_time) { @formatter.format(tokens, tag: tag).html_safe } + Timeout.timeout(timeout_time) { @formatter.format(tokens, tag: tag, since: @since).html_safe } rescue Timeout::Error => e Gitlab::Sentry.track_exception(e) highlight_plain(text) diff --git a/lib/rouge/formatters/html_gitlab.rb b/lib/rouge/formatters/html_gitlab.rb index e2a7d3ef5ba..0d4ac504428 100644 --- a/lib/rouge/formatters/html_gitlab.rb +++ b/lib/rouge/formatters/html_gitlab.rb @@ -8,8 +8,8 @@ module Rouge # Creates a new <tt>Rouge::Formatter::HTMLGitlab</tt> instance. # # [+tag+] The tag (language) of the lexer used to generate the formatted tokens - def initialize(tag: nil) - @line_number = 1 + def initialize(tag: nil, since: nil) + @line_number = since || 1 @tag = tag end |
