summaryrefslogtreecommitdiff
path: root/lib/coderay/encoders/html/css.rb
blob: e06c4869ed781c8f1d8f3c7aace089fe117af221 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
module CodeRay
module Encoders

  class HTML
    class CSS  # :nodoc:
      def initialize style_name = :default
        @style_name = style_name
      end

      def style
        @style ||= CodeRay::Styles[@style_name]
      end

      def stylesheet
        @css ||= [
          style::CSS_MAIN_STYLES,
          style::TOKEN_COLORS.gsub(/^(?!$)/, '.CodeRay ')
        ].join("\n")
      end

      def get_style_for_css_classes css_classes
        cl = styles[css_classes.first]
        return '' unless cl
        style = ''
        1.upto css_classes.size do |offset|
          break if style = cl[css_classes[offset .. -1]]
        end
        # warn 'Style not found: %p' % [styles] if style.empty?
        return style
      end

      private

      CSS_CLASS_PATTERN = /
        (                    # $1 = selectors
          (?:
            (?: \s* \. [-\w]+ )+
            \s* ,?
          )+
        )
        \s* \{ \s*
        ( [^\}]+ )?          # $2 = style
        \s* \} \s*
      |
        ( [^\n]+ )           # $3 = error
      /mx
      def styles
        @styles ||= Hash.new.tap do |styles|
          style::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

    end
  end

end
end