initial commit
[gitredmine.git] / vendor / plugins / coderay-0.7.6.227 / lib / coderay / encoders / html / .svn / text-base / css.rb.svn-base
blobd5776027fcb67773a3275fee0c030c23e048228f
1 module CodeRay
2 module Encoders
4   class HTML
5     class CSS
7       attr :stylesheet
9       def CSS.load_stylesheet style = nil
10         CodeRay::Styles[style]
11       end
13       def initialize style = :default
14         @classes = Hash.new
15         style = CSS.load_stylesheet style
16         @stylesheet = [
17           style::CSS_MAIN_STYLES,
18           style::TOKEN_COLORS.gsub(/^(?!$)/, '.CodeRay ')
19         ].join("\n")
20         parse style::TOKEN_COLORS
21       end
23       def [] *styles
24         cl = @classes[styles.first]
25         return '' unless cl
26         style = ''
27         1.upto(styles.size) do |offset|
28           break if style = cl[styles[offset .. -1]]
29         end
30         raise 'Style not found: %p' % [styles] if $DEBUG and style.empty?
31         return style
32       end
34     private
36       CSS_CLASS_PATTERN = /
37         ( (?:                # $1 = classes
38           \s* \. [-\w]+
39         )+ )
40         \s* \{ \s*
41         ( [^\}]+ )?          # $2 = style
42         \s* \} \s*
43       |
44         ( . )                # $3 = error
45       /mx
46       def parse stylesheet
47         stylesheet.scan CSS_CLASS_PATTERN do |classes, style, error|
48           raise "CSS parse error: '#{error.inspect}' not recognized" if error
49           styles = classes.scan(/[-\w]+/)
50           cl = styles.pop
51           @classes[cl] ||= Hash.new
52           @classes[cl][styles] = style.to_s.strip
53         end
54       end
56     end
57   end
59 end
60 end
62 if $0 == __FILE__
63   require 'pp'
64   pp CodeRay::Encoders::HTML::CSS.new
65 end