view .svn/pristine/76/764f20578b33c5ef0c99adc3a136111ff27ce87f.svn-base @ 1539:22d57b0e0a77 live

OK, this script works now, but it should be using the API
author Chris Cannam <chris.cannam@soundsoftware.ac.uk>
date Thu, 21 May 2015 17:31:06 +0100
parents cbb26bc654de
children
line wrap: on
line source
module CodeRay
module Encoders

  class HTML
    class CSS  # :nodoc:

      attr :stylesheet

      def CSS.load_stylesheet style = nil
        CodeRay::Styles[style]
      end

      def initialize style = :default
        @classes = Hash.new
        style = CSS.load_stylesheet style
        @stylesheet = [
          style::CSS_MAIN_STYLES,
          style::TOKEN_COLORS.gsub(/^(?!$)/, '.CodeRay ')
        ].join("\n")
        parse style::TOKEN_COLORS
      end

      def get_style styles
        cl = @classes[styles.first]
        return '' unless cl
        style = ''
        1.upto styles.size do |offset|
          break if style = cl[styles[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 parse stylesheet
        stylesheet.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
            @classes[cl] ||= Hash.new
            @classes[cl][classes] = style.to_s.strip.delete(' ').chomp(';')
          end
        end
      end

    end
  end

end
end