2013-08-07 71 views
6

我想以編程方式解析.scss文件以生成在該文件中使用的選擇器的平面列表,主要是作爲一些靜態代碼分析的基礎。如何使用SASS解析.scss文件中所有CSS選擇器的列表?

在SASS條款中,我正在尋找一種方法來獲取給定.scss文件的所有Sass::Tree::RuleNode的列表。

到目前爲止,我正在使用Sass::Engine.for_file來創建一棵樹。然後,根據文檔,爲了在單個節點上使用Sass::Tree::RuleNode.resolved_rules,我必須使用Tree::Visitors::Cssize。但是在那裏有一個(可能很簡單)的錯誤。

require 'sass' 

sass_engine = Sass::Engine.for_file('files/examples.scss',{ 
    :style => :compact, 
    :load_paths => ['files'], 
    :syntax => :scss, 
}) 

tree = sass_engine.to_tree 

Sass::Tree::Visitors::Cssize.visit(tree) 

tree.each do |node| 
    if node.is_a? Sass::Tree::RuleNode 
    puts node.resolved_rules 
    end 
end 

這是錯誤和堆棧跟蹤我越來越:

ruby-1.9.3-p362/gems/sass-3.2.10/lib/sass/tree/visitors/cssize.rb:186:in `visit_prop': undefined method `empty?' for nil:NilClass (NoMethodError) 
    ruby-1.9.3-p362/gems/sass-3.2.10/lib/sass/tree/visitors/base.rb:37:in `visit' 
    ruby-1.9.3-p362/gems/sass-3.2.10/lib/sass/tree/visitors/cssize.rb:21:in `visit' 
    ruby-1.9.3-p362/gems/sass-3.2.10/lib/sass/tree/visitors/base.rb:53:in `block in visit_children' 
    ruby-1.9.3-p362/gems/sass-3.2.10/lib/sass/tree/visitors/base.rb:53:in `map' 
    ruby-1.9.3-p362/gems/sass-3.2.10/lib/sass/tree/visitors/base.rb:53:in `visit_children' 
    ruby-1.9.3-p362/gems/sass-3.2.10/lib/sass/tree/visitors/cssize.rb:30:in `block in visit_children' 
    ruby-1.9.3-p362/gems/sass-3.2.10/lib/sass/tree/visitors/cssize.rb:52:in `with_parent' 
    ruby-1.9.3-p362/gems/sass-3.2.10/lib/sass/tree/visitors/cssize.rb:29:in `visit_children' 
    ruby-1.9.3-p362/gems/sass-3.2.10/lib/sass/tree/visitors/base.rb:37:in `block in visit' 
    ruby-1.9.3-p362/gems/sass-3.2.10/lib/sass/tree/visitors/cssize.rb:201:in `visit_rule' 
    ruby-1.9.3-p362/gems/sass-3.2.10/lib/sass/tree/visitors/base.rb:37:in `visit' 
    ruby-1.9.3-p362/gems/sass-3.2.10/lib/sass/tree/visitors/cssize.rb:21:in `visit' 
    ruby-1.9.3-p362/gems/sass-3.2.10/lib/sass/tree/visitors/base.rb:53:in `block in visit_children' 
    ruby-1.9.3-p362/gems/sass-3.2.10/lib/sass/tree/visitors/base.rb:53:in `map' 
    ruby-1.9.3-p362/gems/sass-3.2.10/lib/sass/tree/visitors/base.rb:53:in `visit_children' 
    ruby-1.9.3-p362/gems/sass-3.2.10/lib/sass/tree/visitors/cssize.rb:30:in `block in visit_children' 
    ruby-1.9.3-p362/gems/sass-3.2.10/lib/sass/tree/visitors/cssize.rb:52:in `with_parent' 
    ruby-1.9.3-p362/gems/sass-3.2.10/lib/sass/tree/visitors/cssize.rb:29:in `visit_children' 
    ruby-1.9.3-p362/gems/sass-3.2.10/lib/sass/tree/visitors/base.rb:37:in `block in visit' 
    ruby-1.9.3-p362/gems/sass-3.2.10/lib/sass/tree/visitors/cssize.rb:65:in `visit_root' 
    ruby-1.9.3-p362/gems/sass-3.2.10/lib/sass/tree/visitors/base.rb:37:in `visit' 
    ruby-1.9.3-p362/gems/sass-3.2.10/lib/sass/tree/visitors/cssize.rb:21:in `visit' 
    ruby-1.9.3-p362/gems/sass-3.2.10/lib/sass/tree/visitors/base.rb:24:in `visit' 
    ruby-1.9.3-p362/gems/sass-3.2.10/lib/sass/tree/visitors/cssize.rb:6:in `visit' 

非常感謝提前。

+0

也許堆棧跟蹤將幫助別人來幫助你。 :) –

+0

增加了stacktrace。 – timme

回答

3

我不是太熟悉的薩斯寶石代碼庫,但能夠按照從上海社會科學院寶石的sass/tree/root_node.rb文件Cssize類,它具有的代碼示例得到這個工作(至少沒有崩潰,):

# Runs the dynamic Sass code *and* computes the CSS for the tree. 
    # @see #to_s 
    def render 
    Visitors::CheckNesting.visit(self) 
    result = Visitors::Perform.visit(self) 
    Visitors::CheckNesting.visit(result) # Check again to validate mixins 
    result, extends = Visitors::Cssize.visit(result) 
    Visitors::Extend.visit(result, extends) 
    result.to_s 
    end 

考慮這個代碼片斷並將其應用到你的腳本,我得到:

#!/usr/bin/env ruby 

require 'sass' 

sass_engine = Sass::Engine.for_file('files/examples.scss',{ 
    :style => :compact, 
    :load_paths => ['files'], 
    :syntax => :scss, 
}) 

tree = sass_engine.to_tree 

Sass::Tree::Visitors::CheckNesting.visit(tree) 
result = Sass::Tree::Visitors::Perform.visit(tree) 
Sass::Tree::Visitors::CheckNesting.visit(result) 
result, extends = Sass::Tree::Visitors::Cssize.visit(result) 
Sass::Tree::Visitors::Extend.visit(result, extends) 

result.each do |node| 
    if node.is_a? Sass::Tree::RuleNode 
    puts node.resolved_rules 
    end 
end 
相關問題