Sha256: 242f7a3015ecf1de1d7db03fdf5bfc1ca3ed36a731157414f0228fe445d407e5

Contents?: true

Size: 1.56 KB

Versions: 6915

Compression:

Stored size: 1.56 KB

Contents

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
        @styles = 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_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 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
            @styles[cl] ||= Hash.new
            @styles[cl][classes] = style.to_s.strip.delete(' ').chomp(';')
          end
        end
      end

    end
  end

end
end

Version data entries

6,915 entries across 6,903 versions & 70 rubygems

Version Path
logstash-filter-base64-1.0.4 vendor/bundle/jruby/2.2.0/gems/coderay-1.1.1/lib/coderay/encoders/html/css.rb
logstash-filter-base64-1.0.3 vendor/bundle/jruby/2.2.0/gems/coderay-1.1.1/lib/coderay/encoders/html/css.rb
logstash-filter-delta-1.0.0 vendor/bundle/jruby/2.2.0/gems/coderay-1.1.1/lib/coderay/encoders/html/css.rb
logstash-filter-base64-1.0.2 vendor/bundle/jruby/2.3.0/gems/coderay-1.1.1/lib/coderay/encoders/html/css.rb
logstash-filter-base64-1.0.1 vendor/bundle/jruby/2.3.0/gems/coderay-1.1.1/lib/coderay/encoders/html/css.rb
logstash-filter-base64-1.0.0 vendor/bundle/jruby/2.3.0/gems/coderay-1.1.1/lib/coderay/encoders/html/css.rb
able-neo4j-1.0.0 vendor/bundle/jruby/1.9/gems/coderay-1.1.0/lib/coderay/encoders/html/css.rb
coderay-1.1.1 lib/coderay/encoders/html/css.rb
coderay-1.1.1.rc1 lib/coderay/encoders/html/css.rb
honeybadger-2.4.0 vendor/gems/ruby/2.2.0/gems/coderay-1.1.0/lib/coderay/encoders/html/css.rb
honeybadger-2.4.0 vendor/gems/ruby/2.1.0/gems/coderay-1.1.0/lib/coderay/encoders/html/css.rb
honeybadger-2.4.0 vendor/gems/ruby/1.9.1/gems/coderay-1.1.0/lib/coderay/encoders/html/css.rb
logstash-input-beats-2.0.2 vendor/jruby/1.9/gems/logstash-codec-json-2.0.3/vendor/gems/coderay-1.1.0/lib/coderay/encoders/html/css.rb
logstash-input-beats-2.0.2 vendor/jruby/1.9/gems/coderay-1.1.0/lib/coderay/encoders/html/css.rb
logstash-codec-json-2.0.3 vendor/gems/coderay-1.1.0/lib/coderay/encoders/html/css.rb
logstash-input-beats-0.9.2 vendor/jruby/1.9/gems/coderay-1.1.0/lib/coderay/encoders/html/css.rb
logstash-input-beats-0.9.1 vendor/jruby/1.9/gems/coderay-1.1.0/lib/coderay/encoders/html/css.rb
mastermind_adeybee-0.1.4 vendor/bundle/ruby/2.2.0/gems/coderay-1.1.0/lib/coderay/encoders/html/css.rb
sc_core-0.0.7 test/dummy/vendor/bundle/ruby/2.2.0/gems/coderay-1.1.0/lib/coderay/encoders/html/css.rb
mastermind_adeybee-0.1.3 vendor/bundle/ruby/2.2.0/gems/coderay-1.1.0/lib/coderay/encoders/html/css.rb