Created
March 16, 2015 03:24
-
-
Save val314159/4f70ec4184a3ae9af4fa to your computer and use it in GitHub Desktop.
Handlers - the macro processing package powered by handlebarsjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
# Handlers - the macro processing package powered by handlebarsjs | |
# Usage: | |
# sh handlers.sh | node | |
DIR=lang/py | |
echo // requires | |
cat <<EOF | |
if (!global) global = window; | |
str = JSON.stringify; | |
LOG = console.log; | |
len = function(x){if(len)return len.length}; | |
type = function(x){ | |
var _typ = function(x){return({}).toString.call(x)}; | |
return _typ(x).match( /\s([\w]+)/ )[1].toLowerCase()}; | |
// jshint said this was one of the fastest | |
extend2 = function(dst, src) { | |
var k; | |
for(k in src || {}) if(src.hasOwnProperty(k)) dst[k] = src[k]; | |
return dst}; | |
forArray = function(arr,cb,start,len){ | |
var n = start||0; | |
len = len||arr.length; | |
for(; n<len; n++) | |
cb(arr[n],n,n); | |
return n}; | |
forObject = function(arr,cb,start){ | |
var k, n = start||0; | |
for(k in arr) | |
cb(arr[k],k,n++); | |
return n}; | |
mkProtoClass = function(proto) { | |
proto.constructor.prototype=proto; | |
return proto.constructor}; | |
extendObj = function(self,other){ | |
var k; | |
if(self===undefined || self===null) self = {}; | |
for(k in other) | |
self[k] = other[k]; | |
return self}; | |
var Ctor = mkProtoClass({ // example | |
some_method: function(){}, | |
prototype: undefined, | |
constructor: function(cfg){ | |
return extend2(Object.create(Ctor.prototype),cfg) }}); | |
// handlebars stuff | |
Handlebars = require("handlebars"); | |
Helpers=global.Helpers||{'$type':'Helpers'}; | |
optionsPop = function(args) { | |
var length = args.length-1; | |
var ret = args[length]; | |
if(ret.fn) { | |
args[ args.length = length ] = undefined; | |
return ret}} | |
ProcessStuff = function(document,acc){ | |
acc=acc||{}; | |
var dossier = { document: document }; | |
var templates = Handlebars.templates; | |
forObject(templates, function(v,k){ | |
var ret = v(dossier,{helpers:Helpers,/*data: what is this?!?!*/}); | |
console.log("DID", k,ret); | |
acc[k]=ret; | |
}); | |
return acc; | |
}; | |
EOF | |
echo // partials | |
handlebars $DIR/part -p -e hbs -r $DIR | |
echo // templates | |
handlebars $DIR/tmpl -e hbs -r $DIR | |
echo //\#### | |
cat <<EOF | |
function main() { | |
console.log("Helpers",Helpers); | |
var q = ProcessStuff({ blah: [ 12, 34, 56 ]}); | |
console.log('############qqqq') | |
LOG(q); | |
} | |
main(); | |
EOF | |
echo //\#### EOF |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment