initial commit
This commit is contained in:
+87
@@ -0,0 +1,87 @@
|
||||
#!/usr/bin/env node
|
||||
/*
|
||||
* JavaScript Templates Compiler
|
||||
* https://github.com/blueimp/JavaScript-Templates
|
||||
*
|
||||
* Copyright 2011, Sebastian Tschan
|
||||
* https://blueimp.net
|
||||
*
|
||||
* Licensed under the MIT license:
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
;(function () {
|
||||
'use strict'
|
||||
var path = require('path')
|
||||
var tmpl = require(path.join(__dirname, 'tmpl.js'))
|
||||
var fs = require('fs')
|
||||
// Retrieve the content of the minimal runtime:
|
||||
var runtime = fs.readFileSync(path.join(__dirname, 'runtime.js'), 'utf8')
|
||||
// A regular expression to parse templates from script tags in a HTML page:
|
||||
var regexp = /<script( id="([\w-]+)")? type="text\/x-tmpl"( id="([\w-]+)")?>([\s\S]+?)<\/script>/gi
|
||||
// A regular expression to match the helper function names:
|
||||
var helperRegexp = new RegExp(
|
||||
tmpl.helper.match(/\w+(?=\s*=\s*function\s*\()/g).join('\\s*\\(|') +
|
||||
'\\s*\\('
|
||||
)
|
||||
// A list to store the function bodies:
|
||||
var list = []
|
||||
var code
|
||||
// Extend the Templating engine with a print method for the generated functions:
|
||||
tmpl.print = function (str) {
|
||||
// Only add helper functions if they are used inside of the template:
|
||||
var helper = helperRegexp.test(str) ? tmpl.helper : ''
|
||||
var body = str.replace(tmpl.regexp, tmpl.func)
|
||||
if (helper || /_e\s*\(/.test(body)) {
|
||||
helper = '_e=tmpl.encode' + helper + ','
|
||||
}
|
||||
return (
|
||||
'function(' +
|
||||
tmpl.arg +
|
||||
',tmpl){' +
|
||||
('var ' + helper + "_s='" + body + "';return _s;")
|
||||
.split("_s+='';")
|
||||
.join('') +
|
||||
'}'
|
||||
)
|
||||
}
|
||||
// Loop through the command line arguments:
|
||||
process.argv.forEach(function (file, index) {
|
||||
var listLength = list.length
|
||||
var stats
|
||||
var content
|
||||
var result
|
||||
var id
|
||||
// Skip the first two arguments, which are "node" and the script:
|
||||
if (index > 1) {
|
||||
stats = fs.statSync(file)
|
||||
if (!stats.isFile()) {
|
||||
console.error(file + ' is not a file.')
|
||||
return
|
||||
}
|
||||
content = fs.readFileSync(file, 'utf8')
|
||||
while (true) {
|
||||
// Find templates in script tags:
|
||||
result = regexp.exec(content)
|
||||
if (!result) {
|
||||
break
|
||||
}
|
||||
id = result[2] || result[4]
|
||||
list.push("'" + id + "':" + tmpl.print(result[5]))
|
||||
}
|
||||
if (listLength === list.length) {
|
||||
// No template script tags found, use the complete content:
|
||||
id = path.basename(file, path.extname(file))
|
||||
list.push("'" + id + "':" + tmpl.print(content))
|
||||
}
|
||||
}
|
||||
})
|
||||
if (!list.length) {
|
||||
console.error('Missing input file.')
|
||||
return
|
||||
}
|
||||
// Combine the generated functions as cache of the minimal runtime:
|
||||
code = runtime.replace('{}', '{' + list.join(',') + '}')
|
||||
// Print the resulting code to the console output:
|
||||
console.log(code)
|
||||
})()
|
||||
+47
@@ -0,0 +1,47 @@
|
||||
/*
|
||||
* JavaScript Templates Runtime
|
||||
* https://github.com/blueimp/JavaScript-Templates
|
||||
*
|
||||
* Copyright 2011, Sebastian Tschan
|
||||
* https://blueimp.net
|
||||
*
|
||||
* Licensed under the MIT license:
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
/* global define */
|
||||
|
||||
;(function ($) {
|
||||
'use strict'
|
||||
var tmpl = function (id, data) {
|
||||
var f = tmpl.cache[id]
|
||||
return data
|
||||
? f(data, tmpl)
|
||||
: function (data) {
|
||||
return f(data, tmpl)
|
||||
}
|
||||
}
|
||||
tmpl.cache = {}
|
||||
tmpl.encReg = /[<>&"'\x00]/g // eslint-disable-line no-control-regex
|
||||
tmpl.encMap = {
|
||||
'<': '<',
|
||||
'>': '>',
|
||||
'&': '&',
|
||||
'"': '"',
|
||||
"'": '''
|
||||
}
|
||||
tmpl.encode = function (s) {
|
||||
return (s == null ? '' : '' + s).replace(tmpl.encReg, function (c) {
|
||||
return tmpl.encMap[c] || ''
|
||||
})
|
||||
}
|
||||
if (typeof define === 'function' && define.amd) {
|
||||
define(function () {
|
||||
return tmpl
|
||||
})
|
||||
} else if (typeof module === 'object' && module.exports) {
|
||||
module.exports = tmpl
|
||||
} else {
|
||||
$.tmpl = tmpl
|
||||
}
|
||||
})(this)
|
||||
+95
@@ -0,0 +1,95 @@
|
||||
/*
|
||||
* JavaScript Templates
|
||||
* https://github.com/blueimp/JavaScript-Templates
|
||||
*
|
||||
* Copyright 2011, Sebastian Tschan
|
||||
* https://blueimp.net
|
||||
*
|
||||
* Licensed under the MIT license:
|
||||
* https://opensource.org/licenses/MIT
|
||||
*
|
||||
* Inspired by John Resig's JavaScript Micro-Templating:
|
||||
* http://ejohn.org/blog/javascript-micro-templating/
|
||||
*/
|
||||
|
||||
/* global define */
|
||||
|
||||
;(function ($) {
|
||||
'use strict'
|
||||
var tmpl = function (str, data) {
|
||||
var f = !/[^\w\-.:]/.test(str)
|
||||
? (tmpl.cache[str] = tmpl.cache[str] || tmpl(tmpl.load(str)))
|
||||
: new Function( // eslint-disable-line no-new-func
|
||||
tmpl.arg + ',tmpl',
|
||||
'var _e=tmpl.encode' +
|
||||
tmpl.helper +
|
||||
",_s='" +
|
||||
str.replace(tmpl.regexp, tmpl.func) +
|
||||
"';return _s;"
|
||||
)
|
||||
return data
|
||||
? f(data, tmpl)
|
||||
: function (data) {
|
||||
return f(data, tmpl)
|
||||
}
|
||||
}
|
||||
tmpl.cache = {}
|
||||
tmpl.load = function (id) {
|
||||
return document.getElementById(id).innerHTML
|
||||
}
|
||||
tmpl.regexp = /([\s'\\])(?!(?:[^{]|\{(?!%))*%\})|(?:\{%(=|#)([\s\S]+?)%\})|(\{%)|(%\})/g
|
||||
tmpl.func = function (s, p1, p2, p3, p4, p5) {
|
||||
if (p1) {
|
||||
// whitespace, quote and backspace in HTML context
|
||||
return (
|
||||
{
|
||||
'\n': '\\n',
|
||||
'\r': '\\r',
|
||||
'\t': '\\t',
|
||||
' ': ' '
|
||||
}[p1] || '\\' + p1
|
||||
)
|
||||
}
|
||||
if (p2) {
|
||||
// interpolation: {%=prop%}, or unescaped: {%#prop%}
|
||||
if (p2 === '=') {
|
||||
return "'+_e(" + p3 + ")+'"
|
||||
}
|
||||
return "'+(" + p3 + "==null?'':" + p3 + ")+'"
|
||||
}
|
||||
if (p4) {
|
||||
// evaluation start tag: {%
|
||||
return "';"
|
||||
}
|
||||
if (p5) {
|
||||
// evaluation end tag: %}
|
||||
return "_s+='"
|
||||
}
|
||||
}
|
||||
tmpl.encReg = /[<>&"'\x00]/g // eslint-disable-line no-control-regex
|
||||
tmpl.encMap = {
|
||||
'<': '<',
|
||||
'>': '>',
|
||||
'&': '&',
|
||||
'"': '"',
|
||||
"'": '''
|
||||
}
|
||||
tmpl.encode = function (s) {
|
||||
return (s == null ? '' : '' + s).replace(tmpl.encReg, function (c) {
|
||||
return tmpl.encMap[c] || ''
|
||||
})
|
||||
}
|
||||
tmpl.arg = 'o'
|
||||
tmpl.helper =
|
||||
",print=function(s,e){_s+=e?(s==null?'':s):_e(s);}" +
|
||||
',include=function(s,d){_s+=tmpl(s,d);}'
|
||||
if (typeof define === 'function' && define.amd) {
|
||||
define(function () {
|
||||
return tmpl
|
||||
})
|
||||
} else if (typeof module === 'object' && module.exports) {
|
||||
module.exports = tmpl
|
||||
} else {
|
||||
$.tmpl = tmpl
|
||||
}
|
||||
})(this)
|
||||
+2
@@ -0,0 +1,2 @@
|
||||
!function(e){"use strict";var n=function(e,t){var r=/[^\w\-.:]/.test(e)?new Function(n.arg+",tmpl","var _e=tmpl.encode"+n.helper+",_s='"+e.replace(n.regexp,n.func)+"';return _s;"):n.cache[e]=n.cache[e]||n(n.load(e));return t?r(t,n):function(e){return r(e,n)}};n.cache={},n.load=function(e){return document.getElementById(e).innerHTML},n.regexp=/([\s'\\])(?!(?:[^{]|\{(?!%))*%\})|(?:\{%(=|#)([\s\S]+?)%\})|(\{%)|(%\})/g,n.func=function(e,n,t,r,c,u){return n?{"\n":"\\n","\r":"\\r","\t":"\\t"," ":" "}[n]||"\\"+n:t?"="===t?"'+_e("+r+")+'":"'+("+r+"==null?'':"+r+")+'":c?"';":u?"_s+='":void 0},n.encReg=/[<>&"'\x00]/g,n.encMap={"<":"<",">":">","&":"&",'"':""","'":"'"},n.encode=function(e){return(null==e?"":""+e).replace(n.encReg,function(e){return n.encMap[e]||""})},n.arg="o",n.helper=",print=function(s,e){_s+=e?(s==null?'':s):_e(s);},include=function(s,d){_s+=tmpl(s,d);}","function"==typeof define&&define.amd?define(function(){return n}):"object"==typeof module&&module.exports?module.exports=n:e.tmpl=n}(this);
|
||||
//# sourceMappingURL=tmpl.min.js.map
|
||||
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"sources":["tmpl.js"],"names":["$","tmpl","str","data","f","test","Function","arg","helper","replace","regexp","func","cache","load","id","document","getElementById","innerHTML","s","p1","p2","p3","p4","p5","\n","\r","\t"," ","encReg","encMap","<",">","&","\"","'","encode","c","define","amd","module","exports","this"],"mappings":"CAgBC,SAAWA,GACV,aACA,IAAIC,EAAO,SAAUC,EAAKC,GACxB,IAAIC,EAAK,YAAYC,KAAKH,GAEtB,IAAII,SACJL,EAAKM,IAAM,QACX,qBACIN,EAAKO,OACL,QACAN,EAAIO,QAAQR,EAAKS,OAAQT,EAAKU,MAC9B,gBAPHV,EAAKW,MAAMV,GAAOD,EAAKW,MAAMV,IAAQD,EAAKA,EAAKY,KAAKX,IASzD,OAAOC,EACHC,EAAED,EAAMF,GACR,SAAUE,GACV,OAAOC,EAAED,EAAMF,KAGrBA,EAAKW,SACLX,EAAKY,KAAO,SAAUC,GACpB,OAAOC,SAASC,eAAeF,GAAIG,WAErChB,EAAKS,OAAS,2EACdT,EAAKU,KAAO,SAAUO,EAAGC,EAAIC,EAAIC,EAAIC,EAAIC,GACvC,OAAIJ,GAIEK,KAAM,MACNC,KAAM,MACNC,KAAM,MACNC,IAAK,KACLR,IAAO,KAAOA,EAGhBC,EAES,MAAPA,EACK,QAAUC,EAAK,MAEjB,MAAQA,EAAK,aAAeA,EAAK,MAEtCC,EAEK,KAELC,EAEK,aAFT,GAKFtB,EAAK2B,OAAS,eACd3B,EAAK4B,QACHC,IAAK,OACLC,IAAK,OACLC,IAAK,QACLC,IAAK,SACLC,IAAK,SAEPjC,EAAKkC,OAAS,SAAUjB,GACtB,OAAa,MAALA,EAAY,GAAK,GAAKA,GAAGT,QAAQR,EAAK2B,OAAQ,SAAUQ,GAC9D,OAAOnC,EAAK4B,OAAOO,IAAM,MAG7BnC,EAAKM,IAAM,IACXN,EAAKO,OACH,0FAEoB,mBAAX6B,QAAyBA,OAAOC,IACzCD,OAAO,WACL,OAAOpC,IAEkB,iBAAXsC,QAAuBA,OAAOC,QAC9CD,OAAOC,QAAUvC,EAEjBD,EAAEC,KAAOA,EA5EZ,CA8EEwC"}
|
||||
Reference in New Issue
Block a user