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
honeybadger-4.5.3 vendor/bundle/ruby/2.6.0/gems/coderay-1.1.2/lib/coderay/encoders/html/css.rb
chatops-rpc-0.0.2 fixtures/chatops-controller-example/vendor/bundle/ruby/2.5.0/gems/coderay-1.1.2/lib/coderay/encoders/html/css.rb
chatops-rpc-0.0.1 fixtures/chatops-controller-example/vendor/bundle/ruby/2.5.0/gems/coderay-1.1.2/lib/coderay/encoders/html/css.rb
dadapush_client-1.0.1 vendor/bundle/ruby/2.3.0/gems/coderay-1.1.2/lib/coderay/encoders/html/css.rb
chess_engine-0.0.2 vendor/bundle/gems/coderay-1.1.2/lib/coderay/encoders/html/css.rb
chess_engine-0.0.1 vendor/bundle/gems/coderay-1.1.2/lib/coderay/encoders/html/css.rb
xaiml-0.1.3 vendor/bundle/ruby/2.5.0/gems/coderay-1.1.2/lib/coderay/encoders/html/css.rb
alimentos-alu0100945645-0.1.0 vendor/bundle/ruby/2.3.0/gems/coderay-1.1.2/lib/coderay/encoders/html/css.rb
alimentos-alu0100945645-1.0.0 vendor/bundle/ruby/2.3.0/gems/coderay-1.1.2/lib/coderay/encoders/html/css.rb
xaiml-0.1.2 vendor/bundle/ruby/2.5.0/gems/coderay-1.1.2/lib/coderay/encoders/html/css.rb
xaiml-0.1.1 vendor/bundle/ruby/2.5.0/gems/coderay-1.1.2/lib/coderay/encoders/html/css.rb
xaiml-0.1.0 vendor/bundle/ruby/2.5.0/gems/coderay-1.1.2/lib/coderay/encoders/html/css.rb
logstash-filter-device_detection-1.0.7-java vendor/bundle/jruby/1.9/gems/coderay-1.1.2/lib/coderay/encoders/html/css.rb
mrcooper-logstash-output-azuresearch-0.2.2 vendor/jruby/2.5.0/gems/coderay-1.1.2/lib/coderay/encoders/html/css.rb
monero_wallet_gen-0.1.0 vendor/bundle/ruby/2.3.0/gems/coderay-1.1.2/lib/coderay/encoders/html/css.rb
sb_prime_table-0.1.1 vendor/bundle/ruby/2.4.0/gems/coderay-1.1.2/lib/coderay/encoders/html/css.rb
sb_prime_table-0.1.0 vendor/bundle/ruby/2.4.0/gems/coderay-1.1.2/lib/coderay/encoders/html/css.rb
comiditaULL-0.1.1 vendor/bundle/ruby/2.3.0/gems/coderay-1.1.2/lib/coderay/encoders/html/css.rb
comidita_ull-0.1.1 vendor/bundle/ruby/2.3.0/gems/coderay-1.1.2/lib/coderay/encoders/html/css.rb
comidita_ull-0.1.0 vendor/bundle/ruby/2.3.0/gems/coderay-1.1.2/lib/coderay/encoders/html/css.rb