lib/lookbook/code_formatter.rb in lookbook-0.7.2.beta.0 vs lib/lookbook/code_formatter.rb in lookbook-0.7.2.beta.1
- old
+ new
@@ -3,11 +3,12 @@
module Lookbook
module CodeFormatter
class << self
def highlight(source, language, opts = {})
- source&.gsub!(">", "<")&.gsub!("<", ">")
+ source&.strip! unless opts[:strip] == false
+ source&.gsub!(">", ">")&.gsub!("<", "<")
language ||= "ruby"
formatter = Formatter.new(opts)
lexer = Rouge::Lexer.find(language.to_s) || Rouge::Lexer.find("plaintext")
formatter.format(lexer.lex(source)).html_safe
end
@@ -21,15 +22,17 @@
end
class Formatter < Rouge::Formatters::HTML
def initialize(opts = {})
@opts = opts
+ @highlight_lines = opts[:highlight_lines].to_a || []
+ @start_line = opts[:start_line] || 0
end
def stream(tokens, &block)
token_lines(tokens).each_with_index do |line_tokens, i|
- yield "<div class='line'>"
- yield "<span class='line-number'>#{i}</span>" if @opts[:line_numbers]
+ yield "<div class='line #{"highlighted-line" if @highlight_lines.include?(i + 1)}'>"
+ yield "<span class='line-number'>#{@start_line + i}</span>" if @opts[:line_numbers]
yield "<span class='line-content'>"
line_tokens.each do |token, value|
yield span(token, value)
end
yield "</span>"