Skip to content

Commit

Permalink
upgrade to latest Haml-js
Browse files Browse the repository at this point in the history
including fixes for issue quirkey#22, non-unix line endings.
  • Loading branch information
stevenharman authored and quirkey committed Oct 2, 2010
1 parent 6eb6257 commit 8ebbc0b
Showing 1 changed file with 40 additions and 17 deletions.
57 changes: 40 additions & 17 deletions lib/plugins/sammy.haml.js
Expand Up @@ -2,11 +2,11 @@

/*
port of http://github.com/creationix/haml-js
version v0.2.2 - 2010-04-05
version v0.2.6pre - 2010-10-01
by Tim Caswell <tim@creationix.com>
*/

var matchers, self_close_tags, embedder;
var matchers, self_close_tags, embedder, forceXML;

function html_escape(text) {
return (text + "").
Expand All @@ -19,7 +19,7 @@
function render_attribs(attribs) {
var key, value, result = [];
for (key in attribs) {
if (key !== 'content' && attribs.hasOwnProperty(key)) {
if (key !== '_content' && attribs.hasOwnProperty(key)) {
switch (attribs[key]) {
case 'undefined':
case 'false':
Expand Down Expand Up @@ -63,7 +63,7 @@

if (!(l > 0 && (line.charAt(0) === '{' || line.charAt(0) === '('))) {
return {
content: line[0] === ' ' ? line.substr(1, l) : line
_content: line[0] === ' ' ? line.substr(1, l) : line
};
}
open = line.charAt(0);
Expand Down Expand Up @@ -122,16 +122,16 @@
}
}

if (c === open) {
if (c === open || c === "(") {
count += 1;
}
if (c === close) {
if (c === close || (count > 1 && c === ")")) {
count -= 1;
}
}
}
}
attributes.content = line.substr(i, line.length);
attributes._content = line.substr(i, line.length);
return attributes;
}

Expand Down Expand Up @@ -173,23 +173,23 @@
matchers = [
// html tags
{
regexp: /^(\s*)((?:[.#%][a-z_\-][a-z0-9_\-]*)+)(.*)$/i,
regexp: /^(\s*)((?:[.#%][a-z_\-][a-z0-9_:\-]*)+)(.*)$/i,
process: function () {
var tag, classes, ids, attribs, content;
tag = this.matches[2];
classes = tag.match(/\.([a-z_\-][a-z0-9_\-]*)/gi);
ids = tag.match(/\#([a-z_\-][a-z0-9_\-]*)/gi);
tag = tag.match(/\%([a-z_\-][a-z0-9_\-]*)/gi);
tag = tag.match(/\%([a-z_\-][a-z0-9_:\-]*)/gi);

// Default to <div> tag
tag = tag ? tag[0].substr(1, tag[0].length) : 'div';

attribs = this.matches[3];
if (attribs) {
attribs = parse_attribs(attribs);
if (attribs.content) {
this.contents.unshift(attribs.content.trim());
delete(attribs.content);
if (attribs._content) {
this.contents.unshift(attribs._content.trim());
delete(attribs._content);
}
} else {
attribs = {};
Expand Down Expand Up @@ -227,7 +227,7 @@
content = '';
}

if (self_close_tags.indexOf(tag) == -1) {
if (forceXML ? content.length > 0 : self_close_tags.indexOf(tag) == -1) {
return '"<' + tag + attribs + '>"' +
(content.length > 0 ? ' + \n' + content : "") +
' + \n"</' + tag + '>"';
Expand Down Expand Up @@ -356,7 +356,7 @@

// If lines is a string, turn it into an array
if (typeof lines === 'string') {
lines = lines.trim().split("\n");
lines = lines.trim().replace(/\n\r|\r/g, '\n').split('\n');
}

lines.forEach(function(line) {
Expand Down Expand Up @@ -441,7 +441,7 @@
buffer = [];
}
}
js.split("\n").forEach(function (line) {
js.replace(/\n\r|\r/g, '\n').split('\n').forEach(function (line) {
part = line.match(/^(\".*\")(\s*\+\s*)?$/);
if (!part) {
flush();
Expand All @@ -461,7 +461,31 @@
return new_js.join("\n");
};

function Haml(haml) {
function render(text, options) {
options = options || {};
text = text || "";
var js = compile(text);
if (options.optimize) {
js = Haml.optimize(js);
}
return execute(js, options.context || Haml, options.locals);
};

function execute(js, self, locals) {
return (function () {
with(locals || {}) {
try {
return eval("(" + js + ")");
} catch (e) {
return "\n<pre class='error'>" + html_escape(e.stack) + "</pre>\n";
}

}
}).call(self);
};

Haml = function Haml(haml, xml) {
forceXML = xml;
var js = optimize(compile(haml));
return new Function("locals",
html_escape + "\n" +
Expand All @@ -474,7 +498,6 @@
"}");
}


Sammy = Sammy || {};

// <tt>Sammy.Haml</tt> provides a quick way of using haml style templates in your app.
Expand Down

0 comments on commit 8ebbc0b

Please sign in to comment.