To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.

Statistics Download as Zip
| Branch: | Tag: | Revision:

root / .svn / pristine / 76 / 764f20578b33c5ef0c99adc3a136111ff27ce87f.svn-base @ 1297:0a574315af3e

History | View | Annotate | Download (1.53 KB)

1
module CodeRay
2
module Encoders
3

    
4
  class HTML
5
    class CSS  # :nodoc:
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 get_style 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
        # warn 'Style not found: %p' % [styles] if style.empty?
31
        return style
32
      end
33

    
34
    private
35

    
36
      CSS_CLASS_PATTERN = /
37
        (                    # $1 = selectors
38
          (?:
39
            (?: \s* \. [-\w]+ )+
40
            \s* ,?
41
          )+
42
        )
43
        \s* \{ \s*
44
        ( [^\}]+ )?          # $2 = style
45
        \s* \} \s*
46
      |
47
        ( [^\n]+ )           # $3 = error
48
      /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
            @classes[cl] ||= Hash.new
56
            @classes[cl][classes] = style.to_s.strip.delete(' ').chomp(';')
57
          end
58
        end
59
      end
60

    
61
    end
62
  end
63

    
64
end
65
end