From 90cf0323db24d5fe2e326f9a413d85a49402f5a9 Mon Sep 17 00:00:00 2001 From: Kornelius Kalnbach Date: Sat, 31 Aug 2013 16:09:34 +0200 Subject: cache escaped tokens, reduce HTML enc setup time --- lib/coderay/encoders/html/css.rb | 46 ++++++++++++++++++------------------- lib/coderay/encoders/html/output.rb | 2 +- 2 files changed, 24 insertions(+), 24 deletions(-) (limited to 'lib/coderay/encoders/html') diff --git a/lib/coderay/encoders/html/css.rb b/lib/coderay/encoders/html/css.rb index 164d7f8..32470b4 100644 --- a/lib/coderay/encoders/html/css.rb +++ b/lib/coderay/encoders/html/css.rb @@ -3,25 +3,23 @@ module Encoders class HTML class CSS # :nodoc: - - attr :stylesheet - - def CSS.load_stylesheet style = nil - CodeRay::Styles[style] - end - def initialize style = :default - @styles = Hash.new - style = CSS.load_stylesheet style - @stylesheet = [ - style::CSS_MAIN_STYLES, - style::TOKEN_COLORS.gsub(/^(?!$)/, '.CodeRay ') + @style = style + end + + def stylesheet + @stylesheet ||= CodeRay::Styles[@style] + end + + def css + [ + stylesheet::CSS_MAIN_STYLES, + stylesheet::TOKEN_COLORS.gsub(/^(?!$)/, '.CodeRay ') ].join("\n") - parse style::TOKEN_COLORS end - + def get_style_for_css_classes css_classes - cl = @styles[css_classes.first] + cl = styles[css_classes.first] return '' unless cl style = '' 1.upto css_classes.size do |offset| @@ -46,14 +44,16 @@ module Encoders | ( [^\n]+ ) # $3 = error /mx - def parse stylesheet - stylesheet.scan CSS_CLASS_PATTERN do |selectors, style, error| - raise "CSS parse error: '#{error.inspect}' not recognized" if error - for selector in selectors.split(',') - classes = selector.scan(/[-\w]+/) - cl = classes.pop - @styles[cl] ||= Hash.new - @styles[cl][classes] = style.to_s.strip.delete(' ').chomp(';') + def styles + @styles ||= Hash.new.tap do |styles| + stylesheet::TOKEN_COLORS.scan CSS_CLASS_PATTERN do |selectors, style, error| + raise "CSS parse error: '#{error.inspect}' not recognized" if error + for selector in selectors.split(',') + classes = selector.scan(/[-\w]+/) + cl = classes.pop + styles[cl] ||= Hash.new + styles[cl][classes] = style.to_s.strip.delete(' ').chomp(';') + end end end end diff --git a/lib/coderay/encoders/html/output.rb b/lib/coderay/encoders/html/output.rb index de6f6ea..d3b676c 100644 --- a/lib/coderay/encoders/html/output.rb +++ b/lib/coderay/encoders/html/output.rb @@ -24,7 +24,7 @@ module Encoders end def make_stylesheet css, in_tag = false # :nodoc: - sheet = css.stylesheet + sheet = css.css sheet = <<-'CSS' if in_tag