Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

resolves #264 add line highlighting option #1426

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ The built-in formatters are:
your code into lines, each contained in its own div. The `class` option will
be used to add a class name to the div, given the line number.

* `Rouge::Formatters::HTMLLineHighlighter.new(formatter, highlight_lines: [3, 5])`
will split your code into lines and wrap the lines specified by the
`highlight_lines` option in a span with a class name specified by the
`highlight_line_class` option (default: `hll`).

* `Rouge::Formatters::HTMLLineTable.new(formatter, opts={})` will output an HTML
table containing numbered lines, each contained in its own table-row. Options
are:
Expand Down
1 change: 1 addition & 0 deletions lib/rouge.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ def load_lexers
Rouge.load_file 'formatters/html_pygments'
Rouge.load_file 'formatters/html_legacy'
Rouge.load_file 'formatters/html_linewise'
Rouge.load_file 'formatters/html_line_highlighter'
Rouge.load_file 'formatters/html_line_table'
Rouge.load_file 'formatters/html_inline'
Rouge.load_file 'formatters/terminal256'
Expand Down
26 changes: 26 additions & 0 deletions lib/rouge/formatters/html_line_highlighter.rb
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
28 changes: 28 additions & 0 deletions spec/formatters/html_line_highlighter_spec.rb
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