class Iro::Formatter::HTMLFormatter
Public Class Methods
format(source, highlight, prefix: '')
click to toggle source
# File lib/iro/formatter/html_formatter.rb, line 14 def self.format(source, highlight, prefix: '') formatter = self.new( source: source, highlight: highlight, prefix: prefix, ) formatter.format end
new(source:, highlight:, prefix:)
click to toggle source
# File lib/iro/formatter/html_formatter.rb, line 27 def initialize(source:, highlight:, prefix:) @source = source.each_line.map(&:chomp) @highlight = sort_highlight_by_position(highlight) @prefix = prefix end
sample_stylesheet()
click to toggle source
# File lib/iro/formatter/html_formatter.rb, line 23 def self.sample_stylesheet File.join(__dir__, 'sample.css') end
Public Instance Methods
format()
click to toggle source
# File lib/iro/formatter/html_formatter.rb, line 33 def format buf = [] @source.each.with_index do |line, lineno| highlights = pop_highlight(lineno) if highlights.empty? buf << CGI.escapeHTML(line) next end last_col = 0 highlighted_line = +'' highlights.each do |hi| highlighted_line << CGI.escapeHTML(line[last_col...hi.column]) last_col = hi.column + hi.length token = CGI.escapeHTML(line[(hi.column)...last_col]) highlighted_line << %Q!<span class="#{@prefix}#{hi.group}">#{token}</span>! end highlighted_line << line[last_col..-1] buf << highlighted_line end buf.join("\n") end
pop_highlight(lineno)
click to toggle source
# File lib/iro/formatter/html_formatter.rb, line 68 def pop_highlight(lineno) [].tap do |res| while @highlight.first&.line == lineno res << @highlight.shift end end end
sort_highlight_by_position(highlight)
click to toggle source
# File lib/iro/formatter/html_formatter.rb, line 58 def sort_highlight_by_position(highlight) highlight.flat_map do |group, positions| positions.map do |pos| [group, *pos] end end.sort_by do |pos| [pos.line, pos.column] end end