lib/rouge/formatters/html.rb in rouge-0.4.0 vs lib/rouge/formatters/html.rb in rouge-0.5.0

- old
+ new

@@ -20,13 +20,13 @@ # useful for formats in which stylesheets are not available. # # Content will be wrapped in a `<pre>` tag with the given # `:css_class` unless `:wrap` is set to `false`. def initialize(opts={}) - @css_class = opts[:css_class] || 'highlight' - @line_numbers = opts.fetch(:line_numbers) { false } - @inline_theme = opts.fetch(:inline_theme) { nil } + @css_class = opts.fetch(:css_class, 'highlight') + @line_numbers = opts.fetch(:line_numbers, false) + @inline_theme = opts.fetch(:inline_theme, nil) @wrap = opts.fetch(:wrap, true) end # @yield the html output. def stream(tokens, &b) @@ -46,19 +46,24 @@ yield '</pre>' if @wrap end def stream_tableized(tokens, &b) num_lines = 0 - code = '' + last_val = '' + formatted = '' tokens.each do |tok, val| + last_val = val num_lines += val.scan(/\n/).size - span(tok, val) { |str| code << str } + span(tok, val) { |str| formatted << str } end - # add an extra line number for non-newline-terminated strings - num_lines += 1 if code[-1] != "\n" + # add an extra line for non-newline-terminated strings + if last_val[-1] != "\n" + num_lines += 1 + span(Token::Tokens::Text::Whitespace, "\n") { |str| formatted << str } + end # generate a string of newline-separated line numbers for the gutter numbers = num_lines.times.map do |x| %<<div class="lineno">#{x+1}</div>> end.join @@ -70,10 +75,10 @@ yield '<td class="gutter gl">' yield numbers yield '</td>' yield '<td class="code">' - yield code + yield formatted yield '</td>' yield '</tr></tbody></table>' yield '</pre>' if @wrap end