Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Tatuya Kamada
gitlab-ce
Commits
f8b80f7f
Commit
f8b80f7f
authored
Jun 10, 2016
by
http://jneen.net/
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add custom highlighting via .gitattributes
paired with @stanhu
parent
0fd4b9d3
Changes
7
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
47 additions
and
12 deletions
+47
-12
app/helpers/blob_helper.rb
app/helpers/blob_helper.rb
+4
-4
app/models/repository.rb
app/models/repository.rb
+4
-0
app/views/projects/blob/_text.html.haml
app/views/projects/blob/_text.html.haml
+1
-1
app/views/shared/_file_highlight.html.haml
app/views/shared/_file_highlight.html.haml
+3
-1
lib/gitlab/blame.rb
lib/gitlab/blame.rb
+2
-1
lib/gitlab/highlight.rb
lib/gitlab/highlight.rb
+23
-5
spec/lib/gitlab/highlight_spec.rb
spec/lib/gitlab/highlight_spec.rb
+10
-0
No files found.
app/helpers/blob_helper.rb
View file @
f8b80f7f
module
BlobHelper
def
highlighter
(
blob_name
,
blob_content
,
nowrap:
false
)
Gitlab
::
Highlight
.
new
(
blob_name
,
blob_content
,
nowrap:
nowrap
)
def
highlighter
(
blob_name
,
blob_content
,
repository:
nil
,
nowrap:
false
)
Gitlab
::
Highlight
.
new
(
blob_name
,
blob_content
,
nowrap:
nowrap
,
repository:
repository
)
end
def
highlight
(
blob_name
,
blob_content
,
nowrap:
false
,
plain:
false
)
Gitlab
::
Highlight
.
highlight
(
blob_name
,
blob_content
,
nowrap:
nowrap
,
plain:
plain
)
def
highlight
(
blob_name
,
blob_content
,
repository:
nil
,
nowrap:
false
,
plain:
false
)
Gitlab
::
Highlight
.
highlight
(
blob_name
,
blob_content
,
nowrap:
nowrap
,
plain:
plain
,
repository:
repository
)
end
def
no_highlight_files
...
...
app/models/repository.rb
View file @
f8b80f7f
...
...
@@ -978,6 +978,10 @@ class Repository
raw_repository
.
ls_files
(
actual_ref
)
end
def
gitattribute
(
path
,
name
)
raw_repository
.
attributes
(
path
)[
name
]
end
def
copy_gitattributes
(
ref
)
actual_ref
=
ref
||
root_ref
begin
...
...
app/views/projects/blob/_text.html.haml
View file @
f8b80f7f
...
...
@@ -16,4 +16,4 @@
.file-content.code
.nothing-here-block
Empty file
-
else
=
render
'shared/file_highlight'
,
blob:
blob
=
render
'shared/file_highlight'
,
blob:
blob
,
repository:
@repository
app/views/shared/_file_highlight.html.haml
View file @
f8b80f7f
-
repository
=
nil
unless
local_assigns
.
key?
(
:repository
)
.file-content.code.js-syntax-highlight
.line-numbers
-
if
blob
.
data
.
present?
...
...
@@ -11,4 +13,4 @@
=
link_icon
=
i
.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?
)
lib/gitlab/blame.rb
View file @
f8b80f7f
...
...
@@ -41,7 +41,8 @@ module Gitlab
def
highlighted_lines
@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
def
project
...
...
lib/gitlab/highlight.rb
View file @
f8b80f7f
module
Gitlab
class
Highlight
def
self
.
highlight
(
blob_name
,
blob_content
,
nowrap:
true
,
plain:
false
)
new
(
blob_name
,
blob_content
,
nowrap:
nowrap
).
def
self
.
highlight
(
blob_name
,
blob_content
,
repository:
nil
,
nowrap:
true
,
plain:
false
)
new
(
blob_name
,
blob_content
,
nowrap:
nowrap
,
repository:
repository
).
highlight
(
blob_content
,
continue:
false
,
plain:
plain
)
end
...
...
@@ -10,12 +11,29 @@ module Gitlab
return
[]
unless
blob
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
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
)
@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
def
highlight
(
text
,
continue:
true
,
plain:
false
)
...
...
spec/lib/gitlab/highlight_spec.rb
View file @
f8b80f7f
...
...
@@ -18,4 +18,14 @@ describe Gitlab::Highlight, lib: true do
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
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment