From e1824aa101a2181fe7e5ce080582cfa188d19da8 Mon Sep 17 00:00:00 2001 From: "http://jneen.net/" Date: Wed, 15 Jun 2016 12:32:55 -0700 Subject: use the new token_lines interface to format lines --- lib/rouge/formatters/html_gitlab.rb | 81 +++++-------------------------------- 1 file changed, 11 insertions(+), 70 deletions(-) (limited to 'lib/rouge') diff --git a/lib/rouge/formatters/html_gitlab.rb b/lib/rouge/formatters/html_gitlab.rb index b9e3272e0b5..723bac574dd 100644 --- a/lib/rouge/formatters/html_gitlab.rb +++ b/lib/rouge/formatters/html_gitlab.rb @@ -2,83 +2,24 @@ require 'cgi' module Rouge module Formatters - class HTMLGitlab < Rouge::Formatter + class HTMLGitlab < Rouge::Formatters::HTML tag 'html_gitlab' # Creates a new Rouge::Formatter::HTMLGitlab instance. # - # [+cssclass+] CSS class for the wrapping
tag - # (default: 'highlight'). - # [+lineanchors+] If set to true the formatter will wrap each output - # line in an anchor tag with a name of L-linenumber. - # This allows easy linking to certain lines - # (default: false). - # [+lineanchorsid+] If lineanchors is true the name of the anchors can - # be changed with lineanchorsid to e.g. foo-linenumber - # (default: 'L'). - def initialize( - lineanchors: false, - lineanchorsid: 'L' - ) - @lineanchors = lineanchors - @lineanchorsid = lineanchorsid + # [+linenostart+] The line number for the first line (default: 1). + def initialize(linenostart: 1) + @linenostart = linenostart + @line_number = linenostart end - def render(tokens) - data = process_tokens(tokens) + def stream(tokens, &b) + token_lines(tokens) do |line| + yield %<> + line.each { |token, value| yield span(token, value) } + yield %<\n> - wrap_lines(data[:code]) - end - - alias_method :format, :render - - private - - def process_tokens(tokens) - rendered = [] - current_line = '' - - tokens.each do |tok, val| - # In the case of multi-line values (e.g. comments), we need to apply - # styling to each line since span elements are inline. - val.lines.each do |line| - stripped = line.chomp - current_line << span(tok, stripped) - - if line.end_with?("\n") - rendered << current_line - current_line = '' - end - end - end - - # Add leftover text - rendered << current_line if current_line.present? - - { code: rendered } - end - - def wrap_lines(lines) - if @lineanchors - lines = lines.each_with_index.map do |line, index| - number = index + @linenostart - - "#{line}" \ - '' - end - end - - lines.join("\n") - end - - def span(tok, val) - # http://stackoverflow.com/a/1600584/2587286 - val = CGI.escapeHTML(val) - - if tok.shortname.empty? - val - else - "#{val}" + @line_number += 1 end end end -- cgit v1.2.1