Sha256: 939c6cd6dc33f474a06c2647c4732e853c212dda3394cd6d1a7a9e6bf4489da7

Contents?: true

Size: 894 Bytes

Versions: 2

Compression:

Stored size: 894 Bytes

Contents

class UnderOs::Parser::CSS
  def parse(style)
    style = style.gsub(/\/\*[\s\S]+?\*\//, '').strip

    {}.tap do |result|
      style.scan(/(\A|\})([a-z0-9_\-\.\s#:,]+)\{([^}]+)/).map do |rule|
        values = parse_styles(rule[2])

        rule[1].split(',').each do |css_rule|
          result[css_rule.gsub(/\s+/, ' ').strip] = values
        end
      end
    end
  end

  def parse_styles(styles)
    {}.tap do |hash|
      styles.scan(/([a-z\-]+)\s*:\s*([^;]+)\s*/).each do |param|
        hash.merge! normalized_values(param[0], param[1])
      end
    end
  end

  def normalized_values(key, value)
    key   = key.camelize.to_sym
    value = value.strip.gsub(/px$/, '')
    value = value.gsub(/^('|")(.*?)\1$/, '\2')
    value = value.to_f if value =~ /^[\-\d\.]+$/

    if key == :background && value =~ /^[\S]+$/
      key = :backgroundColor
    end


    {key => value}
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
under-os-1.1.0 lib/under_os/parser/css.rb
under-os-1.0.0 lib/under_os/parser/css.rb