Permalink
Browse files

added haml-js directly

  • Loading branch information...
liammclennan committed Mar 19, 2011
1 parent ca9cf08 commit ad53ffdbc2da2cd3a5e5ec168a14c27991c17311
Showing with 1,086 additions and 0 deletions.
  1. +47 −0 packages/haml-js/CHANGELOG.markdown
  2. +22 −0 packages/haml-js/LICENSE
  3. +205 −0 packages/haml-js/README.markdown
  4. +35 −0 packages/haml-js/lib/cli.js
  5. +506 −0 packages/haml-js/lib/haml.js
  6. +11 −0 packages/haml-js/package.json
  7. +4 −0 packages/haml-js/test/alt_attribs.haml
  8. +1 −0 packages/haml-js/test/alt_attribs.html
  9. +5 −0 packages/haml-js/test/div_nesting.haml
  10. +1 −0 packages/haml-js/test/div_nesting.html
  11. +5 −0 packages/haml-js/test/doctype.haml
  12. +5 −0 packages/haml-js/test/doctype.html
  13. +6 −0 packages/haml-js/test/embedded_code.haml
  14. +7 −0 packages/haml-js/test/embedded_code.html
  15. +7 −0 packages/haml-js/test/embedded_code.js
  16. +12 −0 packages/haml-js/test/foreach.haml
  17. +1 −0 packages/haml-js/test/foreach.html
  18. +10 −0 packages/haml-js/test/foreach.js
  19. +1 −0 packages/haml-js/test/meta.haml
  20. +1 −0 packages/haml-js/test/meta.html
  21. +6 −0 packages/haml-js/test/nanline.haml
  22. +2 −0 packages/haml-js/test/nanline.html
  23. +6 −0 packages/haml-js/test/nested_context.haml
  24. +1 −0 packages/haml-js/test/nested_context.html
  25. +8 −0 packages/haml-js/test/nested_context.js
  26. +8 −0 packages/haml-js/test/no_self_close_div.haml
  27. +9 −0 packages/haml-js/test/no_self_close_div.html
  28. +8 −0 packages/haml-js/test/non-string-attribs.haml
  29. +1 −0 packages/haml-js/test/non-string-attribs.html
  30. +12 −0 packages/haml-js/test/script_css.haml
  31. +15 −0 packages/haml-js/test/script_css.html
  32. +8 −0 packages/haml-js/test/self_close.haml
  33. +1 −0 packages/haml-js/test/self_close.html
  34. +5 −0 packages/haml-js/test/self_close.js
  35. +14 −0 packages/haml-js/test/standard.haml
  36. +3 −0 packages/haml-js/test/standard.html
  37. +12 −0 packages/haml-js/test/standard.js
  38. +23 −0 packages/haml-js/test/test-commonjs.js
  39. +52 −0 packages/haml-js/test/test.js
@@ -0,0 +1,47 @@
+# HAML-JS Changelog
+
+- **v0.2.5** - *2010-05-06* - NPM support
+
+ Fixed to work with Node Package Manager
+
+- **v0.2.4** - *2010-04-16* - Bug fixes, XML support
+
+ Allow for commas in calls to helpers in attributes. Also make haml more XML friendly.
+
+- **v0.2.3** - *2010-04-10* - Bug fixes
+
+ Fixed an issue where "content" html attributes got munched. (This broke meta tags)
+
+- **v0.2.2** - *2010-04-05* - Bug fixes
+
+ Fixed two issues where the parser incorrectly parsed blank lines and extra spaces in attribute blocks.
+
+- **v0.2.1** - *2010-04-01* - Minor speed tweak
+
+ `Haml()` now caches the eval step so that there is no eval in executing a compiled template. This should make things a bit faster.
+
+- **v0.2.0** - *2010-03-31* - Function based API, Safe whitespace, Code interpolation.
+
+ 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.
+
+ 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.
+
+ 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.
+
+- **0.1.2** - *2010-02-03* - Bug fixes, plugin aliases, CommonJS, and more...
+
+ 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.
+
+- **0.1.1** - *2010-01-09* - Add :css and :script plugins
+
+ Added two quick plugins that make working with javascript and css much easier.
+
+ - **0.1.0** - *2010-01-09* - Complete Rewrite
+
+ 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.
+
+ - **0.0.1** - *2009-12-16* - Initial release
+
+ 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.
+
+
View
@@ -0,0 +1,22 @@
+Copyright (c) 2009 Tim Caswell
+
+Permission is hereby granted, free of charge, to any person
+obtaining a copy of this software and associated documentation
+files (the "Software"), to deal in the Software without
+restriction, including without limitation the rights to use,
+copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the
+Software is furnished to do so, subject to the following
+conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,205 @@
+# haml-js - Server side templating language for JavaScript
+
+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][].
+
+## About the language
+
+Here is the first example(with a little extra added) from the [haml][] site converted to haml-js:
+
+**haml-js**
+
+ !!! XML
+ !!! strict
+ %html{ xmlns: "http://www.w3.org/1999/xhtml" }
+ %head
+ %title Sample haml template
+ %body
+ .profile
+ .left.column
+ #date= print_date()
+ #address= current_user.address
+ .right.column
+ #email= current_user.email
+ #bio= current_user.bio
+
+**html**
+
+ <?xml version='1.0' encoding='utf-8' ?>
+ <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
+ <html xmlns="http://www.w3.org/1999/xhtml"><head><title>Sample haml template
+ </title></head><body><div class="profile"><div class="left column"><div id="date">January 1, 2009
+ </div><div id="address">Richardson, TX
+ </div></div><div class="right column"><div id="email">tim@creationix.com
+ </div><div id="bio">Experienced software professional...
+ </div></div></div></body></html>
+
+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..
+
+## API
+
+### Haml(haml) -> template(locals) -> html
+
+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)
+
+Compile and store a template:
+
+ var main = Haml(main_haml);
+
+Then use it whenever you need a new version:
+
+ main({name: "Tim", age: 28});
+
+That's it. Haml templating made easy!
+
+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.
+
+
+### Haml.compile(text) -> JavaScript compiled template
+
+Given a haml template as raw text, this compiles it to a javascript expression
+that can later be eval'ed to get the final HTML.
+
+The following input:
+
+ #home
+ = title
+ %ul.menu
+ %li Go Home
+ %li Go Back
+
+Produces the following JavaScript expression:
+
+ "<div id=\"home\">" +
+ title +
+ "\n" +
+ "<ul class=\"menu\">" +
+ "<li>" +
+ "Go Home\n" +
+ "</li>" +
+ "<li>" +
+ "Go Back\n" +
+ "</li>" +
+ "</ul>" +
+ "</div>"
+
+### Haml.optimize(js) -> optimized JavaScript expression
+
+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.
+
+With the previous input it outputs:
+
+ "<div id=\"home\">" +
+ title +
+ "\n<ul class=\"menu\"><li>Go Home\n</li><li>Go Back\n</li></ul></div>"
+
+Notice how congruent static strings are merged into a single string literal when possible.
+
+### Haml.execute(js, context, locals) -> Executes a compiles template
+
+Context is the value of `this` in the template, and locals is a hash of local variables.
+
+### Haml.render(text, options) -> html text
+
+This is a convenience function that compiles and executes to html in one shot. Most casual users will want to use this function exclusively.
+
+The `text` parameter is the haml source already read from a file.
+
+The three recognized `options` are:
+
+ - **context**: This is the `this` context within the haml template.
+ - **locals**: This is an object that's used in the `with` scope. Basically it creates local variables and function accessible to the haml template.
+ - **optimize**: This is a flag to tell the compiler to use the extra optimizations.
+
+See [test.js][] for an example usage of Haml.render
+
+## Code interpolation
+
+New in 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.
+
+## Plugins
+
+There are plugins in the parser for things like inline script tags, css blocks, and support for if statements and for loops.
+
+### `:if` statements
+
+`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.
+
+ :if todolist.length > 20
+ %p Oh my, you are a busy fellow!
+
+### `:each` loops
+
+`: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__`.
+
+Here is an example over a simple array.
+
+ %ul.todolist
+ :each item in todolist
+ %li= item.description
+
+You can loop over the keys and values of objects too (Note the inner `:each` loop)
+
+ :each item in data
+ :if item.age < 100
+ %dl
+ :each name, value in item
+ %dt&= name
+ %dd&= value
+
+### `:css` and `:javascript` helpers.
+
+It's easy to embed javascript and css blocks in an haml document.
+
+ %head
+ :javascript
+ function greet(message) {
+ alert("Message from MCP: " + message);
+ }
+ %title Script and Css test
+ :css
+ body {
+ color: pink;
+ }
+ %body{ onload: "greet(\"I'm Pink\")" } COLOR ME PINK
+
+This compiles to the following HTML:
+
+ <head>
+ <script type="text/javascript">
+ //<![CDATA[
+ function greet(message) {
+ alert("Message from MCP: " + message);
+ }
+ //]]>
+ </script>
+ <title>Script and Css test
+ </title>
+ <style type="text/css">
+ body {
+ color: pink;
+ }
+ </style>
+ </head><body onload="greet(&quot;I'm Pink&quot;)"> COLOR ME PINK
+ </body>
+
+## Get Involved
+
+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.
+
+## About Performance
+
+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.
+
+## License
+
+Haml-js is [licensed][] under the [MIT license][].
+
+[MIT license]: http://creativecommons.org/licenses/MIT/
+[licensed]: http://github.com/creationix/haml-js/blob/master/LICENSE
+[jquery-haml]: http://github.com/creationix/jquery-haml
+[haml]: http://haml-lang.com/
+[test.js]: http://github.com/creationix/haml-js/blob/master/test/test.js
+
+
+
+
@@ -0,0 +1,35 @@
+
+var Haml = require('./haml');
+
+var readUntilEnd = function(stream, callback) {
+ var chunks = [];
+ stream.on('data', function(chunk) {
+ chunks.push(chunk.toString('utf-8'));
+ });
+ stream.on('end', function() {
+ callback(chunks.join(''));
+ });
+}
+
+readUntilEnd(process.openStdin(), function(haml) {
+ var result;
+
+ if (haml.length == 0) {
+ console.log("Error: HAML expected on stdin")
+ process.exit(1);
+ }
+
+ // --html
+ if ((process.argv.length >= 3) && (process.argv[2] == '--html')) {
+ result = Haml.render(haml);
+ }
+
+ // --js
+ else {
+ result = Haml.optimize(
+ Haml.compile(
+ haml));
+ }
+
+ process.stdout.write(result);
+});
Oops, something went wrong.

0 comments on commit ad53ffd

Please sign in to comment.