haml 1.0.5 → 1.5.0
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of haml might be problematic. Click here for more details.
- data/README +229 -0
- data/Rakefile +56 -60
- data/VERSION +1 -1
- data/bin/haml +4 -14
- data/bin/html2haml +89 -0
- data/bin/sass +8 -0
- data/init.rb +5 -1
- data/lib/haml.rb +643 -0
- data/lib/haml/buffer.rb +33 -30
- data/lib/haml/engine.rb +258 -75
- data/lib/haml/error.rb +43 -0
- data/lib/haml/exec.rb +181 -0
- data/lib/haml/filters.rb +89 -0
- data/lib/haml/helpers.rb +19 -5
- data/lib/haml/helpers/action_view_mods.rb +28 -4
- data/lib/haml/template.rb +13 -27
- data/lib/sass.rb +418 -0
- data/lib/sass/constant.rb +190 -0
- data/lib/sass/constant/color.rb +77 -0
- data/lib/sass/constant/literal.rb +51 -0
- data/lib/sass/constant/number.rb +87 -0
- data/lib/sass/constant/operation.rb +30 -0
- data/lib/sass/constant/string.rb +18 -0
- data/lib/sass/engine.rb +179 -0
- data/lib/sass/error.rb +35 -0
- data/lib/sass/plugin.rb +119 -0
- data/lib/sass/tree/attr_node.rb +44 -0
- data/lib/sass/tree/node.rb +29 -0
- data/lib/sass/tree/rule_node.rb +47 -0
- data/lib/sass/tree/value_node.rb +12 -0
- data/test/benchmark.rb +16 -19
- data/test/haml/engine_test.rb +220 -0
- data/test/{helper_test.rb → haml/helper_test.rb} +9 -8
- data/test/{mocks → haml/mocks}/article.rb +0 -0
- data/test/{results → haml/results}/content_for_layout.xhtml +0 -0
- data/test/{results → haml/results}/eval_suppressed.xhtml +0 -0
- data/test/haml/results/filters.xhtml +57 -0
- data/test/{results → haml/results}/helpers.xhtml +10 -0
- data/test/haml/results/helpful.xhtml +8 -0
- data/test/{results → haml/results}/just_stuff.xhtml +5 -0
- data/test/{results → haml/results}/list.xhtml +0 -0
- data/test/{results → haml/results}/original_engine.xhtml +1 -1
- data/test/{results → haml/results}/partials.xhtml +0 -0
- data/test/{results → haml/results}/silent_script.xhtml +0 -0
- data/test/{results → haml/results}/standard.xhtml +2 -1
- data/test/{results → haml/results}/tag_parsing.xhtml +0 -0
- data/test/{results → haml/results}/very_basic.xhtml +0 -0
- data/test/haml/results/whitespace_handling.xhtml +104 -0
- data/test/{rhtml → haml/rhtml}/standard.rhtml +4 -1
- data/test/{runner.rb → haml/runner.rb} +1 -1
- data/test/{template_test.rb → haml/template_test.rb} +28 -23
- data/test/{templates → haml/templates}/_partial.haml +0 -0
- data/test/{templates → haml/templates}/_text_area.haml +0 -0
- data/test/haml/templates/breakage.haml +8 -0
- data/test/{templates → haml/templates}/content_for_layout.haml +0 -0
- data/test/{templates → haml/templates}/eval_suppressed.haml +0 -0
- data/test/haml/templates/filters.haml +53 -0
- data/test/{templates → haml/templates}/helpers.haml +10 -1
- data/test/{templates → haml/templates}/helpful.haml +3 -0
- data/test/{templates → haml/templates}/just_stuff.haml +7 -0
- data/test/{templates → haml/templates}/list.haml +0 -0
- data/test/haml/templates/original_engine.haml +17 -0
- data/test/{templates → haml/templates}/partialize.haml +0 -0
- data/test/{templates → haml/templates}/partials.haml +0 -0
- data/test/{templates → haml/templates}/silent_script.haml +0 -0
- data/test/{templates → haml/templates}/standard.haml +3 -1
- data/test/{templates → haml/templates}/tag_parsing.haml +0 -0
- data/test/{templates → haml/templates}/very_basic.haml +0 -0
- data/test/haml/templates/whitespace_handling.haml +137 -0
- data/test/profile.rb +36 -18
- data/test/sass/engine_test.rb +87 -0
- data/test/sass/plugin_test.rb +103 -0
- data/test/sass/results/basic.css +9 -0
- data/test/sass/results/compact.css +5 -0
- data/test/sass/results/complex.css +86 -0
- data/test/sass/results/constants.css +12 -0
- data/test/sass/results/expanded.css +18 -0
- data/test/sass/results/nested.css +14 -0
- data/test/sass/templates/basic.sass +23 -0
- data/test/sass/templates/bork.sass +2 -0
- data/test/sass/templates/compact.sass +15 -0
- data/test/sass/templates/complex.sass +291 -0
- data/test/sass/templates/constants.sass +80 -0
- data/test/sass/templates/expanded.sass +15 -0
- data/test/sass/templates/nested.sass +15 -0
- metadata +98 -48
- data/REFERENCE +0 -662
- data/test/engine_test.rb +0 -93
- data/test/results/helpful.xhtml +0 -5
- data/test/results/whitespace_handling.xhtml +0 -51
- data/test/templates/original_engine.haml +0 -17
- data/test/templates/whitespace_handling.haml +0 -66
data/lib/sass.rb
ADDED
@@ -0,0 +1,418 @@
|
|
1
|
+
dir = File.dirname(__FILE__)
|
2
|
+
$LOAD_PATH << dir unless $LOAD_PATH.include?(dir)
|
3
|
+
|
4
|
+
# = Sass (Syntactically Awesome StyleSheets)
|
5
|
+
#
|
6
|
+
# Sass is a meta-language on top of CSS
|
7
|
+
# that's used to describe the style of a document
|
8
|
+
# cleanly and structurally,
|
9
|
+
# with more power than flat CSS allows.
|
10
|
+
# Sass both provides a simpler, more elegant syntax for CSS
|
11
|
+
# and implements various features that are useful
|
12
|
+
# for creating manageable stylesheets.
|
13
|
+
#
|
14
|
+
# == Features
|
15
|
+
#
|
16
|
+
# * Whitespace active
|
17
|
+
# * Well-formatted output
|
18
|
+
# * Elegant input
|
19
|
+
# * Feature-rich
|
20
|
+
#
|
21
|
+
# == Using Sass
|
22
|
+
#
|
23
|
+
# Sass can be used in two ways:
|
24
|
+
# As a plugin for Ruby on Rails
|
25
|
+
# and as a standalone parser.
|
26
|
+
# Sass is bundled with Haml,
|
27
|
+
# so if the Haml plugin or RubyGem is installed,
|
28
|
+
# Sass will already be installed as a plugin or gem, respectively.
|
29
|
+
#
|
30
|
+
# To install Haml and Sass as a Ruby on Rails plugin,
|
31
|
+
# use the normal Rails plugin installer:
|
32
|
+
#
|
33
|
+
# ./script/plugin install http://svn.hamptoncatlin.com/haml/tags/stable
|
34
|
+
#
|
35
|
+
# Sass templates in Rails don't quite function in the same way as views,
|
36
|
+
# because they don't contain dynamic content,
|
37
|
+
# and so only need to be compiled when the template file has been updated.
|
38
|
+
# By default (see options, below),
|
39
|
+
# ".sass" files are placed in public/stylesheets/sass.
|
40
|
+
# Then, whenever necessary, they're compiled into corresponding CSS files in public/stylesheets.
|
41
|
+
# For instance, public/stylesheets/sass/main.sass would be compiled to public/stylesheets/main.css.
|
42
|
+
#
|
43
|
+
# Using Sass in Ruby code is very simple.
|
44
|
+
# First install the Haml/Sass RubyGem:
|
45
|
+
#
|
46
|
+
# gem install haml
|
47
|
+
#
|
48
|
+
# Then you can use it by including the "sass" gem
|
49
|
+
# and using Sass::Engine like so:
|
50
|
+
#
|
51
|
+
# engine = Sass::Engine.new("#main\n :background-color #0000ff")
|
52
|
+
# engine.render #=> "#main { background-color: #0000ff; }\n"
|
53
|
+
#
|
54
|
+
# == CSS Rules
|
55
|
+
#
|
56
|
+
# Rules in flat CSS have two elements:
|
57
|
+
# the selector
|
58
|
+
# (e.g. "#main", "div p", "li a:hover")
|
59
|
+
# and the attributes
|
60
|
+
# (e.g. "color: #00ff00;", "width: 5em;").
|
61
|
+
#
|
62
|
+
# Sass has both of these,
|
63
|
+
# as well as one additional element: nested rules.
|
64
|
+
#
|
65
|
+
# === Rules and Selectors
|
66
|
+
#
|
67
|
+
# However, some of the syntax is a little different.
|
68
|
+
# The syntax for selectors is the same,
|
69
|
+
# but instead of using brackets to delineate the attributes that belong to a particular rule,
|
70
|
+
# Sass uses two spaces of indentation.
|
71
|
+
# For example:
|
72
|
+
#
|
73
|
+
# #main p
|
74
|
+
# <attribute>
|
75
|
+
# <attribute>
|
76
|
+
# ...
|
77
|
+
#
|
78
|
+
# === Attributes
|
79
|
+
#
|
80
|
+
# The syntax for attributes is also slightly different.
|
81
|
+
# The colon is at the beginning of the attribute,
|
82
|
+
# rather than between the name and the value,
|
83
|
+
# so it's easier to tell what elements are attributes just by glancing at them.
|
84
|
+
# Attributes also don't have semicolons at the end;
|
85
|
+
# each attribute is on its own line, so they aren't necessary.
|
86
|
+
# For example:
|
87
|
+
#
|
88
|
+
# #main p
|
89
|
+
# :color #00ff00
|
90
|
+
# :width 97%
|
91
|
+
#
|
92
|
+
# is compiled to:
|
93
|
+
#
|
94
|
+
# #main p {
|
95
|
+
# color: #00ff00;
|
96
|
+
# width: 97% }
|
97
|
+
#
|
98
|
+
# === Nested Rules
|
99
|
+
#
|
100
|
+
# Rules can also be nested within each other.
|
101
|
+
# This signifies that the inner rule's selector is a child of the outer selector.
|
102
|
+
# For example:
|
103
|
+
#
|
104
|
+
# #main p
|
105
|
+
# :color #00ff00
|
106
|
+
# :width 97%
|
107
|
+
#
|
108
|
+
# .redbox
|
109
|
+
# :background-color #ff0000
|
110
|
+
# :color #000000
|
111
|
+
#
|
112
|
+
# is compiled to:
|
113
|
+
#
|
114
|
+
# #main p {
|
115
|
+
# color: #00ff00;
|
116
|
+
# width: 97%; }
|
117
|
+
# #main p .redbox {
|
118
|
+
# background-color: #ff0000;
|
119
|
+
# color: #000000; }
|
120
|
+
#
|
121
|
+
# This makes insanely complicated CSS layouts with lots of nested selectors very simple:
|
122
|
+
#
|
123
|
+
# #main
|
124
|
+
# :width 97%
|
125
|
+
#
|
126
|
+
# p, div
|
127
|
+
# :font-size 2em
|
128
|
+
# a
|
129
|
+
# :font-weight bold
|
130
|
+
#
|
131
|
+
# pre
|
132
|
+
# :font-size 3em
|
133
|
+
#
|
134
|
+
# is compiled to:
|
135
|
+
#
|
136
|
+
# #main {
|
137
|
+
# width: 97%; }
|
138
|
+
# #main p, #main div {
|
139
|
+
# font-size: 2em; }
|
140
|
+
# #main p a, #main div a {
|
141
|
+
# font-weight: bold; }
|
142
|
+
# #main pre {
|
143
|
+
# font-size: 3em; }
|
144
|
+
#
|
145
|
+
# === Attribute Namespaces
|
146
|
+
#
|
147
|
+
# CSS has quite a few attributes that are in "namespaces;"
|
148
|
+
# for instance, "font-family," "font-size," and "font-weight"
|
149
|
+
# are all in the "font" namespace.
|
150
|
+
# In CSS, if you want to set a bunch of attributes in the same namespace,
|
151
|
+
# you have to type it out each time.
|
152
|
+
# Sass offers a shortcut for this:
|
153
|
+
# just write the namespace one,
|
154
|
+
# then indent each of the sub-attributes within it.
|
155
|
+
# For example:
|
156
|
+
#
|
157
|
+
# .funky
|
158
|
+
# :font
|
159
|
+
# :family fantasy
|
160
|
+
# :size 30em
|
161
|
+
# :weight bold
|
162
|
+
#
|
163
|
+
# is compiled to:
|
164
|
+
#
|
165
|
+
# .funky {
|
166
|
+
# font-family: fantasy;
|
167
|
+
# font-size: 30em;
|
168
|
+
# font-weight: bold; }
|
169
|
+
#
|
170
|
+
# == Constants
|
171
|
+
#
|
172
|
+
# Sass has support for setting document-wide constants.
|
173
|
+
# They're set using an exclamation mark followed by the name,
|
174
|
+
# an equals sign, and the value.
|
175
|
+
# An attribute can then be set to the value of a constant
|
176
|
+
# by following it with another equals sign.
|
177
|
+
# For example:
|
178
|
+
#
|
179
|
+
# !main_color = #00ff00
|
180
|
+
#
|
181
|
+
# #main
|
182
|
+
# :color = !main_color
|
183
|
+
# :p
|
184
|
+
# :background-color = !main_color
|
185
|
+
# :color #000000
|
186
|
+
#
|
187
|
+
# is compiled to:
|
188
|
+
#
|
189
|
+
# #main {
|
190
|
+
# color: #00ff00; }
|
191
|
+
# #main p {
|
192
|
+
# background-color: #00ff00;
|
193
|
+
# color: #000000; }
|
194
|
+
#
|
195
|
+
# === Arithmetic
|
196
|
+
#
|
197
|
+
# You can even do basic arithmetic with constants.
|
198
|
+
# Sass recognizes numbers, colors,
|
199
|
+
# lengths (numbers with units),
|
200
|
+
# and strings (everything that's not one of the above),
|
201
|
+
# and various operators that work on various values.
|
202
|
+
# All the normal arithmetic operators
|
203
|
+
# (+, -, *, /, %, and parentheses for grouping)
|
204
|
+
# are defined as usual
|
205
|
+
# for numbers, colors, and lengths.
|
206
|
+
# The "+" operator is also defined for Strings
|
207
|
+
# as the concatenation operator.
|
208
|
+
# For example:
|
209
|
+
#
|
210
|
+
# !main_width = 10
|
211
|
+
# !unit1 = em
|
212
|
+
# !unit2 = px
|
213
|
+
# !bg_color = #a5f39e
|
214
|
+
#
|
215
|
+
# #main
|
216
|
+
# :background-color = !bg_color
|
217
|
+
# p
|
218
|
+
# :background-color = !bg_color + #202020
|
219
|
+
# :width = !main_width + !unit1
|
220
|
+
# img.thumb
|
221
|
+
# :width = (!main_width + 15) + !unit2
|
222
|
+
#
|
223
|
+
# is compiled to:
|
224
|
+
#
|
225
|
+
# #main {
|
226
|
+
# background-color: #a5f39e; }
|
227
|
+
# #main p {
|
228
|
+
# background-color: #c5ffbe;
|
229
|
+
# width: 10em; }
|
230
|
+
# #main img.thumb {
|
231
|
+
# width: 25em; }
|
232
|
+
#
|
233
|
+
# === Colors
|
234
|
+
#
|
235
|
+
# Not only can arithmetic be done between colors and other colors,
|
236
|
+
# but it can be done between colors and normal numbers.
|
237
|
+
# In this case, the operation is done piecewise one each of the
|
238
|
+
# Red, Green, and Blue components of the color.
|
239
|
+
# For example:
|
240
|
+
#
|
241
|
+
# !main_color = #a5f39e
|
242
|
+
#
|
243
|
+
# #main
|
244
|
+
# :background-color = !main_color
|
245
|
+
# p
|
246
|
+
# :background-color = !bg_color + 32
|
247
|
+
#
|
248
|
+
# is compiled to:
|
249
|
+
#
|
250
|
+
# #main {
|
251
|
+
# background-color: #a5f39e; }
|
252
|
+
# #main p {
|
253
|
+
# background-color: #c5ffbe; }
|
254
|
+
#
|
255
|
+
# === Strings
|
256
|
+
#
|
257
|
+
# Strings are the type that's used by default
|
258
|
+
# when an element in a bit of constant arithmetic isn't recognized
|
259
|
+
# as another type of constant.
|
260
|
+
# However, they can also be created explicitly be wrapping a section of code with quotation marks.
|
261
|
+
# Inside the quotation marks,
|
262
|
+
# a backslash can be used to
|
263
|
+
# escape quotation marks that you want to appear in the CSS.
|
264
|
+
# For example:
|
265
|
+
#
|
266
|
+
# !content = "Hello, \"Hubert\" Bean."
|
267
|
+
#
|
268
|
+
# #main
|
269
|
+
# :content = "string(" + !content + ")"
|
270
|
+
#
|
271
|
+
# is compiled to:
|
272
|
+
#
|
273
|
+
# #main {
|
274
|
+
# content: string(Hello, "Hubert" Bean.) }
|
275
|
+
#
|
276
|
+
# === Default Concatenation
|
277
|
+
#
|
278
|
+
# All those plusses and quotes for concatenating strings
|
279
|
+
# can get pretty messy, though.
|
280
|
+
# Most of the time, if you want to concatenate stuff,
|
281
|
+
# you just want individual values with spaces in between them.
|
282
|
+
# Thus, in Sass, when two values are next to each other without an operator,
|
283
|
+
# they're simply joined with a space.
|
284
|
+
# For example:
|
285
|
+
#
|
286
|
+
# !font_family = sans-serif
|
287
|
+
# !main_font_size = 1em
|
288
|
+
#
|
289
|
+
# #main
|
290
|
+
# :font
|
291
|
+
# :family = !font-family
|
292
|
+
# :size = !main_font_size
|
293
|
+
# h6
|
294
|
+
# :font = italic small-caps bold (!main_font_size + 0.1em) !font-family
|
295
|
+
#
|
296
|
+
# is compiled to:
|
297
|
+
#
|
298
|
+
# #main {
|
299
|
+
# font-family: sans-serif;
|
300
|
+
# font-size: 1em; }
|
301
|
+
# #main h6 {
|
302
|
+
# font: italic small-caps bold 1.1em sans-serif; }
|
303
|
+
#
|
304
|
+
# == Output Style
|
305
|
+
#
|
306
|
+
# Although the default CSS style that Sass outputs is very nice,
|
307
|
+
# and reflects the structure of the document in a similar way that Sass does,
|
308
|
+
# sometimes it's good to have other formats available.
|
309
|
+
#
|
310
|
+
# Sass allows you to choose between three different output styles
|
311
|
+
# by setting the <tt>:style</tt> option.
|
312
|
+
# In Rails, this is done by setting <tt>Sass::Template.options[:style]</tt>;
|
313
|
+
# outside Rails, it's done by passing an options hash with </tt>:style</tt> set.
|
314
|
+
#
|
315
|
+
# === <tt>:nested</tt>
|
316
|
+
#
|
317
|
+
# Nested style is the default Sass style,
|
318
|
+
# because it reflects the structure of the document
|
319
|
+
# in much the same way Sass does.
|
320
|
+
# Each attribute has its own line,
|
321
|
+
# but the indentation isn't constant.
|
322
|
+
# Each rule is indented based on how deeply it's nested.
|
323
|
+
# For example:
|
324
|
+
#
|
325
|
+
# #main {
|
326
|
+
# color: #fff;
|
327
|
+
# background-color: #000; }
|
328
|
+
# #main p {
|
329
|
+
# width: 10em; }
|
330
|
+
#
|
331
|
+
# .huge {
|
332
|
+
# font-size: 10em;
|
333
|
+
# font-weight: bold;
|
334
|
+
# text-decoration: underline; }
|
335
|
+
#
|
336
|
+
# Nested style is very useful when looking at large CSS files
|
337
|
+
# for the same reason Sass is useful for making them:
|
338
|
+
# it allows you to very easily grasp the structure of the file
|
339
|
+
# without actually reading anything.
|
340
|
+
#
|
341
|
+
# === <tt>:expanded</tt>
|
342
|
+
#
|
343
|
+
# Expanded is the typical human-made CSS style,
|
344
|
+
# with each attribute and rule taking up one line.
|
345
|
+
# Attributes are indented within the rules,
|
346
|
+
# but the rules aren't indented in any special way.
|
347
|
+
# For example:
|
348
|
+
#
|
349
|
+
# #main {
|
350
|
+
# color: #fff;
|
351
|
+
# background-color: #000;
|
352
|
+
# }
|
353
|
+
# #main p {
|
354
|
+
# width: 10em;
|
355
|
+
# }
|
356
|
+
#
|
357
|
+
# .huge {
|
358
|
+
# font-size: 10em;
|
359
|
+
# font-weight: bold;
|
360
|
+
# text-decoration: underline;
|
361
|
+
# }
|
362
|
+
#
|
363
|
+
# === <tt>:compact</tt>
|
364
|
+
#
|
365
|
+
# Compact style, as the name would imply,
|
366
|
+
# takes up less space than Nested or Expanded.
|
367
|
+
# However, it's also harder to read.
|
368
|
+
# Each CSS rule takes up only one line,
|
369
|
+
# with every attribute defined on that line.
|
370
|
+
# Nested rules are placed next to each other with no newline,
|
371
|
+
# while groups of rules have newlines between them.
|
372
|
+
# For example:
|
373
|
+
#
|
374
|
+
# #main { color: #fff; background-color: #000; }
|
375
|
+
# #main p { width: 10em; }
|
376
|
+
#
|
377
|
+
# .huge { font-size: 10em; font-weight: bold; text-decoration: underline; }
|
378
|
+
#
|
379
|
+
# == Sass Options
|
380
|
+
#
|
381
|
+
# Options can be set by setting the hash <tt>Sass::Plugin.options</tt>
|
382
|
+
# from <tt>environment.rb</tt> in Rails,
|
383
|
+
# or by passing an options hash to Sass::Engine.
|
384
|
+
# Available options are:
|
385
|
+
#
|
386
|
+
# [<tt>:style</tt>] Sets the style of the CSS output.
|
387
|
+
# See the section on Output Style, above.
|
388
|
+
#
|
389
|
+
# [<tt>:always_update</tt>] Whether the CSS files should be updated every
|
390
|
+
# time a controller is accessed,
|
391
|
+
# as opposed to only when the template has been modified.
|
392
|
+
# Defaults to false.
|
393
|
+
# Only has meaning within Ruby on Rails.
|
394
|
+
#
|
395
|
+
# [<tt>:always_update</tt>] Whether a Sass template should be checked for updates every
|
396
|
+
# time a controller is accessed,
|
397
|
+
# as opposed to only when the Rails server starts.
|
398
|
+
# If a Sass template has been updated,
|
399
|
+
# it will be recompiled and will overwrite the corresponding CSS file.
|
400
|
+
# Defaults to false if Rails is running in production mode,
|
401
|
+
# true otherwise.
|
402
|
+
# Only has meaning within Ruby on Rails.
|
403
|
+
#
|
404
|
+
# [<tt>:template_location</tt>] The directory where Sass templates should be read from.
|
405
|
+
# Defaults to <tt>RAILS_ROOT + "/public/stylesheets/sass"</tt>.
|
406
|
+
# Only has meaning within Ruby on Rails.
|
407
|
+
#
|
408
|
+
# [<tt>:css_location</tt>] The directory where CSS output should be written to.
|
409
|
+
# Defaults to <tt>RAILS_ROOT + "/public/stylesheets"</tt>.
|
410
|
+
# Only has meaning within Ruby on Rails.
|
411
|
+
#
|
412
|
+
# [<tt>:filename</tt>] The filename of the file being rendered.
|
413
|
+
# This is used solely for reporting errors,
|
414
|
+
# and is automatically set when using Rails.
|
415
|
+
#
|
416
|
+
module Sass; end
|
417
|
+
|
418
|
+
require 'sass/engine'
|
@@ -0,0 +1,190 @@
|
|
1
|
+
require 'sass/constant/operation'
|
2
|
+
require 'sass/constant/literal'
|
3
|
+
|
4
|
+
module Sass
|
5
|
+
module Constant # :nodoc:
|
6
|
+
# The character that begins a constant.
|
7
|
+
CONSTANT_CHAR = ?!
|
8
|
+
|
9
|
+
# Whitespace characters
|
10
|
+
WHITESPACE = [?\ , ?\t, ?\n, ?\r]
|
11
|
+
|
12
|
+
# The character used to escape values
|
13
|
+
ESCAPE_CHAR = ?\\
|
14
|
+
|
15
|
+
# The character used to open and close strings
|
16
|
+
STRING_CHAR = ?"
|
17
|
+
|
18
|
+
# A mapping of syntactically-significant characters
|
19
|
+
# to parsed symbols
|
20
|
+
SYMBOLS = {
|
21
|
+
?( => :open,
|
22
|
+
?) => :close,
|
23
|
+
?+ => :plus,
|
24
|
+
?- => :minus,
|
25
|
+
?* => :times,
|
26
|
+
?/ => :div,
|
27
|
+
?% => :mod,
|
28
|
+
STRING_CHAR => :str,
|
29
|
+
ESCAPE_CHAR => :esc
|
30
|
+
}
|
31
|
+
|
32
|
+
# The regular expression used to parse constants
|
33
|
+
MATCH = /^#{Regexp.escape(CONSTANT_CHAR.chr)}([^\s#{(SYMBOLS.keys + [ ?= ]).map {|c| Regexp.escape("#{c.chr}") }}]+)\s*=\s*(.+)/
|
34
|
+
|
35
|
+
# First-order operations
|
36
|
+
FIRST_ORDER = [:times, :div, :mod]
|
37
|
+
|
38
|
+
# Second-order operations
|
39
|
+
SECOND_ORDER = [:plus, :minus]
|
40
|
+
|
41
|
+
class << self
|
42
|
+
def parse(value, constants, line)
|
43
|
+
begin
|
44
|
+
operationalize(parenthesize(tokenize(value)), constants).to_s
|
45
|
+
rescue Sass::SyntaxError => e
|
46
|
+
if e.message == "Constant arithmetic error"
|
47
|
+
e.instance_eval do
|
48
|
+
@message += ": #{value.dump}"
|
49
|
+
end
|
50
|
+
end
|
51
|
+
e.sass_line = line
|
52
|
+
raise e
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
private
|
57
|
+
|
58
|
+
def tokenize(value)
|
59
|
+
escaped = false
|
60
|
+
is_string = false
|
61
|
+
negative_okay = true
|
62
|
+
str = ''
|
63
|
+
to_return = []
|
64
|
+
|
65
|
+
reset_str = Proc.new do
|
66
|
+
to_return << str unless str.empty?
|
67
|
+
''
|
68
|
+
end
|
69
|
+
|
70
|
+
value.each_byte do |byte|
|
71
|
+
unless escaped
|
72
|
+
if byte == ESCAPE_CHAR
|
73
|
+
escaped = true
|
74
|
+
next
|
75
|
+
end
|
76
|
+
|
77
|
+
last = to_return[-1]
|
78
|
+
|
79
|
+
if byte == STRING_CHAR
|
80
|
+
is_string = !is_string
|
81
|
+
|
82
|
+
if is_string && last && (!last.is_a?(Symbol) || last == :close)
|
83
|
+
to_return << :concat
|
84
|
+
end
|
85
|
+
|
86
|
+
str = reset_str.call
|
87
|
+
next
|
88
|
+
end
|
89
|
+
|
90
|
+
unless is_string
|
91
|
+
|
92
|
+
if WHITESPACE.include?(byte)
|
93
|
+
str = reset_str.call
|
94
|
+
next
|
95
|
+
end
|
96
|
+
|
97
|
+
symbol = SYMBOLS[byte]
|
98
|
+
|
99
|
+
if (symbol.nil? || symbol == :open) &&
|
100
|
+
last && (!last.is_a?(Symbol) || last == :close)
|
101
|
+
# Two values connected without an operator
|
102
|
+
to_return << :concat
|
103
|
+
end
|
104
|
+
|
105
|
+
if symbol && !(negative_okay && symbol == :minus)
|
106
|
+
str = reset_str.call
|
107
|
+
negative_okay = true
|
108
|
+
to_return << symbol
|
109
|
+
next
|
110
|
+
end
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
114
|
+
escaped = false
|
115
|
+
negative_okay = false
|
116
|
+
str << byte.chr
|
117
|
+
end
|
118
|
+
|
119
|
+
if is_string
|
120
|
+
raise Sass::SyntaxError.new("Unterminated string: #{value.dump}")
|
121
|
+
end
|
122
|
+
str = reset_str.call
|
123
|
+
to_return
|
124
|
+
end
|
125
|
+
|
126
|
+
def parenthesize(value)
|
127
|
+
parenthesize_helper(0, value, value.length)[0]
|
128
|
+
end
|
129
|
+
|
130
|
+
def parenthesize_helper(i, value, value_len)
|
131
|
+
to_return = []
|
132
|
+
beginning = i
|
133
|
+
token = value[i]
|
134
|
+
|
135
|
+
while i < value_len && token != :close
|
136
|
+
if token == :open
|
137
|
+
to_return.push(*value[beginning...i])
|
138
|
+
sub, i = parenthesize_helper(i + 1, value, value_len)
|
139
|
+
beginning = i
|
140
|
+
to_return << sub
|
141
|
+
else
|
142
|
+
i += 1
|
143
|
+
end
|
144
|
+
|
145
|
+
token = value[i]
|
146
|
+
end
|
147
|
+
to_return.push(*value[beginning...i])
|
148
|
+
return to_return, i + 1
|
149
|
+
end
|
150
|
+
|
151
|
+
#--
|
152
|
+
# TODO: Don't pass around original value;
|
153
|
+
# have Constant.parse automatically add it to exception.
|
154
|
+
#++
|
155
|
+
def operationalize(value, constants)
|
156
|
+
value = [value] unless value.is_a?(Array)
|
157
|
+
length = value.length
|
158
|
+
if length == 1
|
159
|
+
value = value[0]
|
160
|
+
if value.is_a? Operation
|
161
|
+
value
|
162
|
+
else
|
163
|
+
Literal.parse(insert_constant(value, constants))
|
164
|
+
end
|
165
|
+
elsif length == 2
|
166
|
+
raise SyntaxError.new("Constant arithmetic error")
|
167
|
+
elsif length == 3
|
168
|
+
Operation.new(operationalize(value[0], constants), operationalize(value[2], constants), value[1])
|
169
|
+
else
|
170
|
+
if SECOND_ORDER.include?(value[1]) && FIRST_ORDER.include?(value[3])
|
171
|
+
operationalize([value[0], value[1], operationalize(value[2..4], constants), *value[5..-1]], constants)
|
172
|
+
else
|
173
|
+
operationalize([operationalize(value[0..2], constants), *value[3..-1]], constants)
|
174
|
+
end
|
175
|
+
end
|
176
|
+
end
|
177
|
+
|
178
|
+
def insert_constant(value, constants)
|
179
|
+
to_return = value
|
180
|
+
if value[0] == CONSTANT_CHAR
|
181
|
+
to_return = constants[value[1..-1]]
|
182
|
+
unless to_return
|
183
|
+
raise SyntaxError.new("Undefined constant: \"#{value}\"")
|
184
|
+
end
|
185
|
+
end
|
186
|
+
to_return
|
187
|
+
end
|
188
|
+
end
|
189
|
+
end
|
190
|
+
end
|