Chinstrap
Note that Chinstrap has no dependencies in the compiler.
module.exports = ->
Basic Setup
Value: open
The opener is the string pattern that marks the beginning of a code block. Default is {{
Value: close
The closer is the string pattern taht marks the end of a code block. Default is }}.
open: "{{" close: "}}"
Methods: setOpen and setClose
Two methods to override the opne and close values.
: :
Main Execution Code
Method: merge
The main method to call when using Chinstrap.
Receives three arguments:
: returnSource = typeof returnSource isnt 'undefined' and returnSource template = @rendertemplate fn = "obj"template if returnSource then str else fndata
Method: render (String str)
Workhorse method; converts the template into a function.
: str = @stripWhiteSpacestr str = @replaceOpenCharsstr str = strreplace/'/g"$1\r" str = @replaceQmarkWithIfOpenstr str = strreplace/\t\s?\@\=\}\}/g"\titerator=$1;\n}}" str = strreplace/\t\s?WHILE\}\}/g"\twhile ($1) {\n}}" str = strreplace/\t\s?\/WHILE\}\}/g"\t}\n}}" str = strreplace/\t\s?FOR\}\}/g"\tfor ($1) {\n}}" str = strreplace/\t\s?%\}\}/g"', this.merge($1), '" str = strreplace/\t\s?\/FOR\}\}/g"\t}\n}}" str = strreplace/\t\s?\}\}/g"\tif (value($2)) {\n}}" str = strreplace/\t\s?\/\}\}/g"\t}\n}}" str = strreplace/\t\s?\}\}/g"\t} else if (value($2)) {\n}}" str = strreplace/\t\s?\}\}/g"\t} else {\n}}" str = strreplace/\@\@/g"iterator" str = strreplace/\@/g"iterator." str = strsplit"\t"join"');\n" str = strsplit@closejoin"p.push('" str = strsplit"\r"join"\\'" str = "\nvar p=[],iterator = {},print=function(){p.push.apply(p,arguments);},value = function(val){if (typeof val == 'function') {return val.apply(iterator);} else {return val;}};\nwith(config){\np.push('" + str + "');\n}\nreturn p.join('');\n"
String Manipulation Functions
Method: stripWhiteSpace
The conceit of the Resig system is to remove lots of white space characters, and then use those characters as drop-ins for action items.
So step one is to remove tabs and returns, and replace them with a blank space.
: strreplace/[\r\t\n]/g" "
Method: replaceOpenChars
Chinstrap executes snippets of code that live between an open and close character. The default opener is "{{".
Now replace the open character (default "{{") with a tab character ("\t"). We'll use this to put the function back together later.
: strsplit@openjoin"\t"
Replace ? with if statement
You can write an if statement in two alternate styles.
: strreplace/\t\s?=\}\}/g"',value($1),'"
: : : : module.exports =