sass-extractor 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/sass-extractor.rb +87 -0
  2. metadata +81 -0
@@ -0,0 +1,87 @@
1
+ require "fileutils"
2
+ require "sass"
3
+
4
+ class SassExtractor
5
+ def initialize(options)
6
+ @options = options
7
+ end
8
+
9
+ def get_rules(file_name)
10
+ Extractor.new(sass_tree_for_file(file_name)).get_rules(prefixes)
11
+ end
12
+
13
+ def sass_tree_for_file(file_name)
14
+ Sass::Engine.for_file(file_name, sass_options).to_tree
15
+ end
16
+
17
+ def sass_options; @options[:sass_options] end
18
+ def prefixes; @options[:prefixes] || [""] end
19
+
20
+ class Extractor
21
+ def initialize(tree)
22
+ @tree = tree
23
+ compile
24
+ end
25
+
26
+ def get_rules(prefixes)
27
+ Visitor.visit(@tree, prefixes)
28
+ end
29
+
30
+ def compile
31
+ check_nesting
32
+ @tree = Sass::Tree::Visitors::Perform.visit(@tree)
33
+ check_nesting
34
+ @tree, extends = Sass::Tree::Visitors::Cssize.visit(@tree)
35
+ @tree = @tree.do_extend(extends) unless extends.empty?
36
+ end
37
+
38
+ def check_nesting
39
+ Sass::Tree::Visitors::CheckNesting.visit(@tree)
40
+ end
41
+ end
42
+
43
+ class Visitor < Sass::Tree::Visitors::Base
44
+ def self.visit(root, prefixes)
45
+ new(prefixes).send(:visit, root)
46
+ end
47
+
48
+ def initialize(prefixes)
49
+ @prefixes = prefixes
50
+ end
51
+
52
+ def visit_root(node)
53
+ yield.compact
54
+ end
55
+
56
+ def visit_directive(node)
57
+ end
58
+
59
+ def visit_rule(node)
60
+ selector = node.resolved_rules.to_a.join.gsub(/\s+/, " ")
61
+ properties = extract_properties(node)
62
+ [selector, properties] unless properties == {}
63
+ end
64
+
65
+ def extract_properties(rule)
66
+ build_hash do |result|
67
+ each_property(rule) do |name, value|
68
+ result[name] = value if interesting? name
69
+ end
70
+ end
71
+ end
72
+
73
+ def build_hash
74
+ result = {}; yield result; result
75
+ end
76
+
77
+ def each_property(rule)
78
+ rule.children.each do |property|
79
+ yield property.resolved_name, property.resolved_value
80
+ end
81
+ end
82
+
83
+ def interesting?(name)
84
+ @prefixes.any? { |prefix| name.start_with? prefix }
85
+ end
86
+ end
87
+ end
metadata ADDED
@@ -0,0 +1,81 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sass-extractor
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Daniel Brockman
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-05-26 00:00:00 Z
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: sass
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ~>
27
+ - !ruby/object:Gem::Version
28
+ hash: 1
29
+ segments:
30
+ - 3
31
+ - 1
32
+ - 1
33
+ version: 3.1.1
34
+ type: :runtime
35
+ version_requirements: *id001
36
+ description:
37
+ email:
38
+ - daniel@gointeractive.se
39
+ executables: []
40
+
41
+ extensions: []
42
+
43
+ extra_rdoc_files: []
44
+
45
+ files:
46
+ - lib/sass-extractor.rb
47
+ homepage: http://github.com/dbrock/sass-extractor
48
+ licenses: []
49
+
50
+ post_install_message:
51
+ rdoc_options: []
52
+
53
+ require_paths:
54
+ - lib
55
+ required_ruby_version: !ruby/object:Gem::Requirement
56
+ none: false
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ hash: 3
61
+ segments:
62
+ - 0
63
+ version: "0"
64
+ required_rubygems_version: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ hash: 3
70
+ segments:
71
+ - 0
72
+ version: "0"
73
+ requirements: []
74
+
75
+ rubyforge_project:
76
+ rubygems_version: 1.8.2
77
+ signing_key:
78
+ specification_version: 3
79
+ summary: Extract the contents of Sass files.
80
+ test_files: []
81
+