├── CHANGELOG.markdown
├── LICENSE
├── README.markdown
├── lib
├── cli.js
└── haml.js
├── package.json
├── test.haml
└── test
├── alt_attribs.haml
├── alt_attribs.html
├── blank.haml
├── blank.html
├── comments.haml
├── comments.html
├── css.haml
├── css.html
├── div_nesting.haml
├── div_nesting.html
├── doctype.haml
├── doctype.html
├── else.haml
├── else.html
├── embedded_code.haml
├── embedded_code.html
├── embedded_code.js
├── escaping.haml
├── escaping.html
├── escaping.js
├── foreach.haml
├── foreach.html
├── foreach.js
├── if.haml
├── if.html
├── if.js
├── interpolation.haml
├── interpolation.html
├── meta.haml
├── meta.html
├── nanline.haml
├── nanline.html
├── nested_context.haml
├── nested_context.html
├── nested_context.js
├── no_self_close_div.haml
├── no_self_close_div.html
├── non-string-attribs.haml
├── non-string-attribs.html
├── optional_attribs.haml
├── optional_attribs.html
├── other
├── custom_escape.haml
├── custom_escape.html
├── escape_by_default.haml
└── escape_by_default.html
├── raw.haml
├── raw.html
├── raw_complex.haml
├── raw_complex.html
├── script_css.haml
├── script_css.html
├── self_close.haml
├── self_close.html
├── self_close.js
├── standard.haml
├── standard.html
├── standard.js
├── test-commonjs.js
├── test.js
├── whitespace.haml
└── whitespace.html
/CHANGELOG.markdown:
--------------------------------------------------------------------------------
1 | # HAML-JS Changelog
2 |
3 | - **v0.4.0**
4 | Breaking Changes:
5 | Made interpolation #{} escaped by default. Use !{} for unsafe interpolation.
6 |
7 | New Features:
8 | * Optionally exclude `html_escape` function definition from every template -- provide your own escape function invocation string ("MyApp.htmlEscape") and it will be used instead, dramatically shrinking template sizes.
9 | * Optionally escape all output of `=` by default. Set the escapeHtmlByDefault configuration variable.
10 | * New never-escaped `!=` recommended for when you **want** to output strings that contain html.
11 | * More test coverage for interpolation and escaping
12 |
13 | Bugfix: "inside" whitespace was not concatenating properly in some cases.
14 |
15 | - **v0.3.0**
16 | New features:
17 | * Comments -- Haml comments, HTML comments, JavaScript Comments
18 | * Raw JS -- this lets you use if/else, switch, try/catch, et cetera
19 | in your views (use cautiously!)
20 | * Whitespace insertion -- Now, you can insert whitespace in and/or
21 | around tags using < and >. Check the docs.
22 | * Blank templates are now valid!
23 | * More test coverage
24 |
25 | - **v0.2.5** - *2010-05-06* - NPM support
26 |
27 | Fixed to work with Node Package Manager
28 |
29 | - **v0.2.4** - *2010-04-16* - Bug fixes, XML support
30 |
31 | Allow for commas in calls to helpers in attributes. Also make haml more XML friendly.
32 |
33 | - **v0.2.3** - *2010-04-10* - Bug fixes
34 |
35 | Fixed an issue where "content" html attributes got munched. (This broke meta tags)
36 |
37 | - **v0.2.2** - *2010-04-05* - Bug fixes
38 |
39 | Fixed two issues where the parser incorrectly parsed blank lines and extra spaces in attribute blocks.
40 |
41 | - **v0.2.1** - *2010-04-01* - Minor speed tweak
42 |
43 | `Haml()` now caches the eval step so that there is no eval in executing a compiled template. This should make things a bit faster.
44 |
45 | - **v0.2.0** - *2010-03-31* - Function based API, Safe whitespace, Code interpolation.
46 |
47 | At the request of some users, I've removed the new insertion into the generated html. This means that most html will be on one long line, but as an added advantage you won't have that extra whitespace next to your anchor labels messing up your visual display.
48 |
49 | Also I added string interpolation to every place I could fit it. This means you can do crazy stuff like interpolate within strings in attributes, in the body on plain text sections, and of course in javascript and css plugin blocks.
50 |
51 | In order to tame the API, I deprecated the four old interfaces `compile`, `optimize`, `execute` and `render`. The new API is that the Haml/exports object itself is now a function that takes in haml text and outputs a compiled, optimized, ready to execute function.
52 |
53 | - **0.1.2** - *2010-02-03* - Bug fixes, plugin aliases, CommonJS, and more...
54 |
55 | This is a big release with many improvements. First haml-js is now a CommonJS module and is in the Tusk repository. Thanks to Tom Robinson for helping with that. Some of the plugins got aliases for people who didn't like the original name. For example, you can now do `:javascript` instead of `:script` and `:for` instead of `:each`. There were many bug fixes now that the code is starting to be actually used by myself and others.
56 |
57 | - **0.1.1** - *2010-01-09* - Add :css and :script plugins
58 |
59 | Added two quick plugins that make working with javascript and css much easier.
60 |
61 | - **0.1.0** - *2010-01-09* - Complete Rewrite
62 |
63 | Rewrote the compiler to be recursive and compile to JavaScript code instead of JSON data structures. This fixes all the outstanding bugs and simplifies the code. Pending is restoring the `:script` and `:css` plugins.
64 |
65 | - **0.0.1** - *2009-12-16* - Initial release
66 |
67 | Change how haml is packaged. It is a pure JS function with no node dependencies. There is an exports hook for commonjs usability. It's now the responsibility of the script user to acquire the haml text.
68 |
69 |
70 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | Copyright (c) 2009 Tim Caswell
2 |
3 | Permission is hereby granted, free of charge, to any person
4 | obtaining a copy of this software and associated documentation
5 | files (the "Software"), to deal in the Software without
6 | restriction, including without limitation the rights to use,
7 | copy, modify, merge, publish, distribute, sublicense, and/or sell
8 | copies of the Software, and to permit persons to whom the
9 | Software is furnished to do so, subject to the following
10 | conditions:
11 |
12 | The above copyright notice and this permission notice shall be
13 | included in all copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
17 | OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
19 | HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20 | WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 | FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 | OTHER DEALINGS IN THE SOFTWARE.
23 |
--------------------------------------------------------------------------------
/README.markdown:
--------------------------------------------------------------------------------
1 | # haml-js - Server side templating language for JavaScript
2 |
3 | Ever wanted to use the excellent HAML syntax on a javascript project? Me too, so I made one!. This has most of the same functionality as the traditional [haml][].
4 |
5 | ## About the language
6 |
7 | Here is the first example(with a little extra added) from the [haml][] site converted to haml-js:
8 |
9 | **haml-js**
10 |
11 | !!! XML
12 | !!! strict
13 | %html{ xmlns: "http://www.w3.org/1999/xhtml" }
14 | %head
15 | %title Sample haml template
16 | %body
17 | .profile
18 | .left.column
19 | #date= print_date()
20 | #address= current_user.address
21 | .right.column
22 | #email= current_user.email
23 | #bio= current_user.bio
24 |
25 | **html**
26 |
27 |
28 |
29 |
Sample haml template
30 |
January 1, 2009
31 |
Richardson, TX
32 |
tim@creationix.com
33 |
Experienced software professional...
34 |
35 |
36 | Note that this works almost the same as ruby's [haml][], but doesn't pretty print the html. This would greatly slow down and complicate the code. If you really want pretty printed html, then I suggest writing one using the xml parser library and process the resulting html..
37 |
38 | ## API
39 |
40 | ### Haml(haml) -> template(locals) -> html
41 |
42 | This is the new (as of 0.2.0) way to generate haml templates. A haml template is a live function that takes in "this" context and a "locals" variable. This compile step takes a few milliseconds to complete so it should be done at startup and the resulting function should be cached. Then to use the template function you simply call it with the desired local variables and it will output html at blazing speeds (we're talking millions per second on my 13" MBP)
43 |
44 | Compile and store a template:
45 |
46 | var main = Haml(main_haml);
47 |
48 | Then use it whenever you need a new version:
49 |
50 | main({name: "Tim", age: 28});
51 |
52 | That's it. Haml templating made easy!
53 |
54 | If you want to store the generated javascript to a file to skip the compile step later on you can either decompile the template function or use the `compile` and `optimize` advanced functions directly.
55 |
56 |
57 | ### Haml.compile(text) -> JavaScript compiled template
58 |
59 | Given a haml template as raw text, this compiles it to a javascript expression
60 | that can later be eval'ed to get the final HTML.
61 |
62 | The following input:
63 |
64 | #home
65 | = title
66 | %ul.menu
67 | %li Go Home
68 | %li Go Back
69 |
70 | Produces the following JavaScript expression:
71 |
72 | "
" +
73 | title +
74 | "\n" +
75 | "
" +
76 | "
" +
77 | "Go Home\n" +
78 | "
" +
79 | "
" +
80 | "Go Back\n" +
81 | "
" +
82 | "
" +
83 | "
"
84 |
85 | ### Haml.optimize(js) -> optimized JavaScript expression
86 |
87 | Takes the output of compile and optimizes it to run faster with the tradeoff of longer compile time. This is useful for framework developers wanting to use haml in their framework and want to cache the compiled templates for performance.
88 |
89 | With the previous input it outputs:
90 |
91 | "
" +
92 | title +
93 | "\n
Go Home\n
Go Back\n
"
94 |
95 | Notice how congruent static strings are merged into a single string literal when possible.
96 |
97 | ### Haml.execute(js, context, locals) -> Executes a compiled template
98 |
99 | Context is the value of `this` in the template, and locals is a hash of local variables.
100 |
101 | ### Haml.render(text, options) -> html text
102 |
103 | This is a convenience function that compiles and executes to html in one shot. Most casual users will want to use this function exclusively.
104 |
105 | The `text` parameter is the haml source already read from a file.
106 |
107 | The three recognized `options` are:
108 |
109 | - **context**: This is the `this` context within the haml template.
110 | - **locals**: This is an object that's used in the `with` scope. Basically it creates local variables and function accessible to the haml template.
111 | - **optimize**: This is a flag to tell the compiler to use the extra optimizations.
112 |
113 | See [test.js][] for an example usage of Haml.render
114 |
115 | ## Executable JavaScript (not output)
116 |
117 | New in version 0.2.6 is the ability to embed javascript in your template function. This lets you do variable assignments, if/else, switch statements, and even define functions. In Haml.js, execution blocks begin with a `-` and define a raw js block. This behaves slightly differently from Ruby's Haml. The advantage is that you can easily have multi-line executable blocks and comments, but the downside is that that you have to "outdent" the haml if you want to output from within a javascript block.
118 |
119 | Simple example:
120 |
121 | - var area = 0.5 * length * height
122 | .area= area
123 |
124 | Multi-line example:
125 |
126 | - var obj = {
127 | area: 0.5 * b * h,
128 | r: opposite / adjacent
129 | }
130 | .triangle-details Area is: #{area} and the ratio is: #{r}
131 |
132 | "Outdent" the haml in a javascript block (the "goodbye" div is not rendered!)
133 |
134 | .conditional
135 | - var a = "strings are truthy"
136 | - if(a){
137 | .hello
138 | - } else{
139 | .goodbye
140 | - }
141 |
142 | You can even define functions:
143 |
144 | - function b(item){
145 | .item
146 | %b= item
147 | %span.length= item.length
148 | - }
149 | - b("Hi")
150 | - b("World")
151 |
152 | This outputs:
153 |
154 |
Hi2
World5
155 |
156 | Please see test/raw_complex.haml for more details and examples.
157 |
158 | ## Comments
159 |
160 | Comments that will **not** appear in the compiled JS function nor the output begin with `-#`
161 |
162 | -# This is a comment
163 | - # This is a syntax error because of the extraneous space between the - and #.
164 |
165 | If you want to have comments that will be in the compiled JS function but *NOT* the final HTML output:
166 |
167 | - /*
168 | here we can have a comment that will not be output. Since executable-JS is block-level,
169 | we can have as much comment as we want, and it will not be output to html */
170 |
171 | If you want an HTML comment that **WILL** be in the final HTML, begin with `/`
172 |
173 | ## Whitespace
174 |
175 | By default, Haml.js **has no whitespace between tags**. In this way, Haml.js is the opposite of Haml in Ruby. You can insert whitespace around or inside tags with `>` and `<`, respectively.
176 |
177 | Most commonly, you want to have an `a` or `span` with whitespace around it:
178 |
179 | Download the file
180 | %a(href="/home")> here
181 | now.
182 |
183 | Will produce:
184 |
185 | Download the file here now.
186 |
187 | You can also combine them if you want to have whitespace around and inside your tag.
188 |
189 | %span<> This will have space in and around it.
190 | %span>< This will, too.
191 | %span><= "also works with code".toUpperCase()
192 |
193 | Please see `test/whitespace.haml` for more examples.
194 |
195 | ## Code interpolation
196 |
197 | As of version 0.2.0 there is string interpolation throughout. This means that the body of regular text areas can have embedded code. This is true for attributes and the contents of plugins like javascript and markdown also. If you notice an area that doesn't support interpolation and it should then send me a note and I'll add it.
198 |
199 | For interpolation, you may use `#{}` for escaped interpolation or `!{}` for unsafe interpolation.
200 |
201 | ## Html Escaping / Sanitizer
202 |
203 | You probably don't want to put unescaped user input right into your html. http://xkcd.com/327/ HTML/XSS sanitization is the new "Bobby Tables."
204 |
205 | Let's assume we have a malicious username: `name = ""`
206 |
207 | Always unsafe:
208 |
209 | %span!= name
210 |
211 |
212 |
213 | Always safe:
214 |
215 | %span&= name
216 | <script>...</script>
217 |
218 | Sometimes safe:
219 |
220 | %span= name
221 |
222 | The behavior of `=` depends on the setting of the `escapeHtmlByDefault` configuration variable. To make `=` safe, call Haml like this:
223 |
224 | Haml(src, {escapeHtmlByDefault: true})
225 |
226 | ## Plugins
227 |
228 | There are plugins in the parser for things like inline script tags, css blocks, and support for if statements and for loops.
229 |
230 | ### `:if/:else` statements
231 |
232 | `if` statements evaluate a condition for truthiness (as opposed to a strict comparison to `true`) and includes the content inside the block if it's truthy. An optional `else` is also supported.
233 |
234 | :if todolist.length > 20
235 | %p Oh my, you are a busy fellow!
236 |
237 | :if val == selectedVal
238 | %option{value: val, selected: true}= val
239 | :else
240 | %option{value: val}= val
241 |
242 | ### `:each` loops
243 |
244 | `:each` loops allow you to loop over a collection including a block of content once for each item. You need to what variable to pull the data from and where to put the index and value. The index variable is optional and defaults to `__key__`.
245 |
246 | Here is an example over a simple array.
247 |
248 | %ul.todolist
249 | :each item in todolist
250 | %li= item.description
251 |
252 | You can loop over the keys and values of objects too (Note the inner `:each` loop)
253 |
254 | :each item in data
255 | :if item.age < 100
256 | %dl
257 | :each name, value in item
258 | %dt&= name
259 | %dd&= value
260 |
261 | ### `:css` and `:script` helpers.
262 |
263 | It's easy to embed script and css tags in an haml document. Note that both `:script` and `:javascript` will work.
264 |
265 | %head
266 | :javascript
267 | function greet(message) {
268 | alert("Message from MCP: " + message);
269 | }
270 | %title Script and Css test
271 | :css
272 | body {
273 | color: pink;
274 | }
275 | %body{ onload: "greet(\"I'm Pink\")" } COLOR ME PINK
276 |
277 | This compiles to the following HTML:
278 |
279 |
280 |
287 | Script and Css test
288 |
289 |
294 | COLOR ME PINK
295 |
296 |
297 |
298 | ## Custom Escaper
299 |
300 | By default, Haml(src) returns a completely self-sufficient function, including a nested `html_escape` function. However, repeating the html_escape function definition in each of your templates is going to use more size than necessary. So, you may pass the name of a custom escaper in an optional config variable.
301 |
302 | Haml(src, {customEscape: "MyApp.esc"})
303 |
304 | Then, the output template function definition will call `MyApp.esc(string)` and will omit the `html_escape` function definition. Haml.html_escape exposes the default escape function. If you are going to render your templates in the same context where you compile them (for instance, if you are only rendering them on the server side,) it might make sense to use `Haml(src, {customEscape: "Haml.html_escape"})`
305 |
306 | ## Get Involved
307 |
308 | If you want to use this project and something is missing then send me a message. I'm very busy and have several open source projects I manage. I'll contribute to this project as I have time, but if there is more interest for some particular aspect, I'll work on it a lot faster. Also you're welcome to fork this project and send me patches/pull-requests.
309 |
310 | ## About Performance
311 |
312 | The haml compiler isn't built for speed, it's built for maintainability. The actual generated templates, however are blazing fast. I benchmarked them with over 65 million renders per second on a small (20 line) template with some dynamic data on my laptop. Compare this to the 629 compiles per second I got out of the compiler. The idea is that you pre-compile your templates and reuse them on every request. While 629 per second is nothing compared to 65 million, that still means that your server with over 600 different views can boot up in about a second. I think that's fine for something that only happens every few weeks.
313 |
314 | ## License
315 |
316 | Haml-js is [licensed][] under the [MIT license][].
317 |
318 | [MIT license]: http://creativecommons.org/licenses/MIT/
319 | [licensed]: http://github.com/creationix/haml-js/blob/master/LICENSE
320 | [jquery-haml]: http://github.com/creationix/jquery-haml
321 | [haml]: http://haml.info/
322 | [test.js]: http://github.com/creationix/haml-js/blob/master/test/test.js
323 |
--------------------------------------------------------------------------------
/lib/cli.js:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env node
2 |
3 | var Haml = require('./haml');
4 |
5 | var readUntilEnd = function(stream, callback) {
6 | var chunks = [];
7 | stream.on('data', function(chunk) {
8 | chunks.push(chunk.toString('utf-8'));
9 | });
10 | stream.on('end', function() {
11 | callback(chunks.join(''));
12 | });
13 | }
14 |
15 | readUntilEnd(process.openStdin(), function(haml) {
16 | var result;
17 |
18 | if (haml.length == 0) {
19 | console.log("Error: HAML expected on stdin")
20 | process.exit(1);
21 | }
22 |
23 | // --html
24 | if ((process.argv.length >= 3) && (process.argv[2] == '--html')) {
25 | result = Haml.render(haml);
26 | }
27 |
28 | // --js
29 | else {
30 | result = Haml.optimize(
31 | Haml.compile(
32 | haml));
33 | }
34 |
35 | process.stdout.write(result);
36 | });
37 |
--------------------------------------------------------------------------------
/lib/haml.js:
--------------------------------------------------------------------------------
1 | var Haml;
2 |
3 | (function () {
4 |
5 | var matchers, self_close_tags, embedder, forceXML, escaperName = 'html_escape', escapeHtmlByDefault;
6 |
7 | function html_escape(text) {
8 | return (text + "").
9 | replace(/&/g, "&").
10 | replace(//g, ">").
12 | replace(/\"/g, """);
13 | }
14 |
15 | function render_attribs(attribs) {
16 | var key, value, result = [];
17 | for (key in attribs) {
18 | if (key[key.length - 1] == "?") {
19 | value = escaperName + '(' + attribs[key] + ')';
20 | result.push('" + (' + attribs[key] + ' ? " ' + key.replace('?', '') + '=\\"" + ' + value + ' + "\\"" : "") + "');
21 | continue;
22 | }
23 | if (key !== '_content' && attribs.hasOwnProperty(key)) {
24 | switch (attribs[key]) {
25 | case 'undefined':
26 | case 'false':
27 | case 'null':
28 | case '""':
29 | break;
30 | default:
31 | try {
32 | value = JSON.parse("[" + attribs[key] +"]")[0];
33 | if (value === true) {
34 | value = key;
35 | } else if (typeof value === 'string' && embedder.test(value)) {
36 | value = '" +\n' + parse_interpol(html_escape(value)) + ' +\n"';
37 | } else {
38 | value = html_escape(value);
39 | }
40 | result.push(" " + key + '=\\"' + value + '\\"');
41 | } catch (e) {
42 | result.push(" " + key + '=\\"" + '+escaperName+'(' + attribs[key] + ') + "\\"');
43 | }
44 | }
45 | }
46 | }
47 | return result.join("");
48 | }
49 |
50 | // Parse the attribute block using a state machine
51 | function parse_attribs(line) {
52 | var attributes = {},
53 | l = line.length,
54 | i, c,
55 | count = 1,
56 | quote = false,
57 | skip = false,
58 | open, close, joiner, seperator,
59 | pair = {
60 | start: 1,
61 | middle: null,
62 | end: null
63 | };
64 |
65 | if (!(l > 0 && (line.charAt(0) === '{' || line.charAt(0) === '('))) {
66 | return {
67 | _content: line[0] === ' ' ? line.substr(1, l) : line
68 | };
69 | }
70 | open = line.charAt(0);
71 | close = (open === '{') ? '}' : ')';
72 | joiner = (open === '{') ? ':' : '=';
73 | seperator = (open === '{') ? ',' : ' ';
74 |
75 | function process_pair() {
76 | if (typeof pair.start === 'number' &&
77 | typeof pair.middle === 'number' &&
78 | typeof pair.end === 'number') {
79 | var key = line.substr(pair.start, pair.middle - pair.start).trim(),
80 | value = line.substr(pair.middle + 1, pair.end - pair.middle - 1).trim();
81 | attributes[key] = value;
82 | }
83 | pair = {
84 | start: null,
85 | middle: null,
86 | end: null
87 | };
88 | }
89 |
90 | for (i = 1; count > 0; i += 1) {
91 |
92 | // If we reach the end of the line, then there is a problem
93 | if (i > l) {
94 | throw "Malformed attribute block";
95 | }
96 |
97 | c = line.charAt(i);
98 | if (skip) {
99 | skip = false;
100 | } else {
101 | if (quote) {
102 | if (c === '\\') {
103 | skip = true;
104 | }
105 | if (c === quote) {
106 | quote = false;
107 | }
108 | } else {
109 | if (c === '"' || c === "'") {
110 | quote = c;
111 | }
112 |
113 | if (count === 1) {
114 | if (c === joiner) {
115 | pair.middle = i;
116 | }
117 | if (c === seperator || c === close) {
118 | pair.end = i;
119 | process_pair();
120 | if (c === seperator) {
121 | pair.start = i + 1;
122 | }
123 | }
124 | }
125 |
126 | if (c === open || c === "(") {
127 | count += 1;
128 | }
129 | if (c === close || (count > 1 && c === ")")) {
130 | count -= 1;
131 | }
132 | }
133 | }
134 | }
135 | attributes._content = line.substr(i, line.length);
136 | return attributes;
137 | }
138 |
139 | // Split interpolated strings into an array of literals and code fragments.
140 | function parse_interpol(value) {
141 | var items = [],
142 | pos = 0,
143 | next = 0,
144 | match;
145 | while (true) {
146 | // Match up to embedded string
147 | next = value.substr(pos).search(embedder);
148 | if (next < 0) {
149 | if (pos < value.length) {
150 | items.push(JSON.stringify(value.substr(pos)));
151 | }
152 | break;
153 | }
154 | items.push(JSON.stringify(value.substr(pos, next)));
155 | pos += next;
156 |
157 | // Match embedded string
158 | match = value.substr(pos).match(embedder);
159 | next = match[0].length;
160 | if (next < 0) { break; }
161 | if(match[1] === "#"){
162 | items.push(escaperName+"("+(match[2] || match[3])+")");
163 | }else{
164 | //unsafe!!!
165 | items.push(match[2] || match[3]);
166 | }
167 |
168 | pos += next;
169 | }
170 | return items.filter(function (part) { return part && part.length > 0}).join(" +\n");
171 | }
172 |
173 | // Used to find embedded code in interpolated strings.
174 | embedder = /([#!])\{([^}]*)\}/;
175 |
176 | self_close_tags = ["meta", "img", "link", "br", "hr", "input", "area", "base"];
177 |
178 | // All matchers' regexps should capture leading whitespace in first capture
179 | // and trailing content in last capture
180 | matchers = [
181 | // html tags
182 | {
183 | name: "html tags",
184 | regexp: /^(\s*)((?:[.#%][a-z_\-][a-z0-9_:\-]*)+)(.*)$/i,
185 | process: function () {
186 | var line_beginning, tag, classes, ids, attribs, content, whitespaceSpecifier, whitespace={}, output;
187 | line_beginning = this.matches[2];
188 | classes = line_beginning.match(/\.([a-z_\-][a-z0-9_\-]*)/gi);
189 | ids = line_beginning.match(/\#([a-z_\-][a-z0-9_\-]*)/gi);
190 | tag = line_beginning.match(/\%([a-z_\-][a-z0-9_:\-]*)/gi);
191 |
192 | // Default to
--------------------------------------------------------------------------------
/test/raw_complex.haml:
--------------------------------------------------------------------------------
1 | - /*
2 | here we can have a comment that will not be output.
3 | Since executable-JS is a block-level thing, we can
4 | have as much comment as we want */
5 |
6 |
7 | - /* now you can have arbitrary control logic
8 | This will output
1
2
3
9 | notice: the %div isn't indented! Explained below!
10 | */
11 |
12 | - for(var i=1; i < 4; i++){
13 | %div= i
14 | -}
15 |
16 |
17 | %h1 Woah!
18 |
19 | - /* we can include new variable declarations as well! */
20 | var someObj = {
21 | a: 1,
22 | b: 2
23 | }
24 | = someObj.b
25 |
26 |
27 | %br
28 |
29 |
30 | - /* this is going to be funky. i DO NOT expect you to do this.
31 | Here we will begin with a comment, then define our variable.
32 | Next we begin a function definition.
33 |
34 | We will use this function to output a div that contains a number.
35 | Successive calls should output the next number in the div.
36 |
37 | We want our function to output the value of the counter,
38 | so we "outdent" to escape the JavaScript block in haml.
39 | Note that we have not closed our function definition's
40 | parenthesis yet. So, even though we outdent, the
41 | concatenation statement will be in the function's body.
42 | */
43 |
44 | var counter = 0;
45 | function increment(){
46 | counter++;
47 | .count
48 | = counter
49 | -}
50 | - increment() /* the tags wil be appended to the buffer, so use - instead of = */
51 | - increment()
52 |
53 |
54 |
55 |
56 | - function b(item){
57 | .item
58 | %b= item
59 | %span.length= item.length
60 | - }
61 | - b("Hi")
62 | - b("World")
--------------------------------------------------------------------------------
/test/raw_complex.html:
--------------------------------------------------------------------------------
1 |