Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes and additions #33

Merged
merged 9 commits into from
Jul 18, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/environment.co
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ class Environment
return eval t

({@utils ? require("./utils"), @filters ? require("./filters"), @parser ? new Parser!, @pre_compile_func ? "", @require_exp ? 'require'} ? {}) ->
@outputUndefinedVariable = false
@outputUndefinedVariableWithBrackets = false

getTemplateFromString: function (str)
try
Expand All @@ -28,7 +30,7 @@ class Environment
str = @pre_compile_func str if @pre_compile_func

ast = @parser.parse str, compilation_ctx
opts = {__indent__: 1, @utils, @filters}
opts = {__indent__: 1, @utils, @filters, @outputUndefinedVariable, @outputUndefinedVariableWithBrackets}

compilation_ctx = filters_used: {}
compilation_ctx <<< {@filters, @utils}
Expand Down
3 changes: 1 addition & 2 deletions src/expression.pegco
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

->
compilation_ctx = arguments[2] ? {}
compilation_ctx.filters ?= {}
Expand Down Expand Up @@ -320,7 +319,7 @@ tag_for
/ key:identifier COMMA value:identifier IN exp:expression ->
return key: key, value: value, condition: exp

tag_let
tag_set
= variable_name:identifier ASSIGN expression:expression ->
return variable_name: variable_name, expression: expression

Expand Down
25 changes: 18 additions & 7 deletions src/filters.co
Original file line number Diff line number Diff line change
Expand Up @@ -186,13 +186,13 @@ exports import do
if value < 1024
return "#{value}"
if value < 1024 * 1024
val = val / 1024
val = value / 1024
unit = "Kb"
else if value < 1024 * 1024 * 1024
val = val / (1024 * 1024)
val = value / (1024 * 1024)
unit = "Mb"
else
val = val / (1024 * 1024 * 1024)
val = value / (1024 * 1024 * 1024)
unit = "Gb"

strval = "#{Math.round val}"
Expand All @@ -203,7 +203,7 @@ exports import do

$float: function (value)
res = parseFloat value
return 0.0 if res is NaN
return 0.0 if isNaN res
return res

$forceescape: function (value)
Expand Down Expand Up @@ -441,10 +441,21 @@ exports import do
obj = obj[a]
return obj

$reverse: function (arr)
new_arr = arr.splice 0
$reverse: function (arr, keep_array)
keep_array = keep_array or false
new_arr = arr
if \number is typeof arr
new_arr = new_arr.toString()
new_arr = new_arr.split("")
new_arr = new_arr.splice(0)
else if \string is typeof arr
new_arr = new_arr.split("")
new_arr = new_arr.splice(0)
else if arr instanceof Array
new_arr = new_arr.splice(0)
else throw new Error("Unsupported type \"" + typeof arr + "\" for reverse")
new_arr.reverse()
return new_arr
return (if (keep_array) then new_arr else new_arr.join(""))

$round: function (value, precision ? 0, method ? 'common')
if method is \common
Expand Down
26 changes: 24 additions & 2 deletions src/helpers.co
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* @param filter: a callback (str) -> that transforms the source before
* giving it to JinJS.
*/
function registerExtension (ext, filter)
function _registerExtension (ext, filter)
filter ?= (str) -> str

require.extensions[ext] = (module, filename) ->
Expand All @@ -20,5 +20,27 @@ function registerExtension (ext, filter)
# registerExtension \.pwi, (str) -> pwilang.parse str
# registerExtension \.pwx, (str) -> pwilang.parse str

exports import { registerExtension }
/**
* Register flag to output undefined variables
* @param output: flag to output undefined variable name
* @param withBrackets: flag to output undefined variable name with brackets
*/
function _outputUndefinedVariable (output, withBrackets)
output ?= true
withBrackets ?= false

defaultEnvironment.outputUndefinedVariable = output
defaultEnvironment.outputUndefinedVariableWithBrackets = withBrackets

/**
* Options for JinJS
* @param opts: object where the properties are set
*/
function options (opts)
opts ?= {}

_registerExtension(opts.ext, opts.filter)
_outputUndefinedVariable(opts.outputUndefinedVariable, opts.outputUndefinedVariableWithBrackets)

exports import { options }

4 changes: 2 additions & 2 deletions src/main.co
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{ defaultEnvironment, Environment } = require \./environment
{ registerExtension } = require \./helpers
{ options } = require \./helpers
{ compile } = require \./express

exports <<< { defaultEnvironment, registerExtension, Environment, compile }
exports <<< { defaultEnvironment, options, Environment, compile }

36 changes: 27 additions & 9 deletions src/nodes.co
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@

{ parse:make_expression } = require \./expression

make_parse_rule = (rule_name) ->
return (contents, ctx) -> make_expression contents, rule_name, ctx

parse_for = make_parse_rule \tag_for
parse_let = make_parse_rule \tag_let
parse_set = make_parse_rule \tag_set
parse_macro = make_parse_rule \tag_macro
parse_extends = make_expression
parse_block = make_parse_rule \tag_block
Expand Down Expand Up @@ -108,7 +107,12 @@ class NodePrint extends Node
(specs) -> super ...

compile: function (opts, ctx)
return "_res += ((_ref = #{make_expression @contents, ctx}) !== undefined && _ref !== null ? _ref : '').toString();"
output = ''
if (opts.outputUndefinedVariableWithBrackets)
output = "{{#{@contents}}}"
else if (opts.outputUndefinedVariable)
output = @contents
return "_res += ((_ref = #{make_expression @contents, ctx}) !== undefined && _ref !== null ? _ref : '#{output}').toString();"

class NodeTag extends Node
@tag = \__tag__
Expand Down Expand Up @@ -242,13 +246,13 @@ class NodeAbspath extends NodeTag
/**
*
*/
class NodeLet extends NodeTag
@tag = \let
class NodeSet extends NodeTag
@tag = \set

(specs) -> super ...

compile: function (opts, ctx)
{ variable_name, expression } = parse_let @contents, ctx
{ variable_name, expression } = parse_set @contents, ctx

ctx[variable_name] = true # The variable name is now accessible to the rest of the template.
return res = "var #{variable_name} = ($$.#{variable_name} = #{expression});"
Expand Down Expand Up @@ -452,6 +456,19 @@ class NodeIf extends NodeTagContainer
}"""
return res

/**
* Raw Node
*/
class NodeRaw extends NodeTagContainer
@tag = \raw
@until = \endraw

(specs) ->
super ...

compile : function (opts, ctx)
console.dir @
return "_res += 'ffs';";

class NodeElseFor extends NodeTagContainer
@tag = \else
Expand Down Expand Up @@ -542,13 +559,13 @@ class NodeFor extends NodeTagContainer
"""

exports <<< {
NodeIf, NodeDo, NodeLet, NodeFor, NodeMacro, NodeList, NodeBasic,
NodeIf, NodeDo, NodeSet, NodeFor, NodeMacro, NodeList, NodeBasic,
NodePrint, NodeComment, NodeExtends, NodeInclude, NodeImport, NodeFromImport,
NodeContinue, NodeCall
NodeContinue, NodeCall, NodeRaw
default_nodes: do
\if : NodeIf
\do : NodeDo
\let : NodeLet
\set : NodeSet
\for : NodeFor
\macro : NodeMacro
\extends : NodeExtends
Expand All @@ -560,5 +577,6 @@ exports <<< {
\continue : NodeContinue
\break : NodeBreak
\call : NodeCall
\raw : NodeRaw
}