Skip to content

Commit

Permalink
Improve functionality of HTMLLinewise formatter (#1156)
Browse files Browse the repository at this point in the history
This commit:

- provides customisable tags for wrapping lines (default is `<div>`); 
- delegates to stream method instead of span method on inner formatter;
  and
- preserves newlines.

It corrects the mistaken merge at 6f8cfdb.
  • Loading branch information
mojavelinux authored and pyrmont committed Jun 4, 2019
1 parent 7043f3c commit 7bd180f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 16 deletions.
17 changes: 6 additions & 11 deletions lib/rouge/formatters/html_linewise.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,18 @@ module Formatters
class HTMLLinewise < Formatter
def initialize(formatter, opts={})
@formatter = formatter
@tag_name = opts.fetch(:tag_name, 'div')
@class_format = opts.fetch(:class, 'line-%i')
end

def stream(tokens, &b)
token_lines(tokens) do |line|
yield "<div class=#{next_line_class}>"
line.each do |tok, val|
yield @formatter.span(tok, val)
end
yield '</div>'
lineno = 0
token_lines(tokens) do |line_tokens|
yield %(<#{@tag_name} class="#{sprintf @class_format, lineno += 1}">)
@formatter.stream(line_tokens) {|formatted| yield formatted }
yield %(\n</#{@tag_name}>)
end
end

def next_line_class
@lineno ||= 0
sprintf(@class_format, @lineno += 1).inspect
end
end
end
end
19 changes: 14 additions & 5 deletions spec/formatters/html_linewise_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,31 +12,40 @@
let(:input_stream) { [[Token['Name'], 'foo']] }

it 'formats' do
assert { output == %(<div class="line-1"><span class="n">foo</span></div>) }
assert { output == %(<div class="line-1"><span class="n">foo</span>\n</div>) }
end
end

describe 'final newlines' do
let(:input_stream) { [[Token['Text'], "foo\n"], [Token['Name'], "bar\n"]] }

it 'formats' do
assert { output == %(<div class="line-1">foo</div><div class="line-2"><span class="n">bar</span></div>) }
assert { output == %(<div class="line-1">foo\n</div><div class="line-2"><span class="n">bar</span>\n</div>) }
end
end

describe 'intermediate newlines' do
let(:input_stream) { [[Token['Name'], "foo\nbar"]] }

it 'formats' do
assert { output == %(<div class="line-1"><span class="n">foo</span></div><div class="line-2"><span class="n">bar</span></div>) }
assert { output == %(<div class="line-1"><span class="n">foo</span>\n</div><div class="line-2"><span class="n">bar</span>\n</div>) }
end
end

describe 'alternate tag name' do
let(:input_stream) { [[Token['Text'], "foo\n"], [Token['Name'], "bar\n"]] }
let(:options) { { tag_name: 'span' } }

it 'should use tag name specified by :tag_name option' do
assert { output == %(<span class="line-1">foo\n</span><span class="line-2"><span class="n">bar</span>\n</span>) }
end
end

describe 'inside html table formatter' do
let(:input_stream) { [[Token['Text'], "foo\n"], [Token['Name'], "bar\n"]] }

it 'formats' do
assert { Rouge::Formatters::HTMLTable.new(subject).format(input_stream) == %(<table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1\n2\n</pre></td><td class="rouge-code"><pre><div class="line-1">foo</div><div class="line-2"><span class="n">bar</span></div></pre></td></tr></tbody></table>) }
it 'should delegate to linewise formatter' do
assert { Rouge::Formatters::HTMLTable.new(subject).format(input_stream) == %(<table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1\n2\n</pre></td><td class="rouge-code"><pre><div class="line-1">foo\n</div><div class="line-2"><span class="n">bar</span>\n</div></pre></td></tr></tbody></table>) }
end
end
end

0 comments on commit 7bd180f

Please sign in to comment.