Comparing version 0.1.6 to 0.1.7-beta
70
index.js
/** | ||
* @file Smarty4Js | ||
* @file Smarty4js | ||
* @author johnson [zoumiaojiang@gmail.com] | ||
@@ -7,3 +7,2 @@ * @date 2014-11-13 | ||
var parser = require('./lib/parser/index'); | ||
var Compiler = require('./lib/compiler'); | ||
@@ -17,4 +16,6 @@ var Renderer = require('./lib/renderer'); | ||
/** | ||
* pre define template code | ||
* pretreatment template code | ||
* | ||
* @param {string} code template code | ||
@@ -24,8 +25,43 @@ * @param {Object} conf smarty config | ||
*/ | ||
function defineCode(code, conf) { | ||
function pretreatmentCode(code, conf) { | ||
var ld = conf.left_delimiter; | ||
var rd = conf.right_delimiter; | ||
return (code + ld + '*smarty4Js*' + rd) | ||
.replace(new RegExp(utils.regEscape(ld), 'g'), '{%') | ||
.replace(new RegExp(utils.regEscape(rd), 'g'), '%}') | ||
var reld = utils.regEscape(ld); | ||
var rerd = utils.regEscape(rd); | ||
// literal filter | ||
var literalPaserModulePath = path.resolve(__dirname, 'lib', 'parser','literal.js'); | ||
var literalPaserFile = fs.readFileSync(literalPaserModulePath, 'utf8'); | ||
var homePath = utils.getHomePath(); | ||
var realLiteralParserFilePath = path.resolve(homePath, 'literal.js'); | ||
if (ld !== '{%') { | ||
literalPaserFile = literalPaserFile.replace(/\\\{%/g, reld); | ||
} | ||
if (rd !== '%}') { | ||
literalPaserFile = literalPaserFile.replace(/%\\\}/g, rerd); | ||
} | ||
fs.writeFileSync(realLiteralParserFilePath, literalPaserFile); | ||
var literalAst = require(realLiteralParserFilePath).parse(code); | ||
var newCode = ''; | ||
literalAst.forEach(function (node) { | ||
var type = node.type; | ||
var value = node.value; | ||
if (value && value.trim().length > 0) { | ||
if (type === 'TEXT') { | ||
newCode += value; | ||
} | ||
else if (type === 'LTEXT') { | ||
newCode += value.replace(new RegExp(reld, 'g'), '__LD').replace(new RegExp(rerd, 'g'), '__RD'); | ||
} | ||
} | ||
}); | ||
// jison hack | ||
newCode += (ld + '*smarty4js*' + rd); | ||
return newCode | ||
.replace(/\\\"/g, '__QD') // replace \" in string "" | ||
@@ -35,2 +71,3 @@ .replace(/\\\'/g, '__QS'); // replace \' in string '' | ||
/** | ||
@@ -79,3 +116,9 @@ * @constructor | ||
var conf = this.conf; | ||
var filePath = path.resolve(process.cwd(), tpl); | ||
var filePath = path.resolve(process.cwd(), (tpl || '' + utils.getGUID())); | ||
var ld = conf.left_delimiter; | ||
var rd = conf.right_delimiter; | ||
var paserModulePath = path.resolve(__dirname, 'lib', 'parser','index.js'); | ||
var paserFile = fs.readFileSync(paserModulePath, 'utf8'); | ||
var homePath = utils.getHomePath(); | ||
var realParserFilePath = path.resolve(homePath, 'index.js'); | ||
@@ -86,4 +129,13 @@ if (fs.existsSync(filePath)) { | ||
} | ||
this.ast = parser.parse(defineCode(tpl, conf)); | ||
if (ld !== '{%') { | ||
paserFile = paserFile.replace(/\\\{%/g, utils.regEscape(ld)); | ||
} | ||
if (rd !== '%}') { | ||
paserFile = paserFile.replace(/%\\\}/g, utils.regEscape(rd)); | ||
} | ||
fs.writeFileSync(realParserFilePath, paserFile); | ||
this.ast = require(realParserFilePath).parse(pretreatmentCode(tpl, conf)); | ||
return this.compiler; | ||
@@ -90,0 +142,0 @@ }; |
@@ -1,1 +0,1 @@ | ||
var __nre=/[\.\(\)\[\]\{\}\+\-\*\?\|\^\$]/g;module.exports={escape:function(r,n){var t="";function e(r,n,t,e){r=r.toString(t||10);e=e||"0";while(r.length<n){r=e+r}return r.toUpperCase()}if(!r){return""}if(typeof r==="string"){if(!n){return this.escape(r,"html")}if(n==="html"||n==="htmlall"){var u={"<":"<",">":">","'":"'",'"':""","&":"&"};return r.replace(/['"<>&']/g,function(r){return u[r]})}if(n==="url"){return encodeURI(r)}if(n==="urlpathinfo"){return encodeURIComponent(r).replace(/%2F/g,"/")}if(n==="quotes"){return r.replace(/\'/g,"\\'").replace(/\"/g,'\\"')}if(n==="mail"){return r.replace(/@/g," [AT] ").replace(/\./g," [DOT] ")}if(n==="hex"){for(var o=0,i=r.length;o<i;o++){t+="%"+e(r.charCodeAt(o),2,16)}return t}if(n==="hexentity"){for(o=0,i=r.length;o<i;o++){t+="&#x"+e(r.charCodeAt(o),4,16)}return t}if(n==="decentity"){for(o=0,i=r.length;o<i;o++){t+="&#"+r.charCodeAt(o)+";"}return t}if(n==="javascript"){var a={"\\":"\\\\","'":"\\'",'"':'\\"',"\r":"\\r","\n":"\\n","</":"<\\/"};return r.replace(/[\\'"\r\n]|<\//g,function(r){return a[r]})}}return r},strip:function(r,n){return r.replace(/[\s\n\r\t]+/g,n&&"string"===typeof n?n:" ")},isset:function(r){return r===undefined?false:true},empty:function(r){var n=0;for(var t in r){if(r.hasOwnProperty(t)){n++}}return n>0?false:true},count:function(r){var n=0;for(var t in r){if(r.hasOwnProperty(t)){n++}}return n},sizeof:function(r){var n=0;for(var t in r){if(r.hasOwnProperty(t)){n++}}return n},time:function(){var r=new Date;return""+[r.getFullYear(),r.getMonth()+1,r.getDate()].join("-")+" "+[r.getHours(),r.getMinutes(),r.getSeconds()].join(":")},capitalize:function(r,n){return r.replace(/\w+\s*/g,function(r){return/\d/g.test(r)?n===true?r.charAt(0).toUpperCase()+r.slice(1):r:r.charAt(0).toUpperCase()+r.slice(1)})},cat:function(r,n){return""+r+n},count_characters:function(r,n){return n?r.length:r.split(/\s*/).join("").length},count_paragraphs:function(r,n){return r.split(new RegExp("[\r\n]"+n?"":"+")).length},count_sentences:function(r){return r.split(".").length},count_words:function(r){return r.split(/\w+\s*/).length-1},"default":function(r,n){return r!==undefined?r:n},indent:function(r,n,t){function e(r){var n="";for(var e=0;e<r;e++){n+=t?""+t:" "}return n}return"number"===typeof n&&n>0?e(n)+r:e(4)+r},lower:function(r){return(""+r).toLowerCase()},nl2br:function(r){return(""+r).replace(/\n/g,"<br/>")},regex_replace:function(r,n,t){return(""+r).replace(new Function("return "+n)(),t)},replace:function(r,n,t){return(""+r).split(n).join(t)},spacify:function(r,n){return n!==undefined?r.split("").join(n):r.split("").join(" ")},string_format:function(r,n){r=parseFloat(""+(r||0),10);return n!==undefined?n==="%d"?parseInt(""+r,10):/%\.(\d)f/.test(n)?r.toFixed(parseInt(RegExp.$1,10)):r:r},strip_tags:function(r){return(""+r).replace(/<.*?>/g,"")},truncate:function(r,n,t){r=""+r;return n>=r.length?r:r.substr(0,n>=0?n:80)+(t?""+t:"...")},upper:function(r){return(""+r).toUpperCase()},wordwrap:function(r,n,t){r=(""+r).split("");n=n>=0?n:80;for(var e=0,u=r.length;e<u;e++){if(e%n===0&&e!==0){r[e]=r[e]+(t?""+t:"\n")}}return r.join("")},is_array:function(r){return{}.toString.call(r)==="[object Object]"||{}.toString.call(r)==="[object Array]"},ceil:function(r){return Math.ceil(parseFloat(r||0,10))},range:function(r,n,t){var e=[];t=t||1;if(typeof r==="number"&&typeof n==="number"&&r<n){for(var u=r;u<=n;u+=t){e[(u-r)/t]=u}}if(typeof r==="string"&&typeof n==="string"){r=(""+r).charCodeAt(0);n=(""+n).charCodeAt(0);if(r<n){for(u=r;u<=n;u+=t){e[(u-r)/t]=String.fromCharCode(""+u)}}}return e},in_array:function(r,n){return n[r]?true:false},explode:function(r,n){var t={};var e=n.split(r);for(var u=0,o=e.length;u<o;u++){t["__a"+u]=e[u]}return t},implode:function(r,n){var t=[];for(var e in n){if(n.hasOwnProperty(e)){t.push(n[e])}}return t.join(r)},join:function(r,n){var t=[];for(var e in n){if(n.hasOwnProperty(e)){t.push(n[e])}}return t.join(r)},array:function(){return[]},array_unique:function(r){var n={};var t={};for(var e in r){if(r.hasOwnProperty(e)&&!n[r[e]]){n[r[e]]=true;t[e]=r[e]}}return t},array_sum:function(r){var n=0;for(var t in r){if(r.hasOwnProperty(t)&&typeof r[t]==="number"){n+=r[t]}}return n},array_product:function(r){var n=1;for(var t in r){if(r.hasOwnProperty(t)&&typeof r[t]==="number"){n*=r[t]}}return n},array_merge:function(){var r=Array.prototype.slice.call(arguments);var n={};var t=0;for(var e=0,u=r.length;e<u;e++){var o=r[e];for(var i in o){if(o.hasOwnProperty(i)){n[i.indexOf("__a")>-1?"__a"+t++:i]=o[i]}}}return n},array_merge_recursive:function(){var r=Array.prototype.slice.call(arguments);var n={};var t=0;for(var e=0,u=r.length;e<u;e++){var o=r[e];for(var i in o){if(o.hasOwnProperty(i)){n[i.indexOf("__a")>-1?"__a"+t++:i]=o[i]}}}return n},array_keys:function(r){var n=[];for(var t in r){if(r.hasOwnProperty(t)){n.push(t.replace("__a",""))}}return n},array_key_exists:function(r,n){return!n[r]===undefined?true:false},addcslashes:function(r,n){return(""+r).split(n).join("\\"+n)},stripcslashes:function(r){return(""+r).replace(/\\/,"")},addslashes:function(r){return(""+r).replace(/[\'\"\\]/g,function(r){return"\\"+r})},stripslashes:function(r){return(""+r).replace(/(\\\')|(\\\")|(\\\\)/g,function(r){return r.replace("\\","")})},chop:function(r,n){var t=n!==undefined?"["+(""+n).replace(__nre,function(r){return"\\"+r})+"]+$":"[\\0\\t\\n\\r\\s]+$";return(""+r).replace(new RegExp(t),"")},chr:function(r){return String.fromCharCode(""+r)},chunk_split:function(r,n,t){r=""+r;n=n||0;t=t||"";return n===0?r:r.replace(new RegExp(".{"+n+"}","g"),function(r){return r+t})},ltrim:function(r,n){var t=n!==undefined?"^["+(""+n).replace(__nre,function(r){return"\\"+r})+"]+":"^[\\0\\t\\n\\r\\s]+";return(""+r).replace(new RegExp(t),"")},rtrim:function(r,n){var t=n!==undefined?"["+(""+n).replace(__nre,function(r){return"\\"+r})+"]+$":"[\\0\\t\\n\\r\\s]+$";return(""+r).replace(new RegExp(t),"")},trim:function(r,n){var t=n!==undefined?(""+n).replace(__nre,function(r){return"\\"+r}):"\\0\\t\\n\\r\\s";return(""+r).replace(new RegExp("(^["+t+"]+)|(["+t+"]+$)","g"),"")},ord:function(r){return(""+r).charCodeAt(0)},parse_str:function(r){var n={};var t=(""+r).split("&");for(var e=0,u=t.length;e<u;e++){var o=t[e];o=o.indexOf("%")>-1?decodeURI(o):o;var i=o.split("=");n[i[0]]=i[1]||""}return n},print:function(){var r=Array.prototype.slice.call(arguments);var n="";for(var t=0,e=r.length;t<e;t++){var u=r[t];n+=u?u:""}return n},quotemeta:function(r){return(""+r).replace(__nre,function(r){return"\\"+r})},str_pad:function(r,n,t,e){var u={STR_PAD_RIGHT:0,STR_PAD_LEFT:1,STR_PAD_BOTH:2};var o="";var i=0;r=""+r;var a=r;n=n||0;t=t||"";e=e||"STR_PAD_RIGHT";if(n>r.length){if(u[e]!==2){i=(n-r.length)/t.length;while(i-->0){o+=t}a=u[e]===0?r+o:o+r}else{i=(n-r.length)/t.length/2;while(i-->0){o+=t}a=o+r+o}}return a},str_repeat:function(r,n){var t="";while(n--){t+=r}return t},str_split:function(r,n){var t=[];r=""+r;n=n||0;if(n!==0){var e=r.match(new RegExp(".{"+n+"}","g"));for(var u=0,o=e.length;u<o;u++){t.push(e[u])}var i=r.slice(o*n);if(i){t.push(i)}}else{t=[r]}return t},strcmp:function(r,n){r=""+r,n=""+n;return r===n?0:r>n?1:-1},strcasecmp:function(r,n){r=(""+r).toLowerCase(),n=""+n.toLowerCase();return r===n?0:r>n?1:-1},strchr:function(r,n){r=""+r;return r.slice(r.indexOf(typeof n==="number"?String.fromCharCode(n):""+n))},strstr:function(r,n){r=""+r;return r.slice(r.indexOf(typeof n==="number"?String.fromCharCode(n):""+n))},stristr:function(r,n){r=""+r;return r.slice(r.toLowerCase().indexOf(typeof n==="number"?String.fromCharCode(n).toLowerCase():(""+n).toLowerCase()))},stripos:function(r,n,t){r=""+r,t=t||0;return r.toLowerCase().indexOf((""+n).toLowerCase(),t)},strpos:function(r,n,t){r=""+r,t=t||0;return r.indexOf(""+n,t)},strlen:function(r){return(""+r).length},strrchr:function(r,n){r=""+r;return r.slice(r.lastIndexOf(typeof n==="number"?String.fromCharCode(n):""+n))},strrev:function(r){return(""+r).split("").reverse().join("")},strripos:function(r,n,t){r=""+r,t=t||0;return r.toLowerCase().lastIndexOf((""+n).toLowerCase(),t)},strrpos:function(r,n,t){r=""+r,t=t||0;return r.lastIndexOf(""+n,t)},strtolower:function(r){return(""+r).toLowerCase()},strtoupper:function(r){return(""+r).toUpperCase()},substr:function(r,n,t){r=""+r;n=n||0;t=t===undefined||typeof t!=="number"?-1:t;return r.substring(n,t===-1?r.length:n+t)},substr_count:function(r,n,t,e){r=""+r;t=t||0;e=e===undefined||typeof e!=="number"?-1:e;return r.substring(t,e===-1?r.length:t+e).split(n).length-1},ucfirst:function(r){r=""+r;return r.charAt(0).toUpperCase()+r.slice(1)},ucwords:function(r){return(""+r).replace(/\w+\s*/g,function(r){return/\d/g.test(r)?r:r.charAt(0).toUpperCase()+r.slice(1)})},rawurldecode:function(r){return decodeURIComponent(""+r)},rawurlencode:function(r){return encodeURIComponent(""+r)},urldecode:function(r){return decodeURI(""+r)},urlencode:function(r){return encodeURI(""+r)},is_bool:function(r){return r===true||r===false?true:false},floatval:function(r){return parseFloat(""+r,10)},intval:function(r){return parseInt(""+r,10)},is_float:function(r){return typeof r==="number"&&parseInt(r,10)!==r?true:false},is_real:function(r){return typeof r==="number"&&parseInt(r,10)!==r?true:false},is_int:function(r){return typeof r==="number"&&parseInt(r,10)===r?true:false},is_integer:function(r){return typeof r==="number"?true:false},is_object:function(r){return{}.toString.call(r)==="[object Object]"},is_callable:function(r){return typeof r==="function"?true:false},is_string:function(r){return typeof r==="string"?true:false},is_numeric:function(r){return typeof r==="number"?true:false},strval:function(r){return r!==undefined&&r.toString?r.toString():""},var_dump:function(r){function n(r){return{}.toString.call(r)==="[object Array]"}function t(r){return{}.toString.call(r)==="[object Object]"}function e(r){return n(r)||t(r)?true:false}function u(r){for(var n in r){if(r.hasOwnProperty(n)&&n.indexOf("__a")!==0){return false}}return true}function o(r){var i=n(r)||u(r)&&t(r)?0:t(r)&&!u(r)?1:2;var a;if(i===0||i===1){a=i===0?"[":"{";for(var f in r){if(r.hasOwnProperty(f)){var c=r[f];a+=(i===0?"":'"'+f+'":')+(e(c)?o(c):typeof c==="string"?'"'+c+'"':c)+","}}a=a.slice(0,a.length-1)+(i===0?"]":"}")}else{a=r}return a}return n(r)||t(r)?o(r).replace(/__a/g,""):r||""},echo:function(){var r=Array.prototype.slice.call(arguments);var n="";for(var t=0,e=r.length;t<e;t++){var u=r[t];n+=u?u:""}return n},abs:function(r){return Math.abs(r)},acos:function(r){return Math.acos(r)},acosh:function(r){return Math.acosh(r)},asin:function(r){return Math.asin(r)},asinh:function(r){return Math.asinh(r)},atan2:function(r,n){return Math.atan2(r,n)},atan:function(r){return Math.atan(r)},atanh:function(r){return Math.atanh(r)},cos:function(r){return Math.cos(r)},sin:function(r){return Math.sin(r)},tan:function(r){return Math.tan(r)},cosh:function(r){return Math.cosh(r)},sinh:function(r){return Math.sinh(r)},tanh:function(r){return Math.tanh(r)},dexbin:function(r){return r.toString(2)},dechex:function(r){return r.toString(16)},decoct:function(r){return r.toString(8)},deg2rad:function(r){return r*Math.PI/180},rad2deg:function(r){return r*180/Math.PI},exp:function(r){return Math.exp(r)},expm1:function(r){return Math.expm1(r)},floor:function(r){return Math.floor(r)},fmod:function(r,n){return typeof r==="number"&&typeof n==="number"&&r%n},hexdec:function(r){return parseInt(""+r,16)},octdec:function(r){return parseInt(""+r,8)},hypot:function(r,n){return typeof r==="number"&&typeof n==="number"&&Math.sqrt(r*r+n*n)},is_infinite:function(r){return""+r==="Infinity"?true:false},is_nan:function(r){return""+r==="NaN"?true:false},lcg_value:function(){return Math.random()},log10:function(r){return Math.log10(r)},log1p:function(r){return Math.log1p(r)},log:function(r){return Math.log(r)},max:function(){var r=Array.prototype.slice.call(arguments);var n=typeof r[0]==="object"?r[0]:r;var t=n[0];for(var e in n){if(n.hasOwnProperty(e)&&n[e]>t){t=n[e]}}return t},min:function(){var r=Array.prototype.slice.call(arguments);var n=typeof r[0]==="object"?r[0]:r;var t=n[0];for(var e in n){if(n.hasOwnProperty(e)&&n[e]<t){t=n[e]}}return t},pi:function(){return Math.PI},pow:function(r,n){return typeof r==="number"&&typeof n==="number"?Math.pow(r,n):false},round:function(r){return Math.round(r)},sqrt:function(r){return Math.sqrt(r)},uniqid:function(r){return(r||"")+(new Date).getTime()},highlight:function(r,n){return r},json_encode:function(r){function n(r){return{}.toString.call(r)==="[object Array]"}function t(r){return{}.toString.call(r)==="[object Object]"}function e(r){return n(r)||t(r)?true:false}function u(r){for(var n in r){if(r.hasOwnProperty(n)&&n.indexOf("__a")!==0){return false}}return true}function o(r){var i=n(r)||u(r)&&t(r)?0:t(r)&&!u(r)?1:2;var a=i===0?"[":"{";for(var f in r){if(r.hasOwnProperty(f)){var c=r[f];a+=(i===0?"":'"'+f+'":')+(e(c)?o(c):typeof c==="string"?'"'+c+'"':c)+","}}a=a.slice(0,a.length-1)+(i===0?"]":"}");return a}return o(r).replace(/__a/g,"")},json_decode:function(r){}}; | ||
var __nre=/[\.\(\)\[\]\{\}\+\-\*\?\|\^\$]/g;module.exports={escape:function(str,f){var tmp="";function padnum(n,w,r,p){n=n.toString(r||10);p=p||"0";while(n.length<w){n=p+n}return n.toUpperCase()}if(!str){return""}if(typeof str==="string"){if(!f){return this.escape(str,"html")}if(f==="html"||f==="htmlall"){var obj={"<":"<",">":">","'":"'",'"':""","&":"&"};return str.replace(/['"<>&']/g,function(s){return obj[s]})}if(f==="url"){return encodeURI(str)}if(f==="urlpathinfo"){return encodeURIComponent(str).replace(/%2F/g,"/")}if(f==="quotes"){return str.replace(/\'/g,"\\'").replace(/\"/g,'\\"')}if(f==="mail"){return str.replace(/@/g," [AT] ").replace(/\./g," [DOT] ")}if(f==="hex"){for(var i=0,l=str.length;i<l;i++){tmp+="%"+padnum(str.charCodeAt(i),2,16)}return tmp}if(f==="hexentity"){for(i=0,l=str.length;i<l;i++){tmp+="&#x"+padnum(str.charCodeAt(i),4,16)}return tmp}if(f==="decentity"){for(i=0,l=str.length;i<l;i++){tmp+="&#"+str.charCodeAt(i)+";"}return tmp}if(f==="javascript"){var map={"\\":"\\\\","'":"\\'",'"':'\\"',"\r":"\\r","\n":"\\n","</":"<\\/"};return str.replace(/[\\'"\r\n]|<\//g,function(s){return map[s]})}}return str},strip:function(str,s){return str.replace(/[\s\n\r\t]+/g,((s&&("string"===typeof s))?s:" "))},isset:function(any){return any===undefined?false:true},empty:function(obj){var n=0;for(var i in obj){if(obj.hasOwnProperty(i)){n++}}return(n>0)?false:true},count:function(obj){var n=0;for(var i in obj){if(obj.hasOwnProperty(i)){n++}}return n},sizeof:function(obj){var n=0;for(var i in obj){if(obj.hasOwnProperty(i)){n++}}return n},time:function(){var d=new Date();return""+[d.getFullYear(),d.getMonth()+1,d.getDate()].join("-")+" "+[d.getHours(),d.getMinutes(),d.getSeconds()].join(":")},capitalize:function(str,cnb){return str.replace(/\w+\s*/g,function(item){return/\d/g.test(item)?((cnb===true)?item.charAt(0).toUpperCase()+item.slice(1):item):item.charAt(0).toUpperCase()+item.slice(1)})},cat:function(a,b){return""+a+b},"count_characters":function(str,iws){return iws?str.length:str.split(/\s*/).join("").length},"count_paragraphs":function(str,iws){return str.split(new RegExp("[\r\n]"+iws?"":"+")).length},"count_sentences":function(str){return str.split(".").length},"count_words":function(str){return str.split(/\w+\s*/).length-1},"default":function(str,con){return str!==undefined?str:con},indent:function(str,num,repl){function gotSpace(n){var s="";for(var i=0;i<n;i++){s+=(repl?""+repl:" ")}return s}return("number"===typeof num&&num>0)?(gotSpace(num)+str):(gotSpace(4)+str)},lower:function(str){return(""+str).toLowerCase()},nl2br:function(str){return(""+str).replace(/\n/g,"<br/>")},"regex_replace":function(str,re,rs){return(""+str).replace((new Function("return "+re))(),rs)},replace:function(str,s,o){return(""+str).split(s).join(o)},spacify:function(str,ss){return ss!==undefined?str.split("").join(ss):str.split("").join(" ")},"string_format":function(num,f){num=parseFloat(""+(num||0),10);return f!==undefined?f==="%d"?parseInt(""+num,10):(/%\.(\d)f/.test(f))?num.toFixed(parseInt(RegExp.$1,10)):num:num},"strip_tags":function(str){return(""+str).replace(/<.*?>/g,"")},truncate:function(str,num,s){str=""+str;return num>=str.length?str:str.substr(0,((num>=0)?num:80))+(s?""+s:"...")},upper:function(str){return(""+str).toUpperCase()},wordwrap:function(str,num,s){str=(""+str).split("");num=(num>=0?num:80);for(var i=0,l=str.length;i<l;i++){if(i%num===0&&i!==0){str[i]=str[i]+(s?""+s:"\n")}}return str.join("")},"is_array":function(obj){return({}.toString.call(obj)==="[object Object]"||{}.toString.call(obj)==="[object Array]")},ceil:function(num){return Math.ceil(parseFloat((num||0),10))},range:function(a,b,step){var arr=[];step=step||1;if(typeof a==="number"&&typeof b==="number"&&a<b){for(var i=a;i<=b;i+=step){arr[(i-a)/step]=i}}if(typeof a==="string"&&typeof b==="string"){a=(""+a).charCodeAt(0);b=(""+b).charCodeAt(0);if(a<b){for(i=a;i<=b;i+=step){arr[(i-a)/step]=String.fromCharCode(""+i)}}}return arr},"in_array":function(any,array){return array[any]?true:false},explode:function(s,str){var obj={};var arr=str.split(s);for(var i=0,l=arr.length;i<l;i++){obj["__a"+i]=arr[i]}return obj},implode:function(s,obj){var arr=[];for(var p in obj){if(obj.hasOwnProperty(p)){arr.push(obj[p])}}return arr.join(s)},join:function(s,obj){var arr=[];for(var p in obj){if(obj.hasOwnProperty(p)){arr.push(obj[p])}}return arr.join(s)},array:function(){return[]},"array_unique":function(array){var obj={};var ret={};for(var p in array){if(array.hasOwnProperty(p)&&!obj[array[p]]){obj[array[p]]=true;ret[p]=array[p]}}return ret},"array_sum":function(array){var sum=0;for(var p in array){if(array.hasOwnProperty(p)&&typeof array[p]==="number"){sum+=array[p]}}return sum},"array_product":function(array){var sum=1;for(var p in array){if(array.hasOwnProperty(p)&&typeof array[p]==="number"){sum*=array[p]}}return sum},"array_merge":function(){var arrs=Array.prototype.slice.call(arguments);var obj={};var ind=0;for(var i=0,l=arrs.length;i<l;i++){var arr=arrs[i];for(var p in arr){if(arr.hasOwnProperty(p)){obj[(p.indexOf("__a")>-1)?("__a"+ind++):p]=arr[p]}}}return obj},"array_merge_recursive":function(){var arrs=Array.prototype.slice.call(arguments);var obj={};var ind=0;for(var i=0,l=arrs.length;i<l;i++){var arr=arrs[i];for(var p in arr){if(arr.hasOwnProperty(p)){obj[(p.indexOf("__a")>-1)?("__a"+ind++):p]=arr[p]}}}return obj},"array_keys":function(obj){var t=[];for(var p in obj){if(obj.hasOwnProperty(p)){t.push(p.replace("__a",""))}}return t},"array_key_exists":function(any,array){return !array[any]===undefined?true:false},addcslashes:function(str,c){return(""+str).split(c).join("\\"+c)},stripcslashes:function(str){return(""+str).replace(/\\/,"")},addslashes:function(str){return(""+str).replace(/[\'\"\\]/g,function(s){return"\\"+s})},stripslashes:function(str){return(""+str).replace(/(\\\')|(\\\")|(\\\\)/g,function(s){return s.replace("\\","")})},chop:function(str,clist){var res=(clist!==undefined)?"["+(""+clist).replace(__nre,function(s){return"\\"+s})+"]+$":"[\\0\\t\\n\\r\\s]+$";return(""+str).replace(new RegExp(res),"")},chr:function(asc){return String.fromCharCode(""+asc)},"chunk_split":function(str,len,end){str=""+str;len=len||0;end=end||"";return len===0?str:str.replace(new RegExp(".{"+len+"}","g"),function(s){return s+end})},ltrim:function(str,clist){var res=(clist!==undefined)?"^["+(""+clist).replace(__nre,function(s){return"\\"+s})+"]+":"^[\\0\\t\\n\\r\\s]+";return(""+str).replace(new RegExp(res),"")},rtrim:function(str,clist){var res=(clist!==undefined)?"["+(""+clist).replace(__nre,function(s){return"\\"+s})+"]+$":"[\\0\\t\\n\\r\\s]+$";return(""+str).replace(new RegExp(res),"")},trim:function(str,clist){var tmps=(clist!==undefined)?(""+clist).replace(__nre,function(s){return"\\"+s}):"\\0\\t\\n\\r\\s";return(""+str).replace(new RegExp("(^["+tmps+"]+)|(["+tmps+"]+$)","g"),"")},ord:function(str){return(""+str).charCodeAt(0)},"parse_str":function(str){var obj={};var arr=(""+str).split("&");for(var i=0,l=arr.length;i<l;i++){var item=arr[i];item=item.indexOf("%")>-1?decodeURI(item):item;var pi=item.split("=");obj[pi[0]]=pi[1]||""}return obj},print:function(){var args=Array.prototype.slice.call(arguments);var s="";for(var i=0,l=args.length;i<l;i++){var ts=args[i];s+=ts?ts:""}return s},quotemeta:function(str){return(""+str).replace(__nre,function(s){return"\\"+s})},"str_pad":function(str,len,pad,type){var to={"STR_PAD_RIGHT":0,"STR_PAD_LEFT":1,"STR_PAD_BOTH":2};var par="";var t=0;str=""+str;var ret=str;len=len||0;pad=pad||"";type=type||"STR_PAD_RIGHT";if(len>str.length){if(to[type]!==2){t=(len-str.length)/pad.length;while(t-->0){par+=pad}ret=to[type]===0?(str+par):(par+str)}else{t=(len-str.length)/pad.length/2;while(t-->0){par+=pad}ret=par+str+par}}return ret},"str_repeat":function(str,rn){var all="";while(rn--){all+=str}return all},"str_split":function(str,len){var arr=[];str=""+str;len=len||0;if(len!==0){var mats=str.match(new RegExp(".{"+len+"}","g"));for(var i=0,l=mats.length;i<l;i++){arr.push(mats[i])}var lef=str.slice(l*len);if(lef){arr.push(lef)}}else{arr=[str]}return arr},strcmp:function(str1,str2){str1=""+str1,str2=""+str2;return str1===str2?0:(str1>str2?1:-1)},strcasecmp:function(str1,str2){str1=(""+str1).toLowerCase(),str2=""+str2.toLowerCase();return str1===str2?0:(str1>str2?1:-1)},strchr:function(str,search){str=""+str;return str.slice(str.indexOf((typeof search==="number"?String.fromCharCode(search):(""+search))))},strstr:function(str,search){str=""+str;return str.slice(str.indexOf((typeof search==="number"?String.fromCharCode(search):(""+search))))},stristr:function(str,search){str=""+str;return str.slice(str.toLowerCase().indexOf((typeof search==="number"?String.fromCharCode(search).toLowerCase():(""+search).toLowerCase())))},stripos:function(str,find,start){str=""+str,start=start||0;return str.toLowerCase().indexOf((""+find).toLowerCase(),start)},strpos:function(str,find,start){str=""+str,start=start||0;return str.indexOf((""+find),start)},strlen:function(str){return(""+str).length},strrchr:function(str,search){str=""+str;return str.slice(str.lastIndexOf((typeof search==="number"?String.fromCharCode(search):(""+search))))},strrev:function(str){return(""+str).split("").reverse().join("")},strripos:function(str,find,start){str=""+str,start=start||0;return str.toLowerCase().lastIndexOf((""+find).toLowerCase(),start)},strrpos:function(str,find,start){str=""+str,start=start||0;return str.lastIndexOf((""+find),start)},strtolower:function(str){return(""+str).toLowerCase()},strtoupper:function(str){return(""+str).toUpperCase()},substr:function(str,start,len){str=""+str;start=start||0;len=(len===undefined||typeof len!=="number")?-1:len;return str.substring(start,(len===-1?str.length:(start+len)))},"substr_count":function(str,substr,start,len){str=""+str;start=start||0;len=(len===undefined||typeof len!=="number")?-1:len;return str.substring(start,(len===-1?str.length:(start+len))).split(substr).length-1},ucfirst:function(str){str=""+str;return str.charAt(0).toUpperCase()+str.slice(1)},ucwords:function(str){return(""+str).replace(/\w+\s*/g,function(item){return/\d/g.test(item)?item:item.charAt(0).toUpperCase()+item.slice(1)})},rawurldecode:function(str){return decodeURIComponent(""+str)},rawurlencode:function(url){return encodeURIComponent(""+url)},urldecode:function(str){return decodeURI(""+str)},urlencode:function(url){return encodeURI(""+url)},"is_bool":function(bool){return(bool===true||bool===false)?true:false},floatval:function(str){return parseFloat(""+str,10)},intval:function(str){return parseInt(""+str,10)},"is_float":function(f){return(typeof f==="number"&&parseInt(f,10)!==f)?true:false},"is_real":function(f){return(typeof f==="number"&&parseInt(f,10)!==f)?true:false},"is_int":function(i){return(typeof i==="number"&&parseInt(i,10)===i)?true:false},"is_integer":function(i){return(typeof i==="number")?true:false},"is_object":function(o){return{}.toString.call(o)==="[object Object]"},"is_callable":function(o){return(typeof o==="function")?true:false},"is_string":function(s){return(typeof s==="string")?true:false},"is_numeric":function(a){return(typeof a==="number")?true:false},strval:function(any){return(any!==undefined&&any.toString)?any.toString():""},"var_dump":function(any){function isArray(o){return{}.toString.call(o)==="[object Array]"}function isObj(o){return{}.toString.call(o)==="[object Object]"}function isOA(o){return(isArray(o)||isObj(o))?true:false}function isRealArr(o){for(var p in o){if(o.hasOwnProperty(p)&&p.indexOf("__a")!==0){return false}}return true}function pa(o){var type=(isArray(o)||(isRealArr(o)&&isObj(o)))?0:((isObj(o)&&!isRealArr(o))?1:2);var __enc;if(type===0||type===1){__enc=type===0?"[":"{";for(var p in o){if(o.hasOwnProperty(p)){var to=o[p];__enc+=(type===0?"":('"'+p+'":'))+(isOA(to)?pa(to):(typeof to==="string"?'"'+to+'"':to))+","}}__enc=__enc.slice(0,__enc.length-1)+(type===0?"]":"}")}else{__enc=o}return __enc}return(isArray(any)||isObj(any))?pa(any).replace(/__a/g,""):(any||"")},echo:function(){var args=Array.prototype.slice.call(arguments);var s="";for(var i=0,l=args.length;i<l;i++){var ts=args[i];s+=ts?ts:""}return s},abs:function(num){return Math.abs(num)},acos:function(deg){return Math.acos(deg)},acosh:function(deg){return Math.acosh(deg)},asin:function(deg){return Math.asin(deg)},asinh:function(deg){return Math.asinh(deg)},atan2:function(x,y){return Math.atan2(x,y)},atan:function(p){return Math.atan(p)},atanh:function(x){return Math.atanh(x)},cos:function(x){return Math.cos(x)},sin:function(x){return Math.sin(x)},tan:function(x){return Math.tan(x)},cosh:function(x){return Math.cosh(x)},sinh:function(x){return Math.sinh(x)},tanh:function(x){return Math.tanh(x)},dexbin:function(num){return num.toString(2)},dechex:function(num){return num.toString(16)},decoct:function(num){return num.toString(8)},deg2rad:function(deg){return deg*Math.PI/180},rad2deg:function(rad){return rad*180/Math.PI},exp:function(x){return Math.exp(x)},expm1:function(x){return Math.expm1(x)},floor:function(x){return Math.floor(x)},fmod:function(x,y){return(typeof x==="number"&&typeof y==="number")&&x%y},hexdec:function(hex){return parseInt(""+hex,16)},octdec:function(oct){return parseInt(""+oct,8)},hypot:function(x,y){return(typeof x==="number"&&typeof y==="number")&&Math.sqrt(x*x+y*y)},"is_infinite":function(x){return((""+x)==="Infinity")?true:false},"is_nan":function(x){return((""+x)==="NaN")?true:false},"lcg_value":function(){return Math.random()},log10:function(x){return Math.log10(x)},log1p:function(x){return Math.log1p(x)},log:function(x){return Math.log(x)},max:function(){var arrs=Array.prototype.slice.call(arguments);var obj=(typeof arrs[0]==="object")?arrs[0]:arrs;var max=obj[0];for(var p in obj){if(obj.hasOwnProperty(p)&&obj[p]>max){max=obj[p]}}return max},min:function(){var arrs=Array.prototype.slice.call(arguments);var obj=(typeof arrs[0]==="object")?arrs[0]:arrs;var min=obj[0];for(var p in obj){if(obj.hasOwnProperty(p)&&obj[p]<min){min=obj[p]}}return min},pi:function(){return Math.PI},pow:function(x,y){return(typeof x==="number"&&typeof y==="number")?Math.pow(x,y):false},round:function(x){return Math.round(x)},sqrt:function(x){return Math.sqrt(x)},uniqid:function(prefix){return(prefix||"")+(new Date()).getTime()},highlight:function(str,type){return str},"json_encode":function(obj){function isArray(o){return{}.toString.call(o)==="[object Array]"}function isObj(o){return{}.toString.call(o)==="[object Object]"}function isOA(o){return(isArray(o)||isObj(o))?true:false}function isRealArr(o){for(var p in o){if(o.hasOwnProperty(p)&&p.indexOf("__a")!==0){return false}}return true}function pa(o){var type=(isArray(o)||(isRealArr(o)&&isObj(o)))?0:((isObj(o)&&!isRealArr(o))?1:2);var __enc=type===0?"[":"{";for(var p in o){if(o.hasOwnProperty(p)){var to=o[p];__enc+=(type===0?"":('"'+p+'":'))+(isOA(to)?pa(to):(typeof to==="string"?'"'+to+'"':to))+","}}__enc=(__enc.length<=1?__enc:__enc.slice(0,__enc.length-1))+(type===0?"]":"}");return __enc}return pa(obj).replace(/__a/g,"")},date_format:function(time,format){time=(time instanceof Date)?time:new Date(+time);if(format){var o={"%m":time.getMonth()+1,"%d":time.getDate(),"%H":time.getHours(),"%M":time.getMinutes(),"%S":time.getSeconds(),"%s":time.getMilliseconds()};if(/(\%Y)/.test(format)){format=format.replace(RegExp.$1,(time.getFullYear()+""))}for(var k in o){if(o.hasOwnProperty(k)&&new RegExp("("+k+")").test(format)){format=format.replace(RegExp.$1,RegExp.$1.length===1?o[k]:("00"+o[k]).substr((""+o[k]).length))}}}else{format=time.toDateString()}return format},"json_decode":function(json){}}; |
@@ -926,3 +926,3 @@ /** | ||
} | ||
__enc = __enc.slice(0, __enc.length - 1) + (type === 0 ? ']' : '}'); | ||
__enc = (__enc.length <= 1 ? __enc : __enc.slice(0, __enc.length - 1)) + (type === 0 ? ']' : '}'); | ||
return __enc; | ||
@@ -933,2 +933,30 @@ } | ||
date_format: function (time, format) { | ||
time = (time instanceof Date) ? time : new Date(+time); | ||
if (format) { | ||
var o = { | ||
'\%m': time.getMonth() + 1, | ||
'\%d': time.getDate(), | ||
'\%H': time.getHours(), | ||
'\%M': time.getMinutes(), | ||
'\%S': time.getSeconds(), | ||
'\%s': time.getMilliseconds() | ||
} | ||
if (/(\%Y)/.test(format)) { | ||
format = format.replace(RegExp.$1, (time.getFullYear() + '')); | ||
} | ||
for (var k in o) { | ||
if (o.hasOwnProperty(k) && new RegExp('(' + k + ')').test(format)) { | ||
format = format.replace(RegExp.$1, RegExp.$1.length === 1 | ||
? o[k] | ||
: ('00' + o[k]).substr(('' + o[k]).length)); | ||
} | ||
} | ||
} | ||
else { | ||
format = time.toDateString(); | ||
} | ||
return format; | ||
}, | ||
'json_decode': function (json) { | ||
@@ -935,0 +963,0 @@ // TODO |
@@ -51,4 +51,2 @@ /** | ||
return me._getInclude(node); | ||
case 'literal': | ||
return me._getFuncLiteral(node); | ||
case 'nocache': | ||
@@ -55,0 +53,0 @@ return me._getFuncLiteral(node); |
@@ -256,8 +256,16 @@ /** | ||
} | ||
// finanly escape to normal JS code(replace placeholder) | ||
var config = me.engine.conf; | ||
// finanly escape to normal JS code (replace placeholder) | ||
// __D: . | ||
// __QD: " | ||
// __QS: ' | ||
return ret.replace(/__D/g, '.').replace(/__QD/g, '\\\"').replace(/__QS/g, '\\\''); | ||
// __LD: left_delimiter | ||
// __RD: right_delimiter | ||
ret = ret | ||
.replace(/__D/g, '.') | ||
.replace(/__QD/g, '\\\"') | ||
.replace(/__QS/g, '\\\'') | ||
.replace(/__RD/g, config.right_delimiter) | ||
.replace(/__LD/g, config.left_delimiter); | ||
return ret; | ||
} | ||
@@ -264,0 +272,0 @@ }; |
@@ -407,3 +407,3 @@ /** | ||
case '$smarty.now': | ||
return 'new Date().getTime()'; | ||
return 'Date.now()'; | ||
case '$smarty.ldelim': | ||
@@ -410,0 +410,0 @@ return 'smarty__Dldelim'; |
@@ -29,2 +29,3 @@ /** | ||
var end = node.end; | ||
var step = node.step; | ||
var fel = 0; // foreachelse | ||
@@ -34,4 +35,14 @@ var me = this; | ||
var ret; | ||
var stepStr = 'i++'; | ||
if (start && end) { | ||
if (step) { | ||
if (step.key === 'step') { | ||
stepStr = 'i+=' + this._getExpr(step.value); | ||
} | ||
// Just let it going on ~~ :P | ||
// else { | ||
// throw new Error('... for $item = $start to $end ^ [Expecting `step`] ...'); | ||
// } | ||
} | ||
ret = '\n' | ||
@@ -41,3 +52,3 @@ + 'for(' | ||
+ fc + 'i<=' + me._getExpr(end) + ';' | ||
+ fc + 'i++' | ||
+ fc + stepStr //'i++' | ||
+ '){\n'; | ||
@@ -44,0 +55,0 @@ me.ctxScope.push(fc); |
@@ -7,2 +7,3 @@ /** | ||
var guidIndex = 0x0907; | ||
var fs = require('fs'); | ||
@@ -182,3 +183,21 @@ module.exports = { | ||
return flag; | ||
}, | ||
/** | ||
* get home path | ||
* | ||
* @return {string} home path | ||
*/ | ||
getHomePath: function () { | ||
var path = require('path'); | ||
var dir = process.env[ | ||
require('os').platform() === 'win32' | ||
? 'APPDATA' | ||
: 'HOME' | ||
] + path.sep + '.smarty4js'; | ||
!fs.existsSync(dir) && fs.mkdirSync(dir); | ||
return dir; | ||
} | ||
}; |
{ | ||
"name": "smarty4js", | ||
"description": "A JavaScript Template Engine Most Like Smarty", | ||
"version": "0.1.6", | ||
"version": "0.1.7-beta", | ||
"keywords": [ | ||
@@ -25,6 +25,6 @@ "smarty", | ||
"bugs": { | ||
"url": "https://github.com/zoumiaojiang/smarty4Js/issues" | ||
"url": "https://github.com/ecomfe/smarty4js/issues" | ||
}, | ||
"bin": { | ||
"smarty4Js": "./bin/smarty4Js-cli" | ||
"smarty4js": "./bin/smarty4js-cli" | ||
}, | ||
@@ -35,3 +35,3 @@ "main": "index", | ||
}, | ||
"repository": "git://github.com/zoumiaojiang/smarty4Js" | ||
"repository": "git://github.com/ecomfe/smarty4js" | ||
} |
Smarty4Js (Smarty For JavaScript) | ||
====================================== | ||
## what? | ||
## What? | ||
- JavaScript template engine | ||
- Support smarty template grammar | ||
- Used in nodejs | ||
- Simple and powerful way to creat and render a template | ||
- Supports Smarty template syntax | ||
- Used in Node.js | ||
- Simple and powerful way to create and render a template | ||
## why? | ||
## Why? | ||
- Powerful | ||
- Don't want to have two or more template in your project. | ||
- Deep love smarty | ||
- Don't want to have two or more templates in your project | ||
- Deep love for Smarty | ||
## when? | ||
## When? | ||
- When your Synchronous template is Smarty ,and your ajax template will use the same template. | ||
- When you want to write smarty template with JavaScript anywhere. | ||
- When you want to put a lot of logic in JavaScript template | ||
- When you like smarty very much | ||
- When your synchronous templates is Smarty, and your Ajax templates will use the same template | ||
- When you want to write Smarty templates with JavaScript anywhere | ||
- When you want to put a lot of logic in JavaScript templates | ||
- When you like Smarty very much | ||
## how? | ||
## How? | ||
- single | ||
- with grunt | ||
- with gulp | ||
- with edp | ||
- with all nodejs web subject | ||
- Single | ||
- With Grunt | ||
- With Gulp | ||
- With EDP | ||
- With all Node.js web subject | ||
### useage | ||
### Usage | ||
#### install | ||
#### Install | ||
```bash | ||
[sudo] npm install -g smarty4js | ||
npm install -g smarty4js | ||
``` | ||
#### grammar | ||
#### Syntax | ||
``` | ||
most like smarty template grammar | ||
Most of Smarty syntax is supported. | ||
``` | ||
#### command line | ||
#### Command line | ||
```bash | ||
@@ -54,4 +54,4 @@ // get tpl.js | ||
smarty4Js render a.tpl [b.tpl ...] -d|--data=jsonFilePath [-c|--config=confPath -o|--output=outputPath] | ||
-d | --data : json data file path for render | ||
-c | --config: Use user-define config file | ||
-d | --data : JSON data file path to render | ||
-c | --config: Use user-defined config file | ||
-o | --output: Specify a destination directory | ||
@@ -65,5 +65,5 @@ | ||
- if `render`, return html(`demo.tpl.html`), if `compile`, return jsTpl(`demo.tpl.js`) | ||
- jsTpl is closure, hava a `render` method, you can use it like | ||
- jsTpl is a closure. It has a `render` method that you can use: | ||
```javascript | ||
```Javascript | ||
@@ -74,7 +74,7 @@ // amd and cmd | ||
// also could by <script> | ||
// you can also use <script> | ||
``` | ||
#### used in nodejs | ||
#### used in Node.js | ||
```javascript | ||
@@ -84,7 +84,7 @@ // get Smarty class | ||
// create a smarty object | ||
// create a Smarty object | ||
var s = new Smarty(); | ||
// if compile source is template code and hava `include, extend...` sentence in code | ||
// you must give a dirpath by `setBasedir` method | ||
// if compile source is template code and have `include, extend...` sentence in code | ||
// you must give a path by `setBasedir` method | ||
s.setBasedir(path); | ||
@@ -103,3 +103,3 @@ | ||
// render smarty with data (3 methods) | ||
// render Smarty with data (3 methods) | ||
var html = compiler.render(data); | ||
@@ -106,0 +106,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No bug tracker
MaintenancePackage does not have a linked bug tracker in package.json.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
243857
36116
24
4967
1
9