Commit f8b80f7f authored by http://jneen.net/'s avatar http://jneen.net/

add custom highlighting via .gitattributes

paired with @stanhu
parent 0fd4b9d3
module BlobHelper module BlobHelper
def highlighter(blob_name, blob_content, nowrap: false) def highlighter(blob_name, blob_content, repository: nil, nowrap: false)
Gitlab::Highlight.new(blob_name, blob_content, nowrap: nowrap) Gitlab::Highlight.new(blob_name, blob_content, nowrap: nowrap, repository: repository)
end end
def highlight(blob_name, blob_content, nowrap: false, plain: false) def highlight(blob_name, blob_content, repository: nil, nowrap: false, plain: false)
Gitlab::Highlight.highlight(blob_name, blob_content, nowrap: nowrap, plain: plain) Gitlab::Highlight.highlight(blob_name, blob_content, nowrap: nowrap, plain: plain, repository: repository)
end end
def no_highlight_files def no_highlight_files
......
...@@ -978,6 +978,10 @@ class Repository ...@@ -978,6 +978,10 @@ class Repository
raw_repository.ls_files(actual_ref) raw_repository.ls_files(actual_ref)
end end
def gitattribute(path, name)
raw_repository.attributes(path)[name]
end
def copy_gitattributes(ref) def copy_gitattributes(ref)
actual_ref = ref || root_ref actual_ref = ref || root_ref
begin begin
......
...@@ -16,4 +16,4 @@ ...@@ -16,4 +16,4 @@
.file-content.code .file-content.code
.nothing-here-block Empty file .nothing-here-block Empty file
- else - else
= render 'shared/file_highlight', blob: blob = render 'shared/file_highlight', blob: blob, repository: @repository
- repository = nil unless local_assigns.key?(:repository)
.file-content.code.js-syntax-highlight .file-content.code.js-syntax-highlight
.line-numbers .line-numbers
- if blob.data.present? - if blob.data.present?
...@@ -11,4 +13,4 @@ ...@@ -11,4 +13,4 @@
= link_icon = link_icon
= i = i
.blob-content{data: {blob_id: blob.id}} .blob-content{data: {blob_id: blob.id}}
= highlight(blob.name, blob.data, plain: blob.no_highlighting?) = highlight(blob.path, blob.data, repository: repository, plain: blob.no_highlighting?)
...@@ -41,7 +41,8 @@ module Gitlab ...@@ -41,7 +41,8 @@ module Gitlab
def highlighted_lines def highlighted_lines
@blob.load_all_data!(repository) @blob.load_all_data!(repository)
@highlighted_lines ||= Gitlab::Highlight.highlight(@blob.name, @blob.data).lines @highlighted_lines ||=
Gitlab::Highlight.highlight(@blob.path, @blob.data, repository: repository).lines
end end
def project def project
......
module Gitlab module Gitlab
class Highlight class Highlight
def self.highlight(blob_name, blob_content, nowrap: true, plain: false) def self.highlight(blob_name, blob_content,
new(blob_name, blob_content, nowrap: nowrap). repository: nil, nowrap: true, plain: false)
new(blob_name, blob_content, nowrap: nowrap, repository: repository).
highlight(blob_content, continue: false, plain: plain) highlight(blob_content, continue: false, plain: plain)
end end
...@@ -10,12 +11,29 @@ module Gitlab ...@@ -10,12 +11,29 @@ module Gitlab
return [] unless blob return [] unless blob
blob.load_all_data!(repository) blob.load_all_data!(repository)
highlight(file_name, blob.data).lines.map!(&:html_safe) highlight(file_name, blob.data, repository: repository).lines.map!(&:html_safe)
end end
def initialize(blob_name, blob_content, nowrap: true) attr_reader :lexer
def initialize(blob_name, blob_content, repository: nil, nowrap: true)
@blob_name = blob_name
@blob_content = blob_content
@repository = repository
@formatter = rouge_formatter(nowrap: nowrap) @formatter = rouge_formatter(nowrap: nowrap)
@lexer = Rouge::Lexer.guess(filename: blob_name, source: blob_content).new rescue Rouge::Lexers::PlainText
@lexer = custom_language || begin
Rouge::Lexer.guess(filename: blob_name, source: blob_content).new
rescue Rouge::Lexer::AmbiguousGuess => e
e.alternatives.sort_by(&:tag).first
end
end
def custom_language
return nil if @repository.nil?
language_name = @repository.gitattribute(@blob_name, 'gitlab-language')
Rouge::Lexer.find(language_name)
end end
def highlight(text, continue: true, plain: false) def highlight(text, continue: true, plain: false)
......
...@@ -18,4 +18,14 @@ describe Gitlab::Highlight, lib: true do ...@@ -18,4 +18,14 @@ describe Gitlab::Highlight, lib: true do
end end
end end
describe 'custom highlighting from .gitattributes' do
let(:blob) { project.blob_at_branch('master', 'custom-highlighting/test.gitlab-custom') }
let(:highlighter) {
Gitlab::Highlight.new(blob.path, blob.contents, repository: project.repository)
}
it 'highlights as ruby' do
expect(highlighter.lexer).to be_an_instance_of Rouge::Lexers::Ruby
end
end
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