public
Rubygem
Fork of nex3/haml
Description: HTML Abstraction Markup Language - A Markup Haiku
Homepage: http://haml.hamptoncatlin.com
Clone URL: git://github.com/chriseppstein/haml.git
Search Repo:
Created css2sass. Still lacking a lot of functionality, like figuring out 
nesting and dealing with comments and @import. The basics are there, 
though.


git-svn-id: svn://hamptoncatlin.com/haml/trunk@476 
7063305b-7217-0410-af8c-cdc13e5119b9
nex3 (author)
Sun Apr 01 03:03:12 -0700 2007
commit  a25e1fbfdc6dc66e31c17eff3526c3a4f76c7e56
tree    414d56a68c7b5686d3f590a96e745bf73704fd88
parent  2a5337bff0177e6349adf9a1e14867aa1149c68a
...
 
 
 
 
 
 
 
...
1
2
3
4
5
6
7
0
@@ -1 +1,8 @@
0
+#!/usr/bin/env ruby
0
+
0
+require File.dirname(__FILE__) + '/../lib/haml'
0
+require 'haml/exec'
0
+
0
+opts = Haml::Exec::CSS2Sass.new(ARGV)
0
+opts.parse!
...
253
254
255
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
256
257
...
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
0
@@ -253,6 +253,37 @@
0
         output.write(::Haml::HTML.new(input).render)
0
       end
0
     end
0
+
0
+ # A class encapsulating executable functionality
0
+ # specific to the css2sass executable.
0
+ class CSS2Sass < Generic # :nodoc:
0
+ def initialize(args)
0
+ super
0
+
0
+ require 'sass/css'
0
+ end
0
+
0
+ def set_opts(opts)
0
+ opts.banner = <<END
0
+Usage: css2sass [options] (css file) (output file)
0
+
0
+Description: Transforms a CSS file into corresponding Sass code.
0
+
0
+Options:
0
+END
0
+
0
+ super
0
+ end
0
+
0
+ def process_result
0
+ super
0
+
0
+ input = @options[:input]
0
+ output = @options[:output]
0
+
0
+ output.write(::Sass::CSS.new(input).render)
0
+ end
0
+ end
0
   end
0
 end
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
0
@@ -1 +1,92 @@
0
+require File.dirname(__FILE__) + '/../sass'
0
+require 'sass/tree/node'
0
+require 'strscan'
0
+
0
+module Sass
0
+ # :stopdoc:
0
+ module Tree
0
+ class Node
0
+ def to_sass
0
+ result = ''
0
+
0
+ children.each do |child|
0
+ result << "#{child.to_sass(0)}\n"
0
+ end
0
+
0
+ result
0
+ end
0
+ end
0
+
0
+ class RuleNode
0
+ def to_sass(tabs)
0
+ str = "#{' ' * tabs}#{rule}\n"
0
+
0
+ children.each do |child|
0
+ str << "#{child.to_sass(tabs + 1)}\n"
0
+ end
0
+
0
+ str
0
+ end
0
+ end
0
+
0
+ class AttrNode
0
+ def to_sass(tabs)
0
+ "#{' ' * tabs}:#{name} #{value}"
0
+ end
0
+ end
0
+ end
0
+ # :startdoc:
0
+
0
+ # This class contains the functionality used in the +css2sass+ utility,
0
+ # namely converting CSS documents to Sass templates.
0
+ class CSS
0
+ # :stopdoc:
0
+
0
+ # The Regexp matching a CSS rule
0
+ RULE_RE = /\s*([^\{]+)\s*\{/
0
+
0
+ # The Regexp matching a CSS attribute
0
+ ATTR_RE = /\s*[^::\{\}]+\s*:\s*[^:;\{\}]+\s*;/
0
+
0
+ # :startdoc:
0
+
0
+ # Creates a new instance of Sass::CSS that will compile the given document
0
+ # to a Sass string when +render+ is called.
0
+ def initialize(template)
0
+ if template.is_a? IO
0
+ template = template.read
0
+ end
0
+
0
+ @template = StringScanner.new(template)
0
+ end
0
+
0
+ # Processes the document and returns the result as a string
0
+ # containing the CSS template.
0
+ def render
0
+ build_tree.to_sass
0
+ end
0
+
0
+ private
0
+
0
+ def build_tree
0
+ root = Tree::Node.new(nil)
0
+
0
+ while @template.scan(RULE_RE)
0
+ rule = Tree::RuleNode.new(@template[0][0...-1].strip, nil)
0
+ root << rule
0
+
0
+ while @template.scan(ATTR_RE)
0
+ attrs = @template[0][0...-1].split(':').map {|s| s.strip}
0
+ rule << Tree::AttrNode.new(attrs[0], attrs[1], nil)
0
+ end
0
+
0
+ if @template.scan(/\s*\}/).nil?
0
+ raise "Invalid CSS!"
0
+ end
0
+ end
0
+
0
+ root
0
+ end
0
+ end
0
+end

Comments

    No one has commented yet.