jwhitmire-haml 2.1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +332 -0
- data/bin/css2sass +7 -0
- data/bin/haml +9 -0
- data/bin/html2haml +7 -0
- data/bin/sass +8 -0
- data/lib/haml/buffer.rb +255 -0
- data/lib/haml/engine.rb +268 -0
- data/lib/haml/error.rb +22 -0
- data/lib/haml/exec.rb +395 -0
- data/lib/haml/filters.rb +276 -0
- data/lib/haml/helpers/action_view_extensions.rb +45 -0
- data/lib/haml/helpers/action_view_mods.rb +181 -0
- data/lib/haml/helpers.rb +468 -0
- data/lib/haml/html.rb +218 -0
- data/lib/haml/precompiler.rb +889 -0
- data/lib/haml/shared.rb +45 -0
- data/lib/haml/template/patch.rb +58 -0
- data/lib/haml/template/plugin.rb +72 -0
- data/lib/haml/template.rb +51 -0
- data/lib/haml/util.rb +77 -0
- data/lib/haml/version.rb +47 -0
- data/lib/haml.rb +1042 -0
- data/lib/sass/css.rb +388 -0
- data/lib/sass/engine.rb +499 -0
- data/lib/sass/environment.rb +33 -0
- data/lib/sass/error.rb +35 -0
- data/lib/sass/plugin/merb.rb +56 -0
- data/lib/sass/plugin/rails.rb +24 -0
- data/lib/sass/plugin.rb +203 -0
- data/lib/sass/repl.rb +51 -0
- data/lib/sass/script/bool.rb +13 -0
- data/lib/sass/script/color.rb +97 -0
- data/lib/sass/script/funcall.rb +28 -0
- data/lib/sass/script/functions.rb +122 -0
- data/lib/sass/script/lexer.rb +152 -0
- data/lib/sass/script/literal.rb +60 -0
- data/lib/sass/script/number.rb +231 -0
- data/lib/sass/script/operation.rb +30 -0
- data/lib/sass/script/parser.rb +142 -0
- data/lib/sass/script/string.rb +42 -0
- data/lib/sass/script/unary_operation.rb +21 -0
- data/lib/sass/script/variable.rb +20 -0
- data/lib/sass/script.rb +38 -0
- data/lib/sass/tree/attr_node.rb +64 -0
- data/lib/sass/tree/comment_node.rb +34 -0
- data/lib/sass/tree/debug_node.rb +22 -0
- data/lib/sass/tree/directive_node.rb +50 -0
- data/lib/sass/tree/file_node.rb +27 -0
- data/lib/sass/tree/for_node.rb +29 -0
- data/lib/sass/tree/if_node.rb +27 -0
- data/lib/sass/tree/mixin_def_node.rb +18 -0
- data/lib/sass/tree/mixin_node.rb +34 -0
- data/lib/sass/tree/node.rb +99 -0
- data/lib/sass/tree/rule_node.rb +120 -0
- data/lib/sass/tree/variable_node.rb +24 -0
- data/lib/sass/tree/while_node.rb +20 -0
- data/lib/sass.rb +1062 -0
- data/test/benchmark.rb +99 -0
- data/test/haml/engine_test.rb +734 -0
- data/test/haml/helper_test.rb +224 -0
- data/test/haml/html2haml_test.rb +92 -0
- data/test/haml/markaby/standard.mab +52 -0
- data/test/haml/mocks/article.rb +6 -0
- data/test/haml/results/content_for_layout.xhtml +15 -0
- data/test/haml/results/eval_suppressed.xhtml +9 -0
- data/test/haml/results/filters.xhtml +62 -0
- data/test/haml/results/helpers.xhtml +93 -0
- data/test/haml/results/helpful.xhtml +10 -0
- data/test/haml/results/just_stuff.xhtml +68 -0
- data/test/haml/results/list.xhtml +12 -0
- data/test/haml/results/nuke_inner_whitespace.xhtml +40 -0
- data/test/haml/results/nuke_outer_whitespace.xhtml +148 -0
- data/test/haml/results/original_engine.xhtml +20 -0
- data/test/haml/results/partial_layout.xhtml +5 -0
- data/test/haml/results/partials.xhtml +21 -0
- data/test/haml/results/render_layout.xhtml +3 -0
- data/test/haml/results/silent_script.xhtml +74 -0
- data/test/haml/results/standard.xhtml +42 -0
- data/test/haml/results/tag_parsing.xhtml +23 -0
- data/test/haml/results/very_basic.xhtml +5 -0
- data/test/haml/results/whitespace_handling.xhtml +89 -0
- data/test/haml/rhtml/_av_partial_1.rhtml +12 -0
- data/test/haml/rhtml/_av_partial_2.rhtml +8 -0
- data/test/haml/rhtml/action_view.rhtml +62 -0
- data/test/haml/rhtml/standard.rhtml +54 -0
- data/test/haml/template_test.rb +204 -0
- data/test/haml/templates/_av_partial_1.haml +9 -0
- data/test/haml/templates/_av_partial_1_ugly.haml +9 -0
- data/test/haml/templates/_av_partial_2.haml +5 -0
- data/test/haml/templates/_av_partial_2_ugly.haml +5 -0
- data/test/haml/templates/_layout.erb +3 -0
- data/test/haml/templates/_layout_for_partial.haml +3 -0
- data/test/haml/templates/_partial.haml +8 -0
- data/test/haml/templates/_text_area.haml +3 -0
- data/test/haml/templates/action_view.haml +47 -0
- data/test/haml/templates/action_view_ugly.haml +47 -0
- data/test/haml/templates/breakage.haml +8 -0
- data/test/haml/templates/content_for_layout.haml +10 -0
- data/test/haml/templates/eval_suppressed.haml +11 -0
- data/test/haml/templates/filters.haml +66 -0
- data/test/haml/templates/helpers.haml +95 -0
- data/test/haml/templates/helpful.haml +11 -0
- data/test/haml/templates/just_stuff.haml +83 -0
- data/test/haml/templates/list.haml +12 -0
- data/test/haml/templates/nuke_inner_whitespace.haml +32 -0
- data/test/haml/templates/nuke_outer_whitespace.haml +144 -0
- data/test/haml/templates/original_engine.haml +17 -0
- data/test/haml/templates/partial_layout.haml +3 -0
- data/test/haml/templates/partialize.haml +1 -0
- data/test/haml/templates/partials.haml +12 -0
- data/test/haml/templates/render_layout.haml +2 -0
- data/test/haml/templates/silent_script.haml +40 -0
- data/test/haml/templates/standard.haml +42 -0
- data/test/haml/templates/standard_ugly.haml +1 -0
- data/test/haml/templates/tag_parsing.haml +21 -0
- data/test/haml/templates/very_basic.haml +4 -0
- data/test/haml/templates/whitespace_handling.haml +87 -0
- data/test/linked_rails.rb +12 -0
- data/test/sass/css2sass_test.rb +193 -0
- data/test/sass/engine_test.rb +786 -0
- data/test/sass/functions_test.rb +96 -0
- data/test/sass/more_results/more1.css +9 -0
- data/test/sass/more_results/more1_with_line_comments.css +26 -0
- data/test/sass/more_results/more_import.css +29 -0
- data/test/sass/more_templates/_more_partial.sass +2 -0
- data/test/sass/more_templates/more1.sass +23 -0
- data/test/sass/more_templates/more_import.sass +11 -0
- data/test/sass/plugin_test.rb +208 -0
- data/test/sass/results/alt.css +4 -0
- data/test/sass/results/basic.css +9 -0
- data/test/sass/results/compact.css +5 -0
- data/test/sass/results/complex.css +87 -0
- data/test/sass/results/compressed.css +1 -0
- data/test/sass/results/expanded.css +19 -0
- data/test/sass/results/import.css +29 -0
- data/test/sass/results/line_numbers.css +49 -0
- data/test/sass/results/mixins.css +95 -0
- data/test/sass/results/multiline.css +24 -0
- data/test/sass/results/nested.css +22 -0
- data/test/sass/results/parent_ref.css +13 -0
- data/test/sass/results/script.css +16 -0
- data/test/sass/results/subdir/nested_subdir/nested_subdir.css +1 -0
- data/test/sass/results/subdir/subdir.css +3 -0
- data/test/sass/results/units.css +11 -0
- data/test/sass/script_test.rb +153 -0
- data/test/sass/templates/_partial.sass +2 -0
- data/test/sass/templates/alt.sass +16 -0
- data/test/sass/templates/basic.sass +23 -0
- data/test/sass/templates/bork.sass +2 -0
- data/test/sass/templates/bork2.sass +2 -0
- data/test/sass/templates/compact.sass +17 -0
- data/test/sass/templates/complex.sass +309 -0
- data/test/sass/templates/compressed.sass +15 -0
- data/test/sass/templates/expanded.sass +17 -0
- data/test/sass/templates/import.sass +11 -0
- data/test/sass/templates/importee.sass +19 -0
- data/test/sass/templates/line_numbers.sass +13 -0
- data/test/sass/templates/mixins.sass +76 -0
- data/test/sass/templates/multiline.sass +20 -0
- data/test/sass/templates/nested.sass +25 -0
- data/test/sass/templates/parent_ref.sass +25 -0
- data/test/sass/templates/script.sass +101 -0
- data/test/sass/templates/subdir/nested_subdir/_nested_partial.sass +2 -0
- data/test/sass/templates/subdir/nested_subdir/nested_subdir.sass +3 -0
- data/test/sass/templates/subdir/subdir.sass +6 -0
- data/test/sass/templates/units.sass +11 -0
- data/test/test_helper.rb +21 -0
- metadata +245 -0
data/README.rdoc
ADDED
@@ -0,0 +1,332 @@
|
|
1
|
+
= Haml and Sass
|
2
|
+
|
3
|
+
Haml and Sass are templating engines
|
4
|
+
for the two most common types of documents on the web:
|
5
|
+
HTML and CSS, respectively.
|
6
|
+
They are designed to make it both easier and more pleasant
|
7
|
+
to code HTML and CSS documents,
|
8
|
+
by eliminating redundancy,
|
9
|
+
reflecting the underlying structure that the document represents,
|
10
|
+
and providing elegant, easily understandable, and powerful syntax.
|
11
|
+
|
12
|
+
== Using
|
13
|
+
|
14
|
+
There are several ways to use Haml and Sass.
|
15
|
+
They can be used as a plugin for Rails or Merb,
|
16
|
+
or embedded on their own in other applications.
|
17
|
+
The first step of all of these is to install the Haml gem:
|
18
|
+
|
19
|
+
gem install haml
|
20
|
+
|
21
|
+
To install Haml and Sass as a Rails plugin,
|
22
|
+
just run <tt>haml --rails path/to/rails/app</tt>
|
23
|
+
and both Haml and Sass will be installed.
|
24
|
+
Views with the <tt>.haml</tt> (or <tt>.html.haml</tt> for edge)
|
25
|
+
extension will automatically use Haml.
|
26
|
+
Sass is a little more complicated;
|
27
|
+
<tt>.sass</tt> files should be placed in public/stylesheets/sass,
|
28
|
+
where they'll be automatically compiled
|
29
|
+
to corresponding CSS files in public/stylesheets when needed
|
30
|
+
(the Sass template directory is customizable...
|
31
|
+
see the Sass module docs for details).
|
32
|
+
|
33
|
+
For Merb, <tt>.html.haml</tt> views will work without any further modification.
|
34
|
+
To enable Sass, you also need to add a dependency.
|
35
|
+
To do so, just add
|
36
|
+
|
37
|
+
dependency "merb-haml"
|
38
|
+
|
39
|
+
to config/dependencies.rb (or config/init.rb in a flat/very flat Merb application).
|
40
|
+
Then it'll work just like it does in Rails.
|
41
|
+
|
42
|
+
To use Haml and Sass programatically,
|
43
|
+
check out the RDocs for the Haml and Sass modules.
|
44
|
+
|
45
|
+
== Formatting
|
46
|
+
|
47
|
+
=== Haml
|
48
|
+
|
49
|
+
The most basic element of Haml
|
50
|
+
is a shorthand for creating HTML tags:
|
51
|
+
|
52
|
+
%tagname{:attr1 => 'value1', :attr2 => 'value2'} Contents
|
53
|
+
|
54
|
+
No end-tag is needed; Haml handles that automatically.
|
55
|
+
Adding <tt>class</tt> and <tt>id</tt> attributes is even easier.
|
56
|
+
Haml uses the same syntax as the CSS that styles the document:
|
57
|
+
|
58
|
+
%tagname#id.class
|
59
|
+
|
60
|
+
In fact, when you're using the <tt><div></tt> tag,
|
61
|
+
it becomes <em>even easier</em>.
|
62
|
+
Because <tt><div></tt> is such a common element,
|
63
|
+
a tag without a name defaults to a div. So
|
64
|
+
|
65
|
+
#foo Hello!
|
66
|
+
|
67
|
+
becomes
|
68
|
+
|
69
|
+
<div id='foo'>Hello!</div>
|
70
|
+
|
71
|
+
Haml uses indentation
|
72
|
+
to bring the individual elements to represent the HTML structure.
|
73
|
+
A tag's children are indented beneath than the parent tag.
|
74
|
+
Again, a closing tag is automatically added.
|
75
|
+
For example:
|
76
|
+
|
77
|
+
%ul
|
78
|
+
%li Salt
|
79
|
+
%li Pepper
|
80
|
+
|
81
|
+
becomes:
|
82
|
+
|
83
|
+
<ul>
|
84
|
+
<li>Salt</li>
|
85
|
+
<li>Pepper</li>
|
86
|
+
</ul>
|
87
|
+
|
88
|
+
You can also put plain text as a child of an element:
|
89
|
+
|
90
|
+
%p
|
91
|
+
Hello,
|
92
|
+
World!
|
93
|
+
|
94
|
+
It's also possible to embed Ruby code into Haml documents.
|
95
|
+
An equals sign, <tt>=</tt>, will output the result of the code.
|
96
|
+
A hyphen, <tt>-</tt>, will run the code but not output the result.
|
97
|
+
You can even use control statements
|
98
|
+
like <tt>if</tt> and <tt>while</tt>:
|
99
|
+
|
100
|
+
%p
|
101
|
+
Date/Time:
|
102
|
+
- now = DateTime.now
|
103
|
+
%strong= now
|
104
|
+
- if now > DateTime.parse("December 31, 2006")
|
105
|
+
= "Happy new " + "year!"
|
106
|
+
|
107
|
+
Haml provides far more tools than those presented here.
|
108
|
+
Check out the reference documentation in the Haml module.
|
109
|
+
|
110
|
+
=== Sass
|
111
|
+
|
112
|
+
At its most basic,
|
113
|
+
Sass is just another way of writing CSS.
|
114
|
+
Although it's very much like normal CSS,
|
115
|
+
the basic syntax offers a few helpful features:
|
116
|
+
tabulation indicates the attributes in a rule,
|
117
|
+
rather than non-DRY brackets;
|
118
|
+
and newlines indicate the end of an attribute,
|
119
|
+
rather than a semicolon.
|
120
|
+
For example:
|
121
|
+
|
122
|
+
#main
|
123
|
+
:background-color #f00
|
124
|
+
:width 98%
|
125
|
+
|
126
|
+
becomes:
|
127
|
+
|
128
|
+
#main {
|
129
|
+
background-color: #f00;
|
130
|
+
width: 98% }
|
131
|
+
|
132
|
+
However, Sass provides much more than a way to make CSS look nice.
|
133
|
+
In CSS, it's important to have accurate selectors,
|
134
|
+
so your styles don't just apply to everything.
|
135
|
+
However, in order to do this,
|
136
|
+
you need to use nested element selectors.
|
137
|
+
These get very ugly very quickly.
|
138
|
+
I'm sure everyone's had to write something like
|
139
|
+
"#main .sidebar .top p h1 a",
|
140
|
+
followed by
|
141
|
+
"#main .sidebar .top p h1 a:visited" and
|
142
|
+
"#main .sidebar .top p h1 a:hover".
|
143
|
+
Well, Sass gets rid of that.
|
144
|
+
Like Haml, it uses indentation to indicate the structure of the document.
|
145
|
+
So, what was:
|
146
|
+
|
147
|
+
#main {
|
148
|
+
width: 90%;
|
149
|
+
}
|
150
|
+
#main p {
|
151
|
+
border-style: solid;
|
152
|
+
border-width: 1px;
|
153
|
+
border-color: #00f;
|
154
|
+
}
|
155
|
+
#main p a {
|
156
|
+
text-decoration: none;
|
157
|
+
font-weight: bold;
|
158
|
+
}
|
159
|
+
#main p a:hover {
|
160
|
+
text-decoration: underline;
|
161
|
+
}
|
162
|
+
|
163
|
+
becomes:
|
164
|
+
|
165
|
+
#main
|
166
|
+
:width 90%
|
167
|
+
p
|
168
|
+
:border-style solid
|
169
|
+
:border-width 1px
|
170
|
+
:border-color #00f
|
171
|
+
a
|
172
|
+
:text-decoration none
|
173
|
+
:font-weight bold
|
174
|
+
a:hover
|
175
|
+
:text-decoration underline
|
176
|
+
|
177
|
+
Pretty nice, no? Well, it gets better.
|
178
|
+
One of the main complaints against CSS is that it doesn't allow variables.
|
179
|
+
What if have a color or a width you re-use all the time?
|
180
|
+
In CSS, you just have to re-type it each time,
|
181
|
+
which is a nightmare when you decide to change it later.
|
182
|
+
Not so for Sass!
|
183
|
+
You can use the "!" character to set variables.
|
184
|
+
Then, if you put "=" after your attribute name,
|
185
|
+
you can set it to a variable.
|
186
|
+
For example:
|
187
|
+
|
188
|
+
!note_bg= #55aaff
|
189
|
+
|
190
|
+
#main
|
191
|
+
:width 70%
|
192
|
+
.note
|
193
|
+
:background-color= !note_bg
|
194
|
+
p
|
195
|
+
:width 5em
|
196
|
+
:background-color= !note_bg
|
197
|
+
|
198
|
+
becomes:
|
199
|
+
|
200
|
+
#main {
|
201
|
+
width: 70%; }
|
202
|
+
#main .note {
|
203
|
+
background-color: #55aaff; }
|
204
|
+
#main p {
|
205
|
+
width: 5em;
|
206
|
+
background-color: #55aaff; }
|
207
|
+
|
208
|
+
You can even do simple arithmetic operations with variables,
|
209
|
+
adding numbers and even colors together:
|
210
|
+
|
211
|
+
!main_bg= #46ar12
|
212
|
+
!main_width= 40em
|
213
|
+
|
214
|
+
#main
|
215
|
+
:background-color= !main_bg
|
216
|
+
:width= !main_width
|
217
|
+
.sidebar
|
218
|
+
:background-color= !main_bg + #333333
|
219
|
+
:width= !main_width - 25em
|
220
|
+
|
221
|
+
becomes:
|
222
|
+
|
223
|
+
#main {
|
224
|
+
background-color: #46a312;
|
225
|
+
width: 40em; }
|
226
|
+
#main .sidebar {
|
227
|
+
background-color: #79d645;
|
228
|
+
width: 15em; }
|
229
|
+
|
230
|
+
Taking the idea of variables a bit further are mixins.
|
231
|
+
These let you group whole swathes of CSS attributes into a single
|
232
|
+
directive and then include those anywhere you want:
|
233
|
+
|
234
|
+
=blue-border
|
235
|
+
:border
|
236
|
+
:color blue
|
237
|
+
:width 2px
|
238
|
+
:style dotted
|
239
|
+
|
240
|
+
.comment
|
241
|
+
+blue-border
|
242
|
+
:padding 2px
|
243
|
+
:margin 10px 0
|
244
|
+
|
245
|
+
.reply
|
246
|
+
+blue-border
|
247
|
+
|
248
|
+
becomes:
|
249
|
+
|
250
|
+
.comment {
|
251
|
+
border-color: blue;
|
252
|
+
border-width: 2px;
|
253
|
+
border-style: dotted;
|
254
|
+
padding: 2px;
|
255
|
+
margin: 10px 0;
|
256
|
+
}
|
257
|
+
|
258
|
+
.reply {
|
259
|
+
border-color: blue;
|
260
|
+
border-width: 2px;
|
261
|
+
border-style: dotted;
|
262
|
+
}
|
263
|
+
|
264
|
+
A comprehensive list of features is in
|
265
|
+
the documentation for the Sass module.
|
266
|
+
|
267
|
+
== Indentation
|
268
|
+
|
269
|
+
Indentation can be made up of one or more tabs or spaces.
|
270
|
+
However, indentation must be consistent within a given document.
|
271
|
+
Hard tabs and spaces can't be mixed,
|
272
|
+
and the same number of tabs or spaces must be used throughout.
|
273
|
+
|
274
|
+
== Executables
|
275
|
+
|
276
|
+
The Haml gem includes several executables that are useful
|
277
|
+
for dealing with Haml and Sass from the command line.
|
278
|
+
|
279
|
+
=== +haml+
|
280
|
+
|
281
|
+
The +haml+ executable transforms a source Haml file into HTML.
|
282
|
+
See <tt>haml --help</tt> for further information and options.
|
283
|
+
|
284
|
+
=== +sass+
|
285
|
+
|
286
|
+
The +sass+ executable transforms a source Sass file into CSS.
|
287
|
+
See <tt>sass --help</tt> for further information and options.
|
288
|
+
|
289
|
+
=== <tt>html2haml</tt>
|
290
|
+
|
291
|
+
The <tt>html2haml</tt> executable attempts to transform HTML,
|
292
|
+
optionally with ERB markup, into Haml code.
|
293
|
+
Since HTML is so variable, this transformation is not always perfect;
|
294
|
+
it's a good idea to have a human check the output of this tool.
|
295
|
+
See <tt>html2haml --help</tt> for further information and options.
|
296
|
+
|
297
|
+
=== <tt>css2sass</tt>
|
298
|
+
|
299
|
+
The <tt>css2sass</tt> executable attempts to transform CSS into Sass code.
|
300
|
+
This transformation attempts to use Sass nesting where possible.
|
301
|
+
See <tt>css2sass --help</tt> for further information and options.
|
302
|
+
|
303
|
+
== Authors
|
304
|
+
|
305
|
+
Haml and Sass are designed by Hampton Catlin (hcatlin) and he is the author
|
306
|
+
of the original implementation. However, Hampton doesn't even know his way
|
307
|
+
around the code anymore and mostly just concentrates on the language issues.
|
308
|
+
Hampton lives in Toronto, Ontario (though he's an American by birth) and
|
309
|
+
is a partner at Unspace Interactive.
|
310
|
+
|
311
|
+
Nathan Weizenbaum is the primary maintainer and architect of the "modern" Ruby
|
312
|
+
implementation of Haml. His hard work has kept the project alive by endlessly
|
313
|
+
answering forum posts, fixing bugs, refactoring, finding speed improvements,
|
314
|
+
writing documentation, implementing new features, and getting Hampton
|
315
|
+
coffee (a fitting task for a boy-genius). Nathan lives in Seattle, Washington and
|
316
|
+
while not being a student at University of Washington he consults for
|
317
|
+
Unspace Interactive and Microsoft.
|
318
|
+
|
319
|
+
Chris Eppstein is a core contributor to Sass and the creator of Compass, the first
|
320
|
+
Sass-based framework. Chris focuses on making Sass more powerful, easy to use, and
|
321
|
+
on ways to speed its adoption through the web development community. Chris lives in
|
322
|
+
San Jose, CA with his wife and daughter. He is the Software Architect for Caring.com,
|
323
|
+
a website devoted to the 34 Million caregivers whose parents are sick or elderly,
|
324
|
+
that uses Haml and Sass.
|
325
|
+
|
326
|
+
If you use this software, you must pay Hampton a compliment. And
|
327
|
+
buy Nathan some jelly beans. Maybe pet a kitten. Yeah. Pet that kitty.
|
328
|
+
|
329
|
+
Some of the work on Haml was supported by Unspace Interactive.
|
330
|
+
|
331
|
+
Beyond that, the implementation is licensed under the MIT License.
|
332
|
+
Ok, fine, I guess that means compliments aren't *required*.
|
data/bin/css2sass
ADDED
data/bin/haml
ADDED
data/bin/html2haml
ADDED
data/bin/sass
ADDED
data/lib/haml/buffer.rb
ADDED
@@ -0,0 +1,255 @@
|
|
1
|
+
module Haml
|
2
|
+
# This class is used only internally. It holds the buffer of XHTML that
|
3
|
+
# is eventually output by Haml::Engine's to_html method. It's called
|
4
|
+
# from within the precompiled code, and helps reduce the amount of
|
5
|
+
# processing done within instance_eval'd code.
|
6
|
+
class Buffer
|
7
|
+
include Haml::Helpers
|
8
|
+
include Haml::Util
|
9
|
+
|
10
|
+
# The string that holds the compiled XHTML. This is aliased as
|
11
|
+
# _erbout for compatibility with ERB-specific code.
|
12
|
+
attr_accessor :buffer
|
13
|
+
|
14
|
+
# The options hash passed in from Haml::Engine.
|
15
|
+
attr_accessor :options
|
16
|
+
|
17
|
+
# The Buffer for the enclosing Haml document.
|
18
|
+
# This is set for partials and similar sorts of nested templates.
|
19
|
+
# It's nil at the top level (see #toplevel?).
|
20
|
+
attr_accessor :upper
|
21
|
+
|
22
|
+
# See #active?
|
23
|
+
attr_writer :active
|
24
|
+
|
25
|
+
# True if the format is XHTML
|
26
|
+
def xhtml?
|
27
|
+
not html?
|
28
|
+
end
|
29
|
+
|
30
|
+
# True if the format is any flavor of HTML
|
31
|
+
def html?
|
32
|
+
html4? or html5?
|
33
|
+
end
|
34
|
+
|
35
|
+
# True if the format is HTML4
|
36
|
+
def html4?
|
37
|
+
@options[:format] == :html4
|
38
|
+
end
|
39
|
+
|
40
|
+
# True if the format is HTML5
|
41
|
+
def html5?
|
42
|
+
@options[:format] == :html5
|
43
|
+
end
|
44
|
+
|
45
|
+
# True if this buffer is a top-level template,
|
46
|
+
# as opposed to a nested partial.
|
47
|
+
def toplevel?
|
48
|
+
upper.nil?
|
49
|
+
end
|
50
|
+
|
51
|
+
# True if this buffer is currently being used to render a Haml template.
|
52
|
+
# However, this returns false if a subtemplate is being rendered,
|
53
|
+
# even if it's a subtemplate of this buffer's template.
|
54
|
+
def active?
|
55
|
+
@active
|
56
|
+
end
|
57
|
+
|
58
|
+
# Gets the current tabulation of the document.
|
59
|
+
def tabulation
|
60
|
+
@real_tabs + @tabulation
|
61
|
+
end
|
62
|
+
|
63
|
+
# Sets the current tabulation of the document.
|
64
|
+
def tabulation=(val)
|
65
|
+
val = val - @real_tabs
|
66
|
+
@tabulation = val > -1 ? val : 0
|
67
|
+
end
|
68
|
+
|
69
|
+
# Creates a new buffer.
|
70
|
+
def initialize(upper = nil, options = {})
|
71
|
+
@active = true
|
72
|
+
@upper = upper
|
73
|
+
@options = {
|
74
|
+
:attr_wrapper => "'",
|
75
|
+
:ugly => false,
|
76
|
+
:format => :xhtml
|
77
|
+
}.merge options
|
78
|
+
@buffer = ""
|
79
|
+
@tabulation = 0
|
80
|
+
|
81
|
+
# The number of tabs that Engine thinks we should have
|
82
|
+
# @real_tabs + @tabulation is the number of tabs actually output
|
83
|
+
@real_tabs = 0
|
84
|
+
end
|
85
|
+
|
86
|
+
def push_text(text, tab_change, dont_tab_up)
|
87
|
+
if @tabulation > 0
|
88
|
+
# Have to push every line in by the extra user set tabulation.
|
89
|
+
# Don't push lines with just whitespace, though,
|
90
|
+
# because that screws up precompiled indentation.
|
91
|
+
text.gsub!(/^(?!\s+$)/m, tabs)
|
92
|
+
text.sub!(tabs, '') if dont_tab_up
|
93
|
+
end
|
94
|
+
|
95
|
+
@buffer << text
|
96
|
+
@real_tabs += tab_change
|
97
|
+
end
|
98
|
+
|
99
|
+
def adjust_tabs(tab_change)
|
100
|
+
@real_tabs += tab_change
|
101
|
+
end
|
102
|
+
|
103
|
+
Haml::Util.def_static_method(self, :format_script, [:result],
|
104
|
+
:preserve_script, :in_tag, :preserve_tag, :escape_html,
|
105
|
+
:nuke_inner_whitespace, :interpolated, :ugly, <<RUBY)
|
106
|
+
<% unless ugly %>
|
107
|
+
# If we're interpolated,
|
108
|
+
# then the custom tabulation is handled in #push_text.
|
109
|
+
# The easiest way to avoid it here is to reset @tabulation.
|
110
|
+
<% if interpolated %>
|
111
|
+
old_tabulation = @tabulation
|
112
|
+
@tabulation = 0
|
113
|
+
<% end %>
|
114
|
+
|
115
|
+
tabulation = @real_tabs
|
116
|
+
result = result.to_s.<% if nuke_inner_whitespace %>strip<% else %>rstrip<% end %>
|
117
|
+
<% else %>
|
118
|
+
result = result.to_s<% if nuke_inner_whitespace %>.strip<% end %>
|
119
|
+
<% end %>
|
120
|
+
|
121
|
+
<% if escape_html %> result = html_escape(result) <% end %>
|
122
|
+
|
123
|
+
<% if preserve_tag %>
|
124
|
+
result = Haml::Helpers.preserve(result)
|
125
|
+
<% elsif preserve_script %>
|
126
|
+
result = Haml::Helpers.find_and_preserve(result, options[:preserve])
|
127
|
+
<% end %>
|
128
|
+
|
129
|
+
<% if ugly %>
|
130
|
+
return result
|
131
|
+
<% else %>
|
132
|
+
|
133
|
+
has_newline = result.include?("\\n")
|
134
|
+
<% if in_tag && !nuke_inner_whitespace %>
|
135
|
+
<% unless preserve_tag %> if !has_newline <% end %>
|
136
|
+
@real_tabs -= 1
|
137
|
+
<% if interpolated %> @tabulation = old_tabulation <% end %>
|
138
|
+
return result
|
139
|
+
<% unless preserve_tag %> end <% end %>
|
140
|
+
<% end %>
|
141
|
+
|
142
|
+
# Precompiled tabulation may be wrong
|
143
|
+
<% if !interpolated && !in_tag %>
|
144
|
+
result = tabs + result if @tabulation > 0
|
145
|
+
<% end %>
|
146
|
+
|
147
|
+
if has_newline
|
148
|
+
result = result.gsub "\\n", "\\n" + tabs(tabulation)
|
149
|
+
|
150
|
+
# Add tabulation if it wasn't precompiled
|
151
|
+
<% if in_tag && !nuke_inner_whitespace %> result = tabs(tabulation) + result <% end %>
|
152
|
+
end
|
153
|
+
|
154
|
+
<% if in_tag && !nuke_inner_whitespace %>
|
155
|
+
result = "\\n\#{result}\\n\#{tabs(tabulation-1)}"
|
156
|
+
@real_tabs -= 1
|
157
|
+
<% end %>
|
158
|
+
<% if interpolated %> @tabulation = old_tabulation <% end %>
|
159
|
+
result
|
160
|
+
<% end %>
|
161
|
+
RUBY
|
162
|
+
|
163
|
+
# Takes the various information about the opening tag for an
|
164
|
+
# element, formats it, and adds it to the buffer.
|
165
|
+
def open_tag(name, self_closing, try_one_line, preserve_tag, escape_html, class_id,
|
166
|
+
nuke_outer_whitespace, nuke_inner_whitespace, obj_ref, content, *attributes_hashes)
|
167
|
+
tabulation = @real_tabs
|
168
|
+
|
169
|
+
attributes = class_id
|
170
|
+
attributes_hashes.each do |old|
|
171
|
+
self.class.merge_attrs(attributes, to_hash(old.map {|k, v| [k.to_s, v]}))
|
172
|
+
end
|
173
|
+
self.class.merge_attrs(attributes, parse_object_ref(obj_ref)) if obj_ref
|
174
|
+
|
175
|
+
if self_closing && xhtml?
|
176
|
+
str = " />" + (nuke_outer_whitespace ? "" : "\n")
|
177
|
+
else
|
178
|
+
str = ">" + ((if self_closing && html?
|
179
|
+
nuke_outer_whitespace
|
180
|
+
else
|
181
|
+
try_one_line || preserve_tag || nuke_inner_whitespace
|
182
|
+
end) ? "" : "\n")
|
183
|
+
end
|
184
|
+
|
185
|
+
attributes = Precompiler.build_attributes(html?, @options[:attr_wrapper], attributes)
|
186
|
+
@buffer << "#{nuke_outer_whitespace || @options[:ugly] ? '' : tabs(tabulation)}<#{name}#{attributes}#{str}"
|
187
|
+
|
188
|
+
if content
|
189
|
+
@buffer << "#{content}</#{name}>" << (nuke_outer_whitespace ? "" : "\n")
|
190
|
+
return
|
191
|
+
end
|
192
|
+
|
193
|
+
@real_tabs += 1 unless self_closing || nuke_inner_whitespace
|
194
|
+
end
|
195
|
+
|
196
|
+
def self.merge_attrs(to, from)
|
197
|
+
if to['id'] && from['id']
|
198
|
+
to['id'] << '_' << from.delete('id')
|
199
|
+
elsif to['id'] || from['id']
|
200
|
+
from['id'] ||= to['id']
|
201
|
+
end
|
202
|
+
|
203
|
+
if to['class'] && from['class']
|
204
|
+
# Make sure we don't duplicate class names
|
205
|
+
from['class'] = (from['class'].split(' ') | to['class'].split(' ')).join(' ')
|
206
|
+
elsif to['class'] || from['class']
|
207
|
+
from['class'] ||= to['class']
|
208
|
+
end
|
209
|
+
|
210
|
+
to.merge!(from)
|
211
|
+
end
|
212
|
+
|
213
|
+
private
|
214
|
+
|
215
|
+
# Some of these methods are exposed as public class methods
|
216
|
+
# so they can be re-used in helpers.
|
217
|
+
|
218
|
+
@@tab_cache = {}
|
219
|
+
# Gets <tt>count</tt> tabs. Mostly for internal use.
|
220
|
+
def tabs(count = 0)
|
221
|
+
tabs = [count + @tabulation, 0].max
|
222
|
+
@@tab_cache[tabs] ||= ' ' * tabs
|
223
|
+
end
|
224
|
+
|
225
|
+
# Takes an array of objects and uses the class and id of the first
|
226
|
+
# one to create an attributes hash.
|
227
|
+
# The second object, if present, is used as a prefix,
|
228
|
+
# just like you can do with dom_id() and dom_class() in Rails
|
229
|
+
def parse_object_ref(ref)
|
230
|
+
prefix = ref[1]
|
231
|
+
ref = ref[0]
|
232
|
+
# Let's make sure the value isn't nil. If it is, return the default Hash.
|
233
|
+
return {} if ref.nil?
|
234
|
+
class_name = underscore(ref.class)
|
235
|
+
id = "#{class_name}_#{ref.id || 'new'}"
|
236
|
+
if prefix
|
237
|
+
class_name = "#{ prefix }_#{ class_name}"
|
238
|
+
id = "#{ prefix }_#{ id }"
|
239
|
+
end
|
240
|
+
|
241
|
+
{'id' => id, 'class' => class_name}
|
242
|
+
end
|
243
|
+
|
244
|
+
# Changes a word from camel case to underscores.
|
245
|
+
# Based on the method of the same name in Rails' Inflector,
|
246
|
+
# but copied here so it'll run properly without Rails.
|
247
|
+
def underscore(camel_cased_word)
|
248
|
+
camel_cased_word.to_s.gsub(/::/, '_').
|
249
|
+
gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
|
250
|
+
gsub(/([a-z\d])([A-Z])/,'\1_\2').
|
251
|
+
tr("-", "_").
|
252
|
+
downcase
|
253
|
+
end
|
254
|
+
end
|
255
|
+
end
|