CSSパーサー
CSSパーサー
CSS Parser
https://github.com/alexdunae/css_parser
日記を書いたことがある。
CSS Inlinerでも使っている。
CSSPool
Cのライブラリーでlibcrocoっていうのがあるらしくて、それのバインディング。 SACっていうSAXのCSS版みたいな物をサポートしているらしい。
SASS
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.
結構辛い。
Referer | 350 | 28 | 17 | 4 | 4 | 3 | 3 | 3 | 3 | 3 | 2 | 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 | 1 |
Keyword(s):
References: