@@ -3,25 +3,23 @@ module Encoders
3
3
4
4
class HTML
5
5
class CSS # :nodoc:
6
-
7
- attr :stylesheet
8
-
9
- def CSS . load_stylesheet style = nil
10
- CodeRay ::Styles [ style ]
11
- end
12
-
13
6
def initialize style = :default
14
- @styles = Hash . new
15
- style = CSS . load_stylesheet style
16
- @stylesheet = [
17
- style ::CSS_MAIN_STYLES ,
18
- style ::TOKEN_COLORS . gsub ( /^(?!$)/ , '.CodeRay ' )
7
+ @style = style
8
+ end
9
+
10
+ def stylesheet
11
+ @stylesheet ||= CodeRay ::Styles [ @style ]
12
+ end
13
+
14
+ def css
15
+ [
16
+ stylesheet ::CSS_MAIN_STYLES ,
17
+ stylesheet ::TOKEN_COLORS . gsub ( /^(?!$)/ , '.CodeRay ' )
19
18
] . join ( "\n " )
20
- parse style ::TOKEN_COLORS
21
19
end
22
-
20
+
23
21
def get_style_for_css_classes css_classes
24
- cl = @ styles[ css_classes . first ]
22
+ cl = styles [ css_classes . first ]
25
23
return '' unless cl
26
24
style = ''
27
25
1 . upto css_classes . size do |offset |
@@ -46,14 +44,16 @@ def get_style_for_css_classes css_classes
46
44
|
47
45
( [^\n ]+ ) # $3 = error
48
46
/mx
49
- def parse stylesheet
50
- stylesheet . scan CSS_CLASS_PATTERN do |selectors , style , error |
51
- raise "CSS parse error: '#{ error . inspect } ' not recognized" if error
52
- for selector in selectors . split ( ',' )
53
- classes = selector . scan ( /[-\w ]+/ )
54
- cl = classes . pop
55
- @styles [ cl ] ||= Hash . new
56
- @styles [ cl ] [ classes ] = style . to_s . strip . delete ( ' ' ) . chomp ( ';' )
47
+ def styles
48
+ @styles ||= Hash . new . tap do |styles |
49
+ stylesheet ::TOKEN_COLORS . scan CSS_CLASS_PATTERN do |selectors , style , error |
50
+ raise "CSS parse error: '#{ error . inspect } ' not recognized" if error
51
+ for selector in selectors . split ( ',' )
52
+ classes = selector . scan ( /[-\w ]+/ )
53
+ cl = classes . pop
54
+ styles [ cl ] ||= Hash . new
55
+ styles [ cl ] [ classes ] = style . to_s . strip . delete ( ' ' ) . chomp ( ';' )
56
+ end
57
57
end
58
58
end
59
59
end
0 commit comments