Skip to content

Commit

Permalink
resolves #264 add line highlighting option
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
mojavelinux committed Aug 26, 2020
1 parent a9fdd44 commit 8275950
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 0 deletions.
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

0 comments on commit 8275950

Please sign in to comment.