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
82959349
Commit
82959349
authored
Jul 15, 2016
by
Douwe Maan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Don't fail to highlight when Rouge doesn't have a lexer
parent
fbd75c35
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
35 additions
and
14 deletions
+35
-14
lib/banzai/filter/syntax_highlight_filter.rb
lib/banzai/filter/syntax_highlight_filter.rb
+8
-7
spec/lib/banzai/filter/syntax_highlight_filter_spec.rb
spec/lib/banzai/filter/syntax_highlight_filter_spec.rb
+27
-7
No files found.
lib/banzai/filter/syntax_highlight_filter.rb
View file @
82959349
...
@@ -19,21 +19,22 @@ module Banzai
...
@@ -19,21 +19,22 @@ module Banzai
language
=
node
.
attr
(
'class'
)
language
=
node
.
attr
(
'class'
)
code
=
node
.
text
code
=
node
.
text
lexer
=
Rouge
::
Lexer
.
find_fancy
(
language
)
css_classes
=
"code highlight"
lexer
=
Rouge
::
Lexer
.
find_fancy
(
language
)
||
Rouge
::
Lexers
::
PlainText
formatter
=
Rouge
::
Formatters
::
HTML
.
new
formatter
=
Rouge
::
Formatters
::
HTML
.
new
css_classes
=
"code highlight js-syntax-highlight
#{
lexer
.
tag
}
"
begin
begin
highlighted
=
''
code
=
formatter
.
format
(
lexer
.
lex
(
code
))
highlighted
<<
%(<pre class="#{css_classes}"><code>)
highlighted
<<
formatter
.
format
(
lexer
.
lex
(
code
))
css_classes
<<
" js-syntax-highlight
#{
lexer
.
tag
}
"
highlighted
<<
%(</code></pre>)
rescue
rescue
# Gracefully handle syntax highlighter bugs/errors to ensure
# Gracefully handle syntax highlighter bugs/errors to ensure
# users can still access an issue/comment/etc.
# users can still access an issue/comment/etc.
highlighted
=
"<pre>
#{
code
}
</pre>"
end
end
highlighted
=
%(<pre class="#{css_classes}"><code>#{code}</code></pre>)
# Extracted to a method to measure it
# Extracted to a method to measure it
replace_parent_pre_element
(
node
,
highlighted
)
replace_parent_pre_element
(
node
,
highlighted
)
end
end
...
...
spec/lib/banzai/filter/syntax_highlight_filter_spec.rb
View file @
82959349
...
@@ -3,15 +3,35 @@ require 'spec_helper'
...
@@ -3,15 +3,35 @@ require 'spec_helper'
describe
Banzai
::
Filter
::
SyntaxHighlightFilter
,
lib:
true
do
describe
Banzai
::
Filter
::
SyntaxHighlightFilter
,
lib:
true
do
include
FilterSpecHelper
include
FilterSpecHelper
it
'highlights valid code blocks'
do
context
"when no language is specified"
do
result
=
filter
(
'<pre><code>def fun end</code>'
)
it
"highlights as plaintext"
do
expect
(
result
.
to_html
).
to
eq
(
"<pre class=
\"
code highlight js-syntax-highlight plaintext
\"
><code>def fun end</code></pre>"
)
result
=
filter
(
'<pre><code>def fun end</code></pre>'
)
expect
(
result
.
to_html
).
to
eq
(
'<pre class="code highlight js-syntax-highlight plaintext"><code>def fun end</code></pre>'
)
end
end
end
it
'passes through invalid code blocks'
do
context
"when a valid language is specified"
do
allow_any_instance_of
(
Rouge
::
Formatter
).
to
receive
(
:format
).
and_raise
(
StandardError
)
it
"highlights as that language"
do
result
=
filter
(
'<pre><code class="ruby">def fun end</code></pre>'
)
expect
(
result
.
to_html
).
to
eq
(
'<pre class="code highlight js-syntax-highlight ruby"><code><span class="k">def</span> <span class="nf">fun</span> <span class="k">end</span></code></pre>'
)
end
end
context
"when an invalid language is specified"
do
it
"highlights as plaintext"
do
result
=
filter
(
'<pre><code class="gnuplot">This is a test</code></pre>'
)
expect
(
result
.
to_html
).
to
eq
(
'<pre class="code highlight js-syntax-highlight plaintext"><code>This is a test</code></pre>'
)
end
end
context
"when Rouge formatting fails"
do
before
do
allow_any_instance_of
(
Rouge
::
Formatter
).
to
receive
(
:format
).
and_raise
(
StandardError
)
end
result
=
filter
(
'<pre><code>This is a test</code></pre>'
)
it
"highlights as plaintext"
do
expect
(
result
.
to_html
).
to
eq
(
'<pre>This is a test</pre>'
)
result
=
filter
(
'<pre><code class="ruby">This is a test</code></pre>'
)
expect
(
result
.
to_html
).
to
eq
(
'<pre class="code highlight"><code>This is a test</code></pre>'
)
end
end
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