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 ++++++++++++++++++++-------------------- 1 file changed, 23 insertions(+), 23 deletions(-) (limited to 'lib/coderay/encoders/html/css.rb') 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 -- cgit v1.2.1 From 0023deede4144b4d7e8aa90a6557a3df00b88edc Mon Sep 17 00:00:00 2001 From: Kornelius Kalnbach Date: Thu, 3 Oct 2013 13:42:59 +0200 Subject: don't break old API --- lib/coderay/encoders/html/css.rb | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) (limited to 'lib/coderay/encoders/html/css.rb') diff --git a/lib/coderay/encoders/html/css.rb b/lib/coderay/encoders/html/css.rb index 32470b4..e78d062 100644 --- a/lib/coderay/encoders/html/css.rb +++ b/lib/coderay/encoders/html/css.rb @@ -3,21 +3,22 @@ module Encoders class HTML class CSS # :nodoc: - def initialize style = :default - @style = style + def initialize style_name = :default + @style_name = style_name end - - def stylesheet - @stylesheet ||= CodeRay::Styles[@style] + + def style + @style ||= CodeRay::Styles[@style_name] end - + def css - [ - stylesheet::CSS_MAIN_STYLES, - stylesheet::TOKEN_COLORS.gsub(/^(?!$)/, '.CodeRay ') + @css ||= [ + style::CSS_MAIN_STYLES, + style::TOKEN_COLORS.gsub(/^(?!$)/, '.CodeRay ') ].join("\n") end - + alias stylesheet css + def get_style_for_css_classes css_classes cl = styles[css_classes.first] return '' unless cl @@ -29,7 +30,7 @@ module Encoders return style end - private + private CSS_CLASS_PATTERN = / ( # $1 = selectors @@ -46,7 +47,7 @@ module Encoders /mx def styles @styles ||= Hash.new.tap do |styles| - stylesheet::TOKEN_COLORS.scan CSS_CLASS_PATTERN do |selectors, style, error| + style::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]+/) -- cgit v1.2.1 From 94ced6235d43c21e035c39c9da9c94d987447eef Mon Sep 17 00:00:00 2001 From: Kornelius Kalnbach Date: Thu, 3 Oct 2013 13:46:39 +0200 Subject: even less API changes --- lib/coderay/encoders/html/css.rb | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'lib/coderay/encoders/html/css.rb') diff --git a/lib/coderay/encoders/html/css.rb b/lib/coderay/encoders/html/css.rb index e78d062..e06c486 100644 --- a/lib/coderay/encoders/html/css.rb +++ b/lib/coderay/encoders/html/css.rb @@ -11,13 +11,12 @@ module Encoders @style ||= CodeRay::Styles[@style_name] end - def css + def stylesheet @css ||= [ style::CSS_MAIN_STYLES, style::TOKEN_COLORS.gsub(/^(?!$)/, '.CodeRay ') ].join("\n") end - alias stylesheet css def get_style_for_css_classes css_classes cl = styles[css_classes.first] -- cgit v1.2.1