OSDN Git Service

add Redmine trunk rev 3089
[redminele/redminele.git] / redmine / vendor / plugins / coderay-0.7.6.227 / lib / coderay / encoders / html / css.rb
1 module CodeRay
2 module Encoders
3
4   class HTML
5     class CSS
6
7       attr :stylesheet
8
9       def CSS.load_stylesheet style = nil
10         CodeRay::Styles[style]
11       end
12
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
22
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
33
34     private
35
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
55
56     end
57   end
58
59 end
60 end
61
62 if $0 == __FILE__
63   require 'pp'
64   pp CodeRay::Encoders::HTML::CSS.new
65 end