Sha256: 242f7a3015ecf1de1d7db03fdf5bfc1ca3ed36a731157414f0228fe445d407e5

Contents?: true

Size: 1.56 KB

Versions: 6632

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,632 entries across 6,620 versions & 69 rubygems

Version Path
mux_ruby-3.20.0 vendor/bundle/ruby/3.2.0/gems/coderay-1.1.3/lib/coderay/encoders/html/css.rb
cybrid_api_bank_ruby-0.123.12 vendor/bundle/ruby/3.3.0/gems/coderay-1.1.3/lib/coderay/encoders/html/css.rb
cybrid_api_organization_ruby-0.123.12 vendor/bundle/ruby/3.3.0/gems/coderay-1.1.3/lib/coderay/encoders/html/css.rb
cybrid_api_id_ruby-0.123.12 vendor/bundle/ruby/3.3.0/gems/coderay-1.1.3/lib/coderay/encoders/html/css.rb
cybrid_api_bank_ruby-0.123.11 vendor/bundle/ruby/3.3.0/gems/coderay-1.1.3/lib/coderay/encoders/html/css.rb
cybrid_api_organization_ruby-0.123.11 vendor/bundle/ruby/3.3.0/gems/coderay-1.1.3/lib/coderay/encoders/html/css.rb
cybrid_api_id_ruby-0.123.11 vendor/bundle/ruby/3.3.0/gems/coderay-1.1.3/lib/coderay/encoders/html/css.rb
cybrid_api_id_ruby-0.123.10 vendor/bundle/ruby/3.3.0/gems/coderay-1.1.3/lib/coderay/encoders/html/css.rb
cybrid_api_bank_ruby-0.123.10 vendor/bundle/ruby/3.3.0/gems/coderay-1.1.3/lib/coderay/encoders/html/css.rb
cybrid_api_organization_ruby-0.123.10 vendor/bundle/ruby/3.3.0/gems/coderay-1.1.3/lib/coderay/encoders/html/css.rb
cybrid_api_organization_ruby-0.123.7 vendor/bundle/ruby/3.3.0/gems/coderay-1.1.3/lib/coderay/encoders/html/css.rb
cybrid_api_id_ruby-0.123.7 vendor/bundle/ruby/3.3.0/gems/coderay-1.1.3/lib/coderay/encoders/html/css.rb
cybrid_api_bank_ruby-0.123.7 vendor/bundle/ruby/3.3.0/gems/coderay-1.1.3/lib/coderay/encoders/html/css.rb
ory-client-1.15.12 vendor/bundle/ruby/3.1.0/gems/coderay-1.1.3/lib/coderay/encoders/html/css.rb
cybrid_api_bank_ruby-0.123.4 vendor/bundle/ruby/3.3.0/gems/coderay-1.1.3/lib/coderay/encoders/html/css.rb
cybrid_api_id_ruby-0.123.4 vendor/bundle/ruby/3.3.0/gems/coderay-1.1.3/lib/coderay/encoders/html/css.rb
cybrid_api_organization_ruby-0.123.4 vendor/bundle/ruby/3.3.0/gems/coderay-1.1.3/lib/coderay/encoders/html/css.rb
cybrid_api_bank_ruby-0.123.3 vendor/bundle/ruby/3.3.0/gems/coderay-1.1.3/lib/coderay/encoders/html/css.rb
cybrid_api_organization_ruby-0.123.3 vendor/bundle/ruby/3.3.0/gems/coderay-1.1.3/lib/coderay/encoders/html/css.rb
cybrid_api_id_ruby-0.123.3 vendor/bundle/ruby/3.3.0/gems/coderay-1.1.3/lib/coderay/encoders/html/css.rb