Commit d2e7b2e9 authored by Mark Chao's avatar Mark Chao

Move :plain option to Highlight class

This is to DRY the repeated file size check.

Move spec and constants to Highlight
parent ca82a46a
...@@ -2,7 +2,6 @@ ...@@ -2,7 +2,6 @@
module BlobHelper module BlobHelper
def highlight(file_name, file_content, language: nil, plain: false) def highlight(file_name, file_content, language: nil, plain: false)
plain ||= file_content.length > Blob::MAXIMUM_TEXT_HIGHLIGHT_SIZE
highlighted = Gitlab::Highlight.highlight(file_name, file_content, plain: plain, language: language) highlighted = Gitlab::Highlight.highlight(file_name, file_content, plain: plain, language: language)
raw %(<pre class="code highlight"><code>#{highlighted}</code></pre>) raw %(<pre class="code highlight"><code>#{highlighted}</code></pre>)
......
...@@ -8,8 +8,6 @@ class Blob < SimpleDelegator ...@@ -8,8 +8,6 @@ class Blob < SimpleDelegator
CACHE_TIME = 60 # Cache raw blobs referred to by a (mutable) ref for 1 minute CACHE_TIME = 60 # Cache raw blobs referred to by a (mutable) ref for 1 minute
CACHE_TIME_IMMUTABLE = 3600 # Cache blobs referred to by an immutable reference for 1 hour CACHE_TIME_IMMUTABLE = 3600 # Cache blobs referred to by an immutable reference for 1 hour
MAXIMUM_TEXT_HIGHLIGHT_SIZE = 1.megabyte
# Finding a viewer for a blob happens based only on extension and whether the # Finding a viewer for a blob happens based only on extension and whether the
# blob is binary or text, which means 1 blob should only be matched by 1 viewer, # blob is binary or text, which means 1 blob should only be matched by 1 viewer,
# and the order of these viewers doesn't really matter. # and the order of these viewers doesn't really matter.
...@@ -124,10 +122,6 @@ class Blob < SimpleDelegator ...@@ -124,10 +122,6 @@ class Blob < SimpleDelegator
end end
end end
def no_highlighting?
raw_size && raw_size > MAXIMUM_TEXT_HIGHLIGHT_SIZE
end
def empty? def empty?
raw_size == 0 raw_size == 0
end end
......
...@@ -8,7 +8,7 @@ class BlobPresenter < Gitlab::View::Presenter::Simple ...@@ -8,7 +8,7 @@ class BlobPresenter < Gitlab::View::Presenter::Simple
blob.path, blob.path,
blob.data, blob.data,
language: blob.language_from_gitattributes, language: blob.language_from_gitattributes,
plain: (plain || blob.no_highlighting?) plain: plain
) )
end end
end end
...@@ -39,7 +39,7 @@ ...@@ -39,7 +39,7 @@
.blob-content .blob-content
- snippet_chunks.each do |chunk| - snippet_chunks.each do |chunk|
- unless chunk[:data].empty? - unless chunk[:data].empty?
= highlight(snippet.file_name, chunk[:data], plain: snippet.blob.no_highlighting?) = highlight(snippet.file_name, chunk[:data])
- else - else
.file-content.code .file-content.code
.nothing-here-block Empty file .nothing-here-block Empty file
...@@ -4,6 +4,7 @@ module Gitlab ...@@ -4,6 +4,7 @@ module Gitlab
class Highlight class Highlight
TIMEOUT_BACKGROUND = 30.seconds TIMEOUT_BACKGROUND = 30.seconds
TIMEOUT_FOREGROUND = 3.seconds TIMEOUT_FOREGROUND = 3.seconds
MAXIMUM_TEXT_HIGHLIGHT_SIZE = 1.megabyte
def self.highlight(blob_name, blob_content, language: nil, plain: false) def self.highlight(blob_name, blob_content, language: nil, plain: false)
new(blob_name, blob_content, language: language) new(blob_name, blob_content, language: language)
...@@ -20,6 +21,8 @@ module Gitlab ...@@ -20,6 +21,8 @@ module Gitlab
end end
def highlight(text, continue: true, plain: false) 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 = highlight_text(text, continue: continue, plain: plain)
highlighted_text = link_dependencies(text, highlighted_text) if blob_name highlighted_text = link_dependencies(text, highlighted_text) if blob_name
highlighted_text highlighted_text
......
...@@ -23,10 +23,6 @@ module Gitlab ...@@ -23,10 +23,6 @@ module Gitlab
filename filename
end end
def no_highlighting?
false
end
# Since search results often contain many items, # Since search results often contain many items,
# not triggering lookup can avoid n+1 queries. # not triggering lookup can avoid n+1 queries.
def language_from_gitattributes def language_from_gitattributes
......
...@@ -3,63 +3,13 @@ require 'spec_helper' ...@@ -3,63 +3,13 @@ require 'spec_helper'
describe BlobHelper do describe BlobHelper do
include TreeHelper include TreeHelper
let(:blob_name) { 'test.lisp' }
let(:no_context_content) { ":type \"assem\"))" }
let(:blob_content) { "(make-pathname :defaults name\n#{no_context_content}" }
let(:split_content) { blob_content.split("\n") }
let(:multiline_content) do
%q(
def test(input):
"""This is line 1 of a multi-line comment.
This is line 2.
"""
)
end
describe '#highlight' do describe '#highlight' do
it 'returns plaintext for unknown lexer context' do it 'wraps highlighted content' do
result = helper.highlight(blob_name, no_context_content) expect(helper.highlight('test.rb', '52')).to eq(%q[<pre class="code highlight"><code><span id="LC1" class="line" lang="ruby"><span class="mi">52</span></span></code></pre>])
expect(result).to eq(%[<pre class="code highlight"><code><span id="LC1" class="line" lang="">:type "assem"))</span></code></pre>])
end end
it 'returns plaintext for long blobs' do it 'handles plain version' do
stub_const('Blob::MAXIMUM_TEXT_HIGHLIGHT_SIZE', 1) expect(helper.highlight('test.rb', '52', plain: true)).to eq(%q[<pre class="code highlight"><code><span id="LC1" class="line" lang="">52</span></code></pre>])
result = helper.highlight(blob_name, blob_content)
expect(result).to eq(%[<pre class="code highlight"><code><span id="LC1" class="line" lang="">(make-pathname :defaults name</span>\n<span id="LC2" class="line" lang="">:type "assem"))</span></code></pre>])
end
it 'highlights single block' do
expected = %Q[<pre class="code highlight"><code><span id="LC1" class="line" lang="common_lisp"><span class="p">(</span><span class="nb">make-pathname</span> <span class="ss">:defaults</span> <span class="nv">name</span></span>
<span id="LC2" class="line" lang="common_lisp"><span class="ss">:type</span> <span class="s">"assem"</span><span class="p">))</span></span></code></pre>]
expect(helper.highlight(blob_name, blob_content)).to eq(expected)
end
it 'highlights multi-line comments' do
result = helper.highlight(blob_name, multiline_content)
html = Nokogiri::HTML(result)
lines = html.search('.s')
expect(lines.count).to eq(3)
expect(lines[0].text).to eq('"""This is line 1 of a multi-line comment.')
expect(lines[1].text).to eq(' This is line 2.')
expect(lines[2].text).to eq(' """')
end
context 'diff highlighting' do
let(:blob_name) { 'test.diff' }
let(:blob_content) { "+aaa\n+bbb\n- ccc\n ddd\n"}
let(:expected) do
%q(<pre class="code highlight"><code><span id="LC1" class="line" lang="diff"><span class="gi">+aaa</span></span>
<span id="LC2" class="line" lang="diff"><span class="gi">+bbb</span></span>
<span id="LC3" class="line" lang="diff"><span class="gd">- ccc</span></span>
<span id="LC4" class="line" lang="diff"> ddd</span></code></pre>)
end
it 'highlights each line properly' do
result = helper.highlight(blob_name, blob_content)
expect(result).to eq(expected)
end
end end
end end
......
...@@ -18,6 +18,66 @@ describe Gitlab::Highlight do ...@@ -18,6 +18,66 @@ describe Gitlab::Highlight do
end end
describe '#highlight' do describe '#highlight' do
let(:file_name) { 'test.lisp' }
let(:no_context_content) { ":type \"assem\"))" }
let(:content) { "(make-pathname :defaults name\n#{no_context_content}" }
let(:multiline_content) do
%q(
def test(input):
"""This is line 1 of a multi-line comment.
This is line 2.
"""
)
end
it 'highlights' do
expected = %Q[<span id="LC1" class="line" lang="common_lisp"><span class="p">(</span><span class="nb">make-pathname</span> <span class="ss">:defaults</span> <span class="nv">name</span></span>
<span id="LC2" class="line" lang="common_lisp"><span class="ss">:type</span> <span class="s">"assem"</span><span class="p">))</span></span>]
expect(described_class.highlight(file_name, content)).to eq(expected)
end
it 'returns plain version for unknown lexer context' do
result = described_class.highlight(file_name, no_context_content)
expect(result).to eq(%[<span id="LC1" class="line" lang="">:type "assem"))</span>])
end
it 'returns plain version for long content' do
stub_const('Gitlab::Highlight::MAXIMUM_TEXT_HIGHLIGHT_SIZE', 1)
result = described_class.highlight(file_name, content)
expect(result).to eq(%[<span id="LC1" class="line" lang="">(make-pathname :defaults name</span>\n<span id="LC2" class="line" lang="">:type "assem"))</span>])
end
it 'highlights multi-line comments' do
result = described_class.highlight(file_name, multiline_content)
html = Nokogiri::HTML(result)
lines = html.search('.s')
expect(lines.count).to eq(3)
expect(lines[0].text).to eq('"""This is line 1 of a multi-line comment.')
expect(lines[1].text).to eq(' This is line 2.')
expect(lines[2].text).to eq(' """')
end
context 'diff highlighting' do
let(:file_name) { 'test.diff' }
let(:content) { "+aaa\n+bbb\n- ccc\n ddd\n"}
let(:expected) do
%q(<span id="LC1" class="line" lang="diff"><span class="gi">+aaa</span></span>
<span id="LC2" class="line" lang="diff"><span class="gi">+bbb</span></span>
<span id="LC3" class="line" lang="diff"><span class="gd">- ccc</span></span>
<span id="LC4" class="line" lang="diff"> ddd</span>)
end
it 'highlights each line properly' do
result = described_class.highlight(file_name, content)
expect(result).to eq(expected)
end
end
describe 'with CRLF' do describe 'with CRLF' do
let(:branch) { 'crlf-diff' } let(:branch) { 'crlf-diff' }
let(:path) { 'files/whitespace' } let(:path) { 'files/whitespace' }
......
...@@ -18,14 +18,7 @@ describe BlobPresenter, :seed_helper do ...@@ -18,14 +18,7 @@ describe BlobPresenter, :seed_helper do
subject { described_class.new(blob) } subject { described_class.new(blob) }
it 'returns highlighted content' do it 'returns highlighted content' do
expect(Gitlab::Highlight).to receive(:highlight).with('files/ruby/regex.rb', git_blob.data, plain: false, language: nil) expect(Gitlab::Highlight).to receive(:highlight).with('files/ruby/regex.rb', git_blob.data, plain: nil, language: nil)
subject.highlight
end
context 'with :plain' do
it 'returns plain content when no_highlighting? is true' do
allow(blob).to receive(:no_highlighting?).and_return(true)
subject.highlight subject.highlight
end end
...@@ -36,22 +29,13 @@ describe BlobPresenter, :seed_helper do ...@@ -36,22 +29,13 @@ describe BlobPresenter, :seed_helper do
subject.highlight(plain: true) subject.highlight(plain: true)
end end
it 'returns plain content when :plain is false, but no_highlighting? is true' do
allow(blob).to receive(:no_highlighting?).and_return(true)
expect(Gitlab::Highlight).to receive(:highlight).with('files/ruby/regex.rb', git_blob.data, plain: true, language: nil)
subject.highlight(plain: false)
end
end
context 'gitlab-language contains a match' do context 'gitlab-language contains a match' do
before do before do
allow(blob).to receive(:language_from_gitattributes).and_return('ruby') allow(blob).to receive(:language_from_gitattributes).and_return('ruby')
end end
it 'passes language to inner call' do it 'passes language to inner call' do
expect(Gitlab::Highlight).to receive(:highlight).with('files/ruby/regex.rb', git_blob.data, plain: false, language: 'ruby') expect(Gitlab::Highlight).to receive(:highlight).with('files/ruby/regex.rb', git_blob.data, plain: nil, language: 'ruby')
subject.highlight subject.highlight
end end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment