Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fix on Compiler class and fileRenderer function

  • Loading branch information...
commit aa99494ac4ab3988b5849b7a500e874f71793e70 1 parent 09bbd9e
@fadrizul authored
View
75 compiled/compiler.js
@@ -2,8 +2,9 @@
TwigJS
Author: Fadrizul H. <fadrizul[at]gmail.com>
*/
-var Compile, Compiler, exports, pr, x;
+var Compile, Compiler, exports, helpers, pr, x;
x = require("./regexes");
+helpers = require("./helpers");
pr = require("../dev/eyes");
Compiler = (function() {
function Compiler(node) {
@@ -12,44 +13,54 @@ Compiler = (function() {
this.type = node.type;
}
Compiler.prototype.compile = function() {
- var blockName, blocks, errMsg, parent, path, token, tokens, _i, _len;
+ var blockName, blocks, errMsg, i, merge, p, parent, parentBlock, path, token, tokens, _i, _len, _len2;
tokens = this.node.tokens;
blocks = this.node.blocks;
+ parent = {};
+ merge = [];
if (this.type === x.TEMPLATE) {
- for (_i = 0, _len = tokens.length; _i < _len; _i++) {
- token = tokens[_i];
- if (token.name === "extends") {
- path = this.node.options.root.replace(/['"]/g, "") + "/" + token.args[0].replace(/['"]/g, "");
- errMsg = "Only one string literal accepted of Extends.";
- if (token.args.length > 1) {
- throw new Error(errMsg);
+ for (i = 0, _len = tokens.length; i < _len; i++) {
+ token = tokens[i];
+ if (typeof token !== "undefined") {
+ if (token.name === "extends") {
+ path = this.node.options.root.replace(/['"]/g, "") + "/" + token.args[0].replace(/['"]/g, "");
+ errMsg = "Only one string literal accepted of Extends.";
+ if (token.args.length > 1) {
+ throw new Error(errMsg);
+ }
+ parent = this.node.fileRenderer(path, this.node.options);
+ } else if (token.name === "block") {
+ blockName = token.args;
+ if (!helpers.isValidBlockName(blockName)) {
+ throw new Error("Invalid syntax.");
+ }
+ if (this.type !== x.TEMPLATE) {
+ throw new Error("Multiple block tag detected");
+ }
+ if (parent) {
+ for (_i = 0, _len2 = parent.length; _i < _len2; _i++) {
+ p = parent[_i];
+ if (typeof p !== "string") {
+ parentBlock = p.args;
+ if (parentBlock.toString() === blockName.toString()) {
+ p[blockName] = token[parentBlock];
+ tokens.splice(i, 1);
+ }
+ }
+ }
+ }
}
- token.template = this.node.fileRenderer(path, this.node.options);
- pr.ins("no extends");
- } else if (token.name === "block") {
- blockName = token.args[0];
- if (!helpers.isValidBlockName(blockName || token.args.length > 1)) {
- throw new Error("Invalid syntax.");
- }
- if (this.type !== x.TEMPLATE) {
- throw new Error("Multiple block tag detected");
- }
- this.node.blocks[blockName] = this.out(token.tokens);
- pr.ins("no blocks");
}
}
- if (tokens[0].name === "extends") {
- parent = tokens[0].template;
- this.node.blocks = helpers.merge(parent.blocks, this.node.blocks);
- }
- return this.out(tokens);
+ merge = merge.concat(tokens, parent);
+ return this.out(merge);
}
};
Compiler.prototype.out = function(tok) {
- var t, varOut, _i, _len;
- for (_i = 0, _len = tok.length; _i < _len; _i++) {
- t = tok[_i];
- if (typeof t === "string") {
+ var blockName, i, t, varOut, _len;
+ for (i = 0, _len = tok.length; i < _len; i++) {
+ t = tok[i];
+ if (typeof t === "string" && t.name !== "block") {
this.html.push(t);
}
if (t.type === x.VAR_TOKEN) {
@@ -63,6 +74,10 @@ Compiler = (function() {
throw new Error("Nested blocks isn't supported yet.");
}
}
+ if (t.name === "block") {
+ blockName = t.args;
+ this.html.push(t[blockName.toString()]);
+ }
}
return this.html.join("");
};
View
7 compiled/twig.js
@@ -13,16 +13,19 @@ exports.version = "v0.0.2";
exports.Parser = Parser;
exports.Compiler = Compiler;
fileRenderer = function(path, options, fn) {
- var str;
+ var parser, str, tokens;
if (typeof options === "function") {
fn = options;
options = {};
}
options.filename = path;
str = fs.readFileSync(path, "utf8");
+ tokens = {};
if (str) {
- return parse(str, options);
+ parser = new Parser(str, tags);
+ tokens = parser.parseTokens();
}
+ return tokens;
};
parse = function(str, options) {
var compiled, compiler, parser, twigTemplate;
View
58 lib/compiler.coffee
@@ -4,7 +4,8 @@ Author: Fadrizul H. <fadrizul[at]gmail.com>
###
# Module dependencies
-x = require "./regexes"
+x = require "./regexes"
+helpers = require "./helpers"
pr = require "../dev/eyes" # Debugging purposes
class Compiler
@@ -14,39 +15,42 @@ class Compiler
@type = node.type
compile: ->
- tokens = @node.tokens
- blocks = @node.blocks
+ tokens = @node.tokens
+ blocks = @node.blocks
+ parent = {}
+ merge = []
if @type is x.TEMPLATE
- for token in tokens
+ for token, i in tokens
+ if typeof token isnt "undefined"
+ if token.name is "extends"
+ path = @node.options.root.replace(/['"]/g, "") + "/" + token.args[0].replace(/['"]/g, "")
- if token.name is "extends"
- path = @node.options.root.replace(/['"]/g, "") + "/" + token.args[0].replace(/['"]/g, "")
+ errMsg = "Only one string literal accepted of Extends."
+ throw new Error errMsg if token.args.length > 1
- errMsg = "Only one string literal accepted of Extends."
- throw new Error errMsg if token.args.length > 1
+ parent = @node.fileRenderer(path, @node.options)
+ else if token.name is "block"
+ blockName = token.args
- token.template = @node.fileRenderer(path, @node.options)
- pr.ins "no extends"
- else if token.name is "block"
- blockName = token.args[0]
+ throw new Error "Invalid syntax." if not helpers.isValidBlockName blockName
+ throw new Error "Multiple block tag detected" if @type isnt x.TEMPLATE
- throw new Error "Invalid syntax." if not helpers.isValidBlockName blockName or token.args.length > 1
- throw new Error "Multiple block tag detected" if @type isnt x.TEMPLATE
+ if parent
+ for p in parent
+ if typeof p isnt "string"
+ parentBlock = p.args
+ if parentBlock.toString() is blockName.toString()
+ p[blockName] = token[parentBlock]
+ tokens.splice(i, 1)
- @node.blocks[blockName] = @out(token.tokens)
- pr.ins "no blocks"
-
-
- if tokens[0].name is "extends"
- parent = tokens[0].template
- @node.blocks = helpers.merge(parent.blocks, @node.blocks)
-
- @out(tokens)
+ merge = merge.concat(tokens, parent)
+
+ @out merge
out: (tok) ->
- for t in tok
- if typeof t is "string"
+ for t, i in tok
+ if typeof t is "string" and t.name isnt "block"
@html.push t
if t.type is x.VAR_TOKEN
@@ -58,6 +62,10 @@ class Compiler
throw new Error "Extends tag must be the first tag in the template" if @type isnt x.TEMPLATE
throw new Error "Nested blocks isn't supported yet." if @type isnt x.TEMPLATE
+ if t.name is "block"
+ blockName = t.args
+ @html.push t[blockName.toString()]
+
@html.join ""
# Expose
View
9 lib/twig.coffee
@@ -23,12 +23,15 @@ fileRenderer = (path, options, fn) ->
if typeof options is "function"
fn = options
options = {}
-
options.filename = path
- str = fs.readFileSync(path, "utf8")
+ str = fs.readFileSync(path, "utf8")
+ tokens = {}
if str
- parse(str, options)
+ parser = new Parser(str, tags)
+ tokens = parser.parseTokens()
+
+ return tokens
parse = (str, options) ->
# Collection of named properties for rendering
Please sign in to comment.
Something went wrong with that request. Please try again.