public
Description: HTML Abstraction Markup Language - A Markup Haiku
Homepage: http://haml.hamptoncatlin.com
Clone URL: git://github.com/nex3/haml.git
haml / lib / sass / css.rb
100644 379 lines (330 sloc) 8.252 kb
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
require File.dirname(__FILE__) + '/../sass'
require 'sass/tree/node'
require 'strscan'
 
module Sass
  # :stopdoc:
  module Tree
    class Node
      def to_sass(opts = {})
        result = ''
 
        children.each do |child|
          result << "#{child.to_sass(0, opts)}\n"
        end
 
        result
      end
    end
 
    class ValueNode
      def to_sass(tabs)
        "#{value}\n"
      end
    end
 
    class RuleNode
      def to_sass(tabs, opts = {})
        str = "\n#{' ' * tabs}#{rule}#{children.any? { |c| c.is_a? AttrNode } ? "\n" : ''}"
 
        children.each do |child|
          str << "#{child.to_sass(tabs + 1, opts)}"
        end
 
        str
      end
    end
 
    class AttrNode
      def to_sass(tabs, opts = {})
        "#{' ' * tabs}#{opts[:alternate] ? '' : ':'}#{name}#{opts[:alternate] ? ':' : ''} #{value}\n"
      end
    end
  end
 
  # This class is based on the Ruby 1.9 ordered hashes.
  # It keeps the semantics and most of the efficiency of normal hashes
  # while also keeping track of the order in which elements were set.
  class OrderedHash
    Node = Struct.new('Node', :key, :value, :next)
    include Enumerable
 
    def initialize
      @hash = {}
    end
 
    def [](key)
      @hash[key] && @hash[key].value
    end
 
    def []=(key, value)
      node = Node.new(key, value, nil)
      if @first.nil?
        @first = @last = node
      else
        @last.next = node
        @last = node
      end
      @hash[key] = node
      value
    end
 
    def each
      return unless @first
      yield [@first.key, @first.value]
      node = @first
      yield [node.key, node.value] while node = node.next
      self
    end
 
    def values
      self.map { |k, v| v }
    end
  end
 
  # :startdoc:
 
  # This class contains the functionality used in the +css2sass+ utility,
  # namely converting CSS documents to Sass templates.
  class CSS
 
    # Creates a new instance of Sass::CSS that will compile the given document
    # to a Sass string when +render+ is called.
    def initialize(template, options = {})
      if template.is_a? IO
        template = template.read
      end
 
      @options = options
      @template = StringScanner.new(template)
    end
 
    # Processes the document and returns the result as a string
    # containing the CSS template.
    def render
      begin
        build_tree.to_sass(@options).lstrip
      rescue Exception => err
        line = @template.string[0...@template.pos].split("\n").size
 
        err.backtrace.unshift "(css):#{line}"
        raise err
      end
    end
 
    private
 
    def build_tree
      root = Tree::Node.new(nil)
      whitespace
      directives root
      rules root
      expand_commas root
      parent_ref_rules root
      remove_parent_refs root
      flatten_rules root
      fold_commas root
      root
    end
 
    def directives(root)
      while @template.scan(/@/)
        name = @template.scan /[^\s;]+/
        whitespace
        value = @template.scan /[^;]+/
        assert_match /;/
        whitespace
 
        if name == "import" && value =~ /^(url\()?"?([^\s\(\)\"]+)\.css"?\)?$/
          value = $2
        end
 
        root << Tree::ValueNode.new("@#{name} #{value};", nil)
      end
    end
 
    def rules(root)
      rules = []
      while @template.scan(/[^\{\s]+/)
        rules << @template[0]
        whitespace
 
        if @template.scan(/\{/)
          result = Tree::RuleNode.new(rules.join(' '), nil)
          root << result
          rules = []
 
          whitespace
          attributes(result)
        end
      end
    end
 
    def attributes(rule)
      while @template.scan(/[^:\}\s]+/)
        name = @template[0]
        whitespace
 
        assert_match /:/
 
        value = ''
        while @template.scan(/[^;\s\}]+/)
          value << @template[0] << whitespace
        end
 
        assert_match /(;|(?=\}))/
        rule << Tree::AttrNode.new(name, value, nil)
      end
 
      assert_match /\}/
    end
 
    def whitespace
      space = @template.scan(/\s*/) || ''
 
      # If we've hit a comment,
      # go past it and look for more whitespace
      if @template.scan(/\/\*/)
        @template.scan_until(/\*\//)
        return space + whitespace
      end
      return space
    end
 
    def assert_match(re)
      if !@template.scan(re)
        line = @template.string[0..@template.pos].count "\n"
        # Display basic regexps as plain old strings
        expected = re.source == Regexp.escape(re.source) ? "\"#{re.source}\"" : re.inspect
        raise Exception.new("Invalid CSS on line #{line}: expected #{expected}")
      end
      whitespace
    end
 
    # Transform
    #
    # foo, bar, baz
    # color: blue
    #
    # into
    #
    # foo
    # color: blue
    # bar
    # color: blue
    # baz
    # color: blue
    #
    # Yes, this expands the amount of code,
    # but it's necessary to get nesting to work properly.
    def expand_commas(root)
      root.children.map! do |child|
        next child unless Tree::RuleNode === child && child.rule.include?(',')
        child.rule.split(',').map do |rule|
          node = Tree::RuleNode.new(rule, nil)
          node.children = child.children
          node
        end
      end
      root.children.flatten!
    end
 
    # Make rules use parent refs so that
    #
    # foo
    # color: green
    # foo.bar
    # color: blue
    #
    # becomes
    #
    # foo
    # color: green
    # &.bar
    # color: blue
    #
    # This has the side effect of nesting rules,
    # so that
    #
    # foo
    # color: green
    # foo bar
    # color: red
    # foo baz
    # color: blue
    #
    # becomes
    #
    # foo
    # color: green
    # & bar
    # color: red
    # & baz
    # color: blue
    #
    def parent_ref_rules(root)
      rules = OrderedHash.new
      root.children.select { |c| Tree::RuleNode === c }.each do |child|
        root.children.delete child
        first, rest = child.rule.scan(/^(&?(?: .|[^ ])[^.#: \[]*)([.#: \[].*)?$/).first
        rules[first] ||= Tree::RuleNode.new(first, nil)
        if rest
          child.rule = "&" + rest
          rules[first] << child
        else
          rules[first].children += child.children
        end
      end
 
      rules.values.each { |v| parent_ref_rules(v) }
      root.children += rules.values
    end
 
    # Remove useless parent refs so that
    #
    # foo
    # & bar
    # color: blue
    #
    # becomes
    #
    # foo
    # bar
    # color: blue
    #
    def remove_parent_refs(root)
      root.children.each do |child|
        if child.is_a?(Tree::RuleNode)
          child.rule.gsub! /^& /, ''
          remove_parent_refs child
        end
      end
    end
 
    # Flatten rules so that
    #
    # foo
    # bar
    # baz
    # color: red
    #
    # becomes
    #
    # foo bar baz
    # color: red
    #
    # and
    #
    # foo
    # &.bar
    # color: blue
    #
    # becomes
    #
    # foo.bar
    # color: blue
    #
    def flatten_rules(root)
      root.children.each { |child| flatten_rule(child) if child.is_a?(Tree::RuleNode) }
    end
 
    def flatten_rule(rule)
      while rule.children.size == 1 && rule.children.first.is_a?(Tree::RuleNode)
        child = rule.children.first
 
        if child.rule[0] == ?&
          rule.rule = child.rule.gsub /^&/, rule.rule
        else
          rule.rule = "#{rule.rule} #{child.rule}"
        end
 
        rule.children = child.children
      end
 
      flatten_rules(rule)
    end
 
    # Transform
    #
    # foo
    # bar
    # color: blue
    # baz
    # color: blue
    #
    # into
    #
    # foo
    # bar, baz
    # color: blue
    #
    def fold_commas(root)
      prev_rule = nil
      root.children.map! do |child|
        next child unless Tree::RuleNode === child
 
        if prev_rule && prev_rule.children == child.children
          prev_rule.rule << ", #{child.rule}"
          next nil
        end
 
        fold_commas(child)
        prev_rule = child
        child
      end
      root.children.compact!
    end
  end
end