summaryrefslogtreecommitdiff
path: root/lib/coderay/encoders/html/css.rb
diff options
context:
space:
mode:
authorKornelius Kalnbach <murphy@rubychan.de>2013-08-31 16:09:34 +0200
committerKornelius Kalnbach <murphy@rubychan.de>2013-08-31 16:09:34 +0200
commit90cf0323db24d5fe2e326f9a413d85a49402f5a9 (patch)
treeff7062a85cb5bdd3504391cbd905bb8f0809afa6 /lib/coderay/encoders/html/css.rb
parent718c0ac901eef189a1dad36f57a78f36d9d0ba11 (diff)
downloadcoderay-90cf0323db24d5fe2e326f9a413d85a49402f5a9.tar.gz
cache escaped tokens, reduce HTML enc setup time
Diffstat (limited to 'lib/coderay/encoders/html/css.rb')
-rw-r--r--lib/coderay/encoders/html/css.rb46
1 files changed, 23 insertions, 23 deletions
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