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
Jérome Perrin
gitlab-ce
Commits
e1824aa1
Commit
e1824aa1
authored
8 years ago
by
http://jneen.net/
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
use the new token_lines interface to format lines
parent
34a3d2a3
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
12 additions
and
74 deletions
+12
-74
lib/gitlab/highlight.rb
lib/gitlab/highlight.rb
+1
-4
lib/rouge/formatters/html_gitlab.rb
lib/rouge/formatters/html_gitlab.rb
+11
-70
No files found.
lib/gitlab/highlight.rb
View file @
e1824aa1
...
...
@@ -47,10 +47,7 @@ module Gitlab
end
def
rouge_formatter
(
options
=
{})
Rouge
::
Formatters
::
HTMLGitlab
.
new
(
lineanchors:
true
,
lineanchorsid:
'LC'
)
Rouge
::
Formatters
::
HTMLGitlab
.
new
end
end
end
This diff is collapsed.
Click to expand it.
lib/rouge/formatters/html_gitlab.rb
View file @
e1824aa1
...
...
@@ -2,83 +2,24 @@ require 'cgi'
module
Rouge
module
Formatters
class
HTMLGitlab
<
Rouge
::
Formatter
class
HTMLGitlab
<
Rouge
::
Formatter
s
::
HTML
tag
'html_gitlab'
# Creates a new <tt>Rouge::Formatter::HTMLGitlab</tt> instance.
#
# [+cssclass+] CSS class for the wrapping <tt><div></tt> tag
# (default: 'highlight').
# [+lineanchors+] If set to true the formatter will wrap each output
# line in an anchor tag with a name of L-linenumber.
# This allows easy linking to certain lines
# (default: false).
# [+lineanchorsid+] If lineanchors is true the name of the anchors can
# be changed with lineanchorsid to e.g. foo-linenumber
# (default: 'L').
def
initialize
(
lineanchors:
false
,
lineanchorsid:
'L'
)
@lineanchors
=
lineanchors
@lineanchorsid
=
lineanchorsid
# [+linenostart+] The line number for the first line (default: 1).
def
initialize
(
linenostart:
1
)
@linenostart
=
linenostart
@line_number
=
linenostart
end
def
render
(
tokens
)
data
=
process_tokens
(
tokens
)
def
stream
(
tokens
,
&
b
)
token_lines
(
tokens
)
do
|
line
|
yield
%<<span id="LC#{@line_number}" class="line">>
line
.
each
{
|
token
,
value
|
yield
span
(
token
,
value
)
}
yield
%<</span>\n>
wrap_lines
(
data
[
:code
])
end
alias_method
:format
,
:render
private
def
process_tokens
(
tokens
)
rendered
=
[]
current_line
=
''
tokens
.
each
do
|
tok
,
val
|
# In the case of multi-line values (e.g. comments), we need to apply
# styling to each line since span elements are inline.
val
.
lines
.
each
do
|
line
|
stripped
=
line
.
chomp
current_line
<<
span
(
tok
,
stripped
)
if
line
.
end_with?
(
"
\n
"
)
rendered
<<
current_line
current_line
=
''
end
end
end
# Add leftover text
rendered
<<
current_line
if
current_line
.
present?
{
code:
rendered
}
end
def
wrap_lines
(
lines
)
if
@lineanchors
lines
=
lines
.
each_with_index
.
map
do
|
line
,
index
|
number
=
index
+
@linenostart
"<span id=
\"
#{
@lineanchorsid
}#{
number
}
\"
class=
\"
line
\"
>
#{
line
}
"
\
'</span>'
end
end
lines
.
join
(
"
\n
"
)
end
def
span
(
tok
,
val
)
# http://stackoverflow.com/a/1600584/2587286
val
=
CGI
.
escapeHTML
(
val
)
if
tok
.
shortname
.
empty?
val
else
"<span class=
\"
#{
tok
.
shortname
}
\"
>
#{
val
}
</span>"
@line_number
+=
1
end
end
end
...
...
This diff is collapsed.
Click to expand it.
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