Browse files

Remove unusable file, fix IF logic end tag

  • Loading branch information...
1 parent 6d3ee46 commit c29ece6bd2c02932b79ba3d4c85f0a931faf30be @fadrizul committed Sep 22, 2011
Showing with 6 additions and 281 deletions.
  1. +3 −0 compiled/nodes/conditions.js
  2. +3 −3 compiled/parser.js
  3. +0 −109 compiled/tags.js
  4. +0 −169 lib/tags.coffee
View
3 compiled/nodes/conditions.js
@@ -6,6 +6,9 @@ MIT Licensed
var Conditions;
Conditions = (function() {
function Conditions() {}
+ Conditions.prototype.ends = function() {
+ return true;
+ };
return Conditions;
})();
module.exports = Conditions;
View
6 compiled/parser.js
@@ -40,11 +40,11 @@ Parser = (function() {
if (x.Spaces.test(token)) {
token = token.replace(RegExp(" +"), " ").replace(/\n+/, "\n");
} else if (x.twigVar.test(token)) {
- stack[0].push(this.parseVar(token));
+ stack[0].push(this.variable(token));
} else if (x.twigLogic.test(token)) {
parts = token.replace(x.LogDelimiter, "").split(" ");
tagname = parts.shift();
- if (tagname === "endblock" || tagname === "endfor") {
+ if (tagname === "endblock" || tagname === "endfor" || tagname === "endif") {
index--;
stack.pop;
continue;
@@ -74,7 +74,7 @@ Parser = (function() {
}
return stack[index];
};
- Parser.prototype.parseVar = function(token) {
+ Parser.prototype.variable = function(token) {
var filters, parts, varname;
parts = token.replace(x.VarDelimiter, "").split("|");
varname = parts.shift();
View
109 compiled/tags.js
@@ -1,109 +0,0 @@
-/*
-TwigJS
-Copyright(c) 2011 Fadrizul Hasani <fadrizul@twigjs.org>
-MIT Licensed
-*/
-var Compiler, check, escape, helpers;
-Compiler = require("./compiler");
-helpers = require("./helpers");
-check = helpers.check;
-escape = helpers.escape;
-exports["extends"] = {};
-exports.block = {
- ends: true
-};
-exports.slot = function(twig) {
- var ret, slot;
- slot = this.args[0];
- twig = twig || "";
- ret = "(function () { if (" + check(slot, "__context.slots") + ") { var __widget, __slot = " + escape(slot, "__context.slots") + ".content || []; for (var __i=0, __j = (+__slot.length) || 0; __i < __j; ++__i) { __widget = __slot[__i]; if (__widget === undefined || __widget === null || __widget === false) continue; if (typeof __widget === \"string\") __output.push(__widget) else if (__widget.tagname && __widgets && typeof __widgets[__widget.tagname] === \"function\") __output.push(__widgets[__widget.tagname].call(__widget, __context, __parents)); } } " + twig;
- return ret;
-};
-exports.include = function(twig) {
- var template;
- template = this.args[0];
- twig = twig || "";
- if (!helpers.isLiteral(template) && !helpers.isValidName(template)) {
- throw new Error("Invalid arguments passed to 'include' tag.");
- }
- return ["(function () {", " if (" + check(template) + ") {", " var __template = " + escape(template) + ";", " }", " else if (" + check(template, "__context") + ") {", " var __template = " + escape(template, "__context") + ";", " }", " if (typeof __template === \"string\") {", " __output.push(__this.fromFile(__template).render(__context, __parents));", " }", " else if (typeof __template === \"object\" && __template.render) {", " __output.push(__template.render(__context, __parents));", " }", "})();"].join("\n" + twig);
-};
-exports["if"] = function(indent) {
- var compile, negation, operand1, operand2, operator, out;
- compile = new Compiler(indent);
- operand1 = this.args[0];
- operator = this.args[1];
- operand2 = this.args[2];
- negation = false;
- indent = indent || "";
- if (operand1[0] === "!") {
- negation = true;
- operand1 = operand1.substr(1);
- }
- if (negation && operator) {
- throw new Error("Invalid syntax for 'if' tag");
- }
- if (!helpers.isLiteral(operand1) && !helpers.isValidName(operand1)) {
- throw new Error("Invalid arguments (" + operand1 + ") passed to 'if' tag");
- }
- if (operator && ["==", "<", ">", "!=", "<=", ">=", "===", "!==", "in"].indexOf(operator) === -1) {
- throw new Error("Invalid operator (" + operator + ") passed to 'if' tag");
- }
- if (operator && typeof operand2 === "undefined") {
- throw new Error("Missing argument in 'if' tag");
- }
- if (operator && !helpers.isLiteral(operand2) && !helpers.isValidName(operand2)) {
- throw new Error("Invalid arguments (" + operand2 + ") passed to 'if' tag");
- }
- out = ["(function () {"];
- out.push(" var __op1;");
- out.push(" if (" + check(operand1) + ") {");
- out.push(" __op1 = " + escape(operand1) + ";");
- out.push(" }");
- out.push(" else if (" + check(operand1, "__context") + ") {");
- out.push(" __op1 = " + escape(operand1, "__context") + ";");
- out.push(" }");
- if (typeof operand2 === "undefined") {
- out.push(" if (" + (negation ? "!" : "!!") + "__op1) {");
- out.push(commpiler.compile());
- out.push(" }");
- } else {
- out.push(" var __op2;");
- out.push(" if (" + check(operand2) + ") {");
- out.push(" __op2 = " + escape(operand2) + ";");
- out.push(" }");
- out.push(" else if (" + check(operand2, "__context") + ") {");
- out.push(" __op2 = " + escape(operand2, "__context") + ";");
- out.push(" }");
- if (operator === "in") {
- out.push(" if ((Array.isArray(__op2) && __op2.indexOf(__op1) > -1) ||");
- out.push(" (typeof __op2 === \"string\" && __op2.indexOf(__op1) > -1) ||");
- out.push(" (!Array.isArray(__op2) && typeof __op2 === \"object\" && __op1 in __op2)) {");
- } else {
- out.push(" if (__op1 " + escape(operator) + " __op2) {");
- }
- out.push(compiler.compile(this, indent + " "));
- out.push(" }");
- }
- out.push("})();");
- return out.join("\n" + indent);
-};
-exports["if"].ends = true;
-exports["for"] = function(indent) {
- var operand1, operand2, operator;
- operand1 = this.args[0];
- operator = this.args[1];
- operand2 = this.args[2];
- indent = indent || "";
- if (typeof operator !== "undefined" && operator !== "in") {
- throw new Error("Invalid syntax in 'for' tag");
- }
- if (!helpers.isValidShortName(operand1)) {
- throw new Error("Invalid arguments (" + operand1 + ") passed to 'for' tag");
- }
- if (!helpers.isValidName(operand2)) {
- throw new Error("Invalid arguments (" + operand2 + ") passed to 'for' tag");
- }
- return ["(function () {", " if (" + check(operand2) + ") {", " var __forloopIter = " + escape(operand2) + ";", " }", " else if (" + check(operand2, "__context") + ") {", " var __forloopIter = " + escape(operand2, "__context") + ";", " } else {", " return;", " }", " var " + escape(operand1) + ";", " var forloop = {};", " if (Array.isArray(__forloopIter)) {", " var __forloopIndex, __forloopLength;", " for (var __forloopIndex=0, __forloopLength=__forloopIter.length; __forloopIndex<__forloopLength; ++__forloopIndex) {", " forloop.index = __forloopIndex;", " " + escape(operand1) + " = __forloopIter[__forloopIndex];", compile.call(this, indent + " "), " }", " }", " else if (typeof __forloopIter === \"object\") {", " var __forloopIndex;", " for (__forloopIndex in __forloopIter) {", " forloop.index = __forloopIndex;", " " + escape(operand1) + " = __forloopIter[__forloopIndex];", compile.call(this, indent + " "), " }", " }", "})();"].join("\n" + indent);
-};
-exports["for"].ends = true;
View
169 lib/tags.coffee
@@ -1,169 +0,0 @@
-###
-TwigJS
-Copyright(c) 2011 Fadrizul Hasani <fadrizul@twigjs.org>
-MIT Licensed
-###
-
-# Load dependencies
-Compiler = require "./compiler"
-helpers = require "./helpers"
-
-# Declare helpers
-check = helpers.check
-escape = helpers.escape
-
-# Expose
-exports.extends = {}
-exports.block = ends: true
-
-# Slots
-exports.slot = (twig) ->
- slot = @args[0]
- twig = twig or ""
-
- ret =
- "(function () {
- if (" + check(slot, "__context.slots") + ") {
- var __widget, __slot = " + escape(slot, "__context.slots") + ".content || [];
- for (var __i=0, __j = (+__slot.length) || 0; __i < __j; ++__i) {
- __widget = __slot[__i];
- if (__widget === undefined || __widget === null || __widget === false)
- continue;
- if (typeof __widget === \"string\")
- __output.push(__widget)
- else if (__widget.tagname && __widgets && typeof __widgets[__widget.tagname] === \"function\")
- __output.push(__widgets[__widget.tagname].call(__widget, __context, __parents));
- }
- }
- " + twig
- ret
-
-# Include
-exports.include = (twig) ->
- template = @args[0]
- twig = twig or ""
-
- throw new Error "Invalid arguments passed to 'include' tag." if not helpers.isLiteral(template) and not helpers.isValidName(template)
-
- [ "(function () {"
- , " if (" + check(template) + ") {"
- , " var __template = " + escape(template) + ";"
- , " }"
- , " else if (" + check(template, "__context") + ") {"
- , " var __template = " + escape(template, "__context"
- ) + ";"
- , " }"
- , " if (typeof __template === \"string\") {"
- , " __output.push(__this.fromFile(__template).render(__context, __parents));"
- , " }"
- , " else if (typeof __template === \"object\" && __template.render) {"
- , " __output.push(__template.render(__context, __parents));"
- , " }"
- , "})();" ].join "\n" + twig
-
-# IF statement
-exports["if"] = (indent) ->
- compile = new Compiler(indent)
-
- operand1 = @args[0]
- operator = @args[1]
- operand2 = @args[2]
-
- negation = false
- indent = indent or ""
-
- if operand1[0] == "!"
- negation = true
- operand1 = operand1.substr(1)
-
- throw new Error("Invalid syntax for 'if' tag") if negation and operator
- throw new Error("Invalid arguments (" + operand1 + ") passed to 'if' tag") if not helpers.isLiteral(operand1) and not helpers.isValidName(operand1)
- throw new Error("Invalid operator (" + operator + ") passed to 'if' tag") if operator and [ "==", "<", ">", "!=", "<=", ">=", "===", "!==", "in" ].indexOf(operator) == -1
- throw new Error("Missing argument in 'if' tag") if operator and typeof operand2 == "undefined"
- throw new Error("Invalid arguments (" + operand2 + ") passed to 'if' tag") if operator and not helpers.isLiteral(operand2) and not helpers.isValidName(operand2)
-
- out = [ "(function () {" ]
-
- out.push " var __op1;"
- out.push " if (" + check(operand1) + ") {"
- out.push " __op1 = " + escape(operand1) + ";"
- out.push " }"
- out.push " else if (" + check(operand1, "__context") + ") {"
- out.push " __op1 = " + escape(operand1, "__context") + ";"
- out.push " }"
-
- if typeof operand2 == "undefined"
-
- out.push " if (" + (if negation then "!" else "!!") + "__op1) {"
- out.push commpiler.compile()
- out.push " }"
-
- else
-
- out.push " var __op2;"
- out.push " if (" + check(operand2) + ") {"
- out.push " __op2 = " + escape(operand2) + ";"
- out.push " }"
- out.push " else if (" + check(operand2, "__context") + ") {"
- out.push " __op2 = " + escape(operand2, "__context") + ";"
- out.push " }"
-
- if operator == "in"
-
- out.push " if ((Array.isArray(__op2) && __op2.indexOf(__op1) > -1) ||"
- out.push " (typeof __op2 === \"string\" && __op2.indexOf(__op1) > -1) ||"
- out.push " (!Array.isArray(__op2) && typeof __op2 === \"object\" && __op1 in __op2)) {"
-
- else
- out.push " if (__op1 " + escape(operator) + " __op2) {"
-
- out.push compiler.compile(this, indent + " ")
- out.push " }"
-
- out.push "})();"
- out.join "\n" + indent
-
-exports["if"].ends = true
-
-# FOR loop
-exports["for"] = (indent) ->
- operand1 = @args[0]
- operator = @args[1]
- operand2 = @args[2]
-
- indent = indent or ""
-
- throw new Error("Invalid syntax in 'for' tag") if typeof operator != "undefined" and operator != "in"
- throw new Error("Invalid arguments (" + operand1 + ") passed to 'for' tag") unless helpers.isValidShortName(operand1)
- throw new Error("Invalid arguments (" + operand2 + ") passed to 'for' tag") unless helpers.isValidName(operand2)
-
- [ "(function () {"
- , " if (" + check(operand2) + ") {"
- , " var __forloopIter = " + escape(operand2) + ";"
- , " }"
- , " else if (" + check(operand2, "__context") + ") {"
- , " var __forloopIter = " + escape(operand2, "__context") + ";"
- , " } else {"
- , " return;"
- , " }"
- , " var " + escape(operand1) + ";"
- , " var forloop = {};"
- , " if (Array.isArray(__forloopIter)) {"
- , " var __forloopIndex, __forloopLength;"
- , " for (var __forloopIndex=0, __forloopLength=__forloopIter.length; __forloopIndex<__forloopLength; ++__forloopIndex) {"
- , " forloop.index = __forloopIndex;"
- , " " + escape(operand1) + " = __forloopIter[__forloopIndex];"
- , compile.call(this, indent + " ")
- , " }"
- , " }"
- , " else if (typeof __forloopIter === \"object\") {"
- , " var __forloopIndex;"
- , " for (__forloopIndex in __forloopIter) {"
- , " forloop.index = __forloopIndex;"
- , " " + escape(operand1) + " = __forloopIter[__forloopIndex];"
- , compile.call(this, indent + " ")
- , " }"
- , " }"
- , "})();" ].join "\n" + indent
-
-exports["for"].ends = true

0 comments on commit c29ece6

Please sign in to comment.