FrontPage  Index  Search  Changes  Atom  PageRank  Login

CSSパーサー

CSSパーサー

CSS Parser

https://github.com/alexdunae/css_parser

日記を書いたことがある。

CSS Inlinerでも使っている。

CSSPool

http://csspool.rubyforge.org/

Cのライブラリーでlibcrocoっていうのがあるらしくて、それのバインディング。 SACっていうSAXのCSS版みたいな物をサポートしているらしい。

SASS

http://sass-lang.com/

CSSパーサーではないけど内部にCSSパーサーがあるのでそれが流用できるかも(調べ中)。

その他

ご存知Nokogiriもセレクター部分はパースできる。

使用例

sample.css

body {
  color: black;
}
body p.alert {
  color: yellow;
}
body p {
  color: blue;
}
p {
  color: red;
}

CSS Parser

require 'css_parser'

file = 'sample.css'
parser = CssParser::Parser.new
parser.load_file! file

tmp = []
parser.each_selector do |sel, decl, spec|
  tmp << {
    selector:    sel,
    declaration: decl,
    specificity: spec
  }
end
res = tmp.sort_by {|obj| obj[:specificity]}

require 'json'
puts res.to_json

CSSPool

require 'csspool'

file = 'sample.css'

tmp = []
doc = CSSPool.CSS File.read(file)
doc.rule_sets.each do |rs|
  rs.selectors.each do |sel|
    tmp << {
      selector: sel,
      declarations: sel.declarations,
      specificity: sel.specificity
    }
  end
end
res = tmp.sort_by {|obj| obj[:specificity]}

require 'json'
puts res.to_json

Sass

Under construction.

結構辛い。

Last modified:2011/12/08 02:27:40
Keyword(s):
References:
Referer | 327 | 28 | 16 | 4 | 3 | 3 | 3 | 3 | 3 | 3 | 2 | 2 | 2 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 |