Skip to content

Commit 815ca89

Browse files
authored
Merge pull request #325 from jch/syntax-highlight-filter/unused-code-and-docs
Improves syntax highlight filter
2 parents e4c9782 + c594b66 commit 815ca89

File tree

2 files changed

+12
-7
lines changed

2 files changed

+12
-7
lines changed

lib/html/pipeline/syntax_highlight_filter.rb

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,15 @@
44

55
module HTML
66
class Pipeline
7-
# HTML Filter that syntax highlights code blocks wrapped
8-
# in <pre lang="...">.
7+
# HTML Filter that syntax highlights text inside code blocks.
8+
#
9+
# Context options:
10+
#
11+
# :highlight => String represents the language to pick lexer. Defaults to empty string.
12+
# :scope => String represents the class attribute adds to pre element after.
13+
# Defaults to "highlight highlight-css" if highlights a css code block.
14+
#
15+
# This filter does not write any additional information to the context hash.
916
class SyntaxHighlightFilter < Filter
1017
def initialize(*args)
1118
super(*args)
@@ -23,11 +30,8 @@ def call
2330
next if html.nil?
2431

2532
node.inner_html = html
26-
klass = node['class']
27-
scope = context[:scope] || "highlight-#{lang}"
28-
klass = [klass, scope].compact.join ' '
29-
30-
node['class'] = klass
33+
scope = context.fetch(:scope) { 'highlight' }
34+
node['class'] = "#{scope} #{scope}-#{lang}"
3135
end
3236
doc
3337
end

test/html/pipeline/syntax_highlight_filter_test.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ def test_highlight_default
1111
'<pre>hello</pre>', highlight: 'coffeescript'
1212

1313
doc = filter.call
14+
assert !doc.css('.highlight').empty?
1415
assert !doc.css('.highlight-coffeescript').empty?
1516
end
1617

0 commit comments

Comments
 (0)