-
Notifications
You must be signed in to change notification settings - Fork 745
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- add line highlighting option to HTMLLinewise formatter - add dedicated formatter to add line highlighting (only marks highlighted lines)
- Loading branch information
1 parent
334152e
commit d7b60c0
Showing
4 changed files
with
60 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# -*- coding: utf-8 -*- # | ||
# frozen_string_literal: true | ||
|
||
module Rouge | ||
module Formatters | ||
class HTMLLineHighlighter < Formatter | ||
tag 'html_line_highlighter' | ||
|
||
def initialize(delegate, opts = {}) | ||
@delegate = delegate | ||
@highlight_line_class = opts.fetch(:highlight_line_class, 'hll') | ||
@highlight_lines = opts[:highlight_lines] || [] | ||
end | ||
|
||
def stream(tokens) | ||
lineno = 0 | ||
token_lines(tokens) do |tokens_in_line| | ||
lineno += 1 | ||
line = %(#{@delegate.format(tokens_in_line)}\n) | ||
line = %(<span class="#{@highlight_line_class}">#{line}</span>) if @highlight_lines.include? lineno | ||
yield line | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# -*- coding: utf-8 -*- # | ||
# frozen_string_literal: true | ||
|
||
describe Rouge::Formatters::HTMLLineHighlighter do | ||
let(:subject) { Rouge::Formatters::HTMLLineHighlighter.new(formatter, options) } | ||
let(:formatter) { Rouge::Formatters::HTML.new } | ||
|
||
let(:options) { {} } | ||
let(:output) { subject.format(input_stream) } | ||
|
||
describe 'highlight lines' do | ||
let(:input_stream) { [[Token['Text'], "foo\n"], [Token['Name'], "bar\n"]] } | ||
let(:options) { { highlight_lines: [2] } } | ||
|
||
it 'should add highlight line class to lines specified by :highlight_lines option' do | ||
assert { output == %(foo\n<span class="hll"><span class="n">bar</span>\n</span>) } | ||
end | ||
end | ||
|
||
describe 'configure highlight line class' do | ||
let(:input_stream) { [[Token['Text'], "foo\n"], [Token['Name'], "bar\n"]] } | ||
let(:options) { { highlight_lines: [1, 2], highlight_line_class: 'hiline' } } | ||
|
||
it 'should add highlight line class to lines specified by :highlight_lines option' do | ||
assert { output == %(<span class="hiline">foo\n</span><span class="hiline"><span class="n">bar</span>\n</span>) } | ||
end | ||
end | ||
end |