Ruby:解析明文的结构化块

时间:2013-01-07 13:03:35

标签: css ruby parsing css-parsing

我正在为CSS文件编写一个简单的ruby解析器,我有点难以理解如何将CSS块(即选择器和紧随其后的花括号之间的所有内容)作为ruby对象。我可以表现出我黑暗而讨厌的魔法。

理想情况下,我想获得每个属性/值和选择器的ruby哈希值。有没有明确易懂的方法如何做到这一点?

2 个答案:

答案 0 :(得分:1)

正如评论中所提到的,除了有趣的练习外,自己做这件事很愚蠢。我将css文件读入一个字符串,然后用正则表达式扫描它。

Ruby - Convert File to String

http://www.ruby-doc.org/core-1.9.3/String.html#method-i-scan

用于测试正则表达式:http://rubular.com/

答案 1 :(得分:1)

每个人都应该编写解析器。

这应该让你开始......

require 'parslet'
require 'ostruct'
require 'pp'

class Parser < Parslet::Parser
  rule(:opencurl)   { str('{') }
  rule(:closecurl)  { str('}') }
  rule(:space)      { str(' ') }
  rule(:space?)     { space.maybe }
  rule(:comma)      { str(',') }
  rule(:semi)       { str(';') }
  rule(:colon)      { str(':') }
  rule(:eol)        { str("\r").maybe >> str("\n") }
  rule(:eol?)       { eol.maybe }
  rule(:indent?)    { str(" ").repeat(0) }
  rule(:ws?)        {indent? >> eol? >> indent?}

  rule(:value)      { (semi.absent? >> any).repeat(1).as(:value) } #cheating

  rule(:word)       { match['a-zA-Z0-9'].repeat(1) }
  rule(:property)   { (word >> (str("-") >> word).repeat(0)).as(:property) }
  rule(:setting)    { (indent? >> property >> colon >> indent? >> value >> semi  >> eol?) }
  rule(:body?)      { setting.repeat(0).as(:body) }
  rule(:block)      { opencurl >> ws? >> body? >> ws? >> closecurl >> ws? }
  rule(:selector_expression)   { (opencurl.absent? >> any).repeat(1) } #cheating
  rule(:scope)      { ws? >> selector_expression.as("selector") >> indent? >> block.as(:settings) }

  rule(:css?)       { (eol.repeat(0) >> scope).repeat(0) } 
  root(:css?)
end  

class MyTransform < Parslet::Transform
  rule(:property => simple(:p), :value => simple(:v)) { OpenStruct.new(p:p,v:v) }
  rule(:body => sequence(:b)) { b.each_with_object({}){|i,o| o[i.p] = i.v} }
end 

css = <<-css
  h2{ background-image: url(abc);}
  #bob { 
  background-image: url(abc);
  background-color: red;
}
css

par = Parser.new.parse(css)
tar = MyTransform.new.apply(par) 
pp tar

输出:

[{"selector"=>"h2", :settings=>{"background-image"=>"url(abc)"}},
 {"selector"=>"#bob ",
  :settings=>
   {"background-image"=>"url(abc)", "background-color"=>"red"}}]