Skip to content

Commit

Permalink
fix #2 & some strategy optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
zoumiaojiang committed Nov 12, 2015
1 parent 42fd54e commit 1eec9e2
Show file tree
Hide file tree
Showing 10 changed files with 181 additions and 129 deletions.
2 changes: 2 additions & 0 deletions bin/smarty4js-cli
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ function smartyCompile(tplPaths, conf, options) {
ret,
{encodeing: 'utf8'}
);
console.log('smarty4js compile success!');
});

}
Expand All @@ -77,6 +78,7 @@ function smartyRender(tplPaths, data, conf, options) {
ret,
{encodeing: 'utf8'}
);
console.log('smarty4js render success!');
});
}

Expand Down
6 changes: 5 additions & 1 deletion examples/modules/a/b/b.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,8 @@

{%$b = 'this is b page'%}

{%$b%}
{%$b%}

{%literal%}
{%$a.b.c%}
{%/literal%}
2 changes: 1 addition & 1 deletion examples/modules/a/b/c/d/d.tpl.html
Original file line number Diff line number Diff line change
@@ -1 +1 @@
this is a pagethis is b pagethis is a pagethis is c pagethis is d page
this is a pagethis is b page {%$a.b.c%}this is a pagethis is c pagethis is d page
3 changes: 2 additions & 1 deletion examples/modules/a/b/c/d/d.tpl.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
var __h="",__cap={},__ext={},__assign,__sec={},__for={},smarty={foreach:{},capture:{},ldelim:"{%",rdelim:"%}"},__dre=/^\d+(\.\d+)?$/g,__nre=/[\.\(\)\[\]\{\}\+\-\*\?\|\^\$]/g,
__v=function(){var __va=Array.prototype.slice.call(arguments);for(var __vi=0,__vl=__va.length;__vi<__vl;__vi++){var __vd=__va[__vi];if(__vd!=undefined&&""+__vd!="NaN"){if(typeof __vd=="function"){return __vd();}else{return __vd;}}}};var __ic2315={};var __ic2318={};__assign="this is a page";__da.a=__assign;
__h+=__v(__ic2318.a,__ic2315.a,__da.a);__assign="this is b page";__da.b=__assign;
__h+=__v(__ic2315.b,__da.b);var __ic2321={};var __ic2324={};__assign="this is a page";__da.a=__assign;
__h+=__v(__ic2315.b,__da.b);
__h+=" {%$a.b.c%}";var __ic2321={};var __ic2324={};__assign="this is a page";__da.a=__assign;
__h+=__v(__ic2324.a,__ic2321.a,__da.a);__assign="this is c page";__da.c=__assign;
__h+=__v(__ic2321.c,__da.c);__assign="this is d page";__da.d=__assign;
__h+=__v(__da.d);
Expand Down
68 changes: 54 additions & 14 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
/**
* @file Smarty4Js
* @file Smarty4js
* @author johnson [[email protected]]
* @date 2014-11-13
*/

var parser = require('./lib/parser/index');
var Compiler = require('./lib/compiler');
var Renderer = require('./lib/renderer');
var phpfunc = require('./lib/phpfunc');
Expand All @@ -13,29 +12,55 @@ var utils = require('./lib/utils');
var path = require('path');
var fs = require('fs');


/**
* pre define template code
* pretreatment template code
*
* @param {string} code template code
* @param {Object} conf smarty config
* @return {string} code of after pre define
*/
function defineCode(code, conf) {
function pretreatmentCode(code, conf) {
var ld = conf.left_delimiter;
var rd = conf.right_delimiter;

// literal filter
var reld = utils.regEscape(ld);
var rerd = utils.regEscape(rd);
var re = new RegExp(reld + 'literal' + rerd + '([\\\s\\\S]*)' + reld + '\\/literal' + rerd, 'g');

code = code.replace(re, function () {
return RegExp.$1.replace(new RegExp(reld, 'g'), '__LD').replace(new RegExp(rerd, 'g'), '__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');
}
}
});

return (code + ld + '*smarty4js*' + rd)
.replace(new RegExp(reld, 'g'), '{%')
.replace(new RegExp(rerd, 'g'), '%}')
// jison hack
newCode += (ld + '*smarty4js*' + rd);

return newCode
.replace(/\\\"/g, '__QD') // replace \" in string ""
.replace(/\\\'/g, '__QS'); // replace \' in string ''
}
Expand Down Expand Up @@ -84,13 +109,28 @@ Smarty.prototype.setBasedir = function (path) {
Smarty.prototype.compile = function (tpl) {

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');

if (fs.existsSync(filePath)) {
tpl = fs.readFileSync(filePath, 'utf-8');
this.dirname = path.dirname(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;
};
Expand Down
20 changes: 4 additions & 16 deletions lib/parser/index.jison
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ kw (
'elseif'|'if'|'foreach'|'else'|'foreachelse'|
'section'|'sectionelse'|'for'|'to'|'while'|'as'|
'true'|'false'|'null'|'function'|'strip'|'capture'|
'block'|'literal'|'nocache'
'block'|'nocache'
)
multi_op ('>='|'<='|'==='|'=='|'!='|'&&'|'||'|'->'|'=>'|'++'|'--')
al_op ('and'|'or'|'ge'|'not'|'gte'|'le'|'lte'|'lt'|'gt'|'ne'|'neq'|'eq')
Expand Down Expand Up @@ -61,13 +61,13 @@ al_op ('and'|'or'|'ge'|'not'|'gte'|'le'|'lte'|'lt'|'gt'|'ne'|'neq'|'eq')


/*----- start lexical grammar -----*/
%x t v iv eof c g
%x t v iv eof c
%%
<v,iv,t,eof>{ot}/'*' { this.begin('c'); return 'L'; }
<v,iv>'$smarty'((\.[\w]+)+)? { return 'G'; }
<eof>{ot} { this.popState(); this.begin('v'); return 'L'; }
<v,iv>{ot} { this.begin('iv'); return 'L'; }
<v,iv,c,g>{ct} {
<v,iv,c>{ct} {
var s = this.popState();
if ('c' == s) {
s = this.popState();
Expand Down Expand Up @@ -98,7 +98,7 @@ al_op ('and'|'or'|'ge'|'not'|'gte'|'le'|'lte'|'lt'|'gt'|'ne'|'neq'|'eq')
return 'TEXT';
}
}
<eof><<EOF>> { this.popState(); return 'EOF'; }
<t,eof><<EOF>> { this.popState(); return 'EOF'; }
<INITIAL><<EOF>> { return 'EOF'; }
<INITIAL> { this.begin('t');}

Expand Down Expand Up @@ -183,8 +183,6 @@ blocks
{ $$ = $1; }
| block_stmts
{ $$ = $1; }
| literal_stmts
{ $$ = $1; }
| capture_stmts
{ $$ = $1; }
| nocache_stmts
Expand Down Expand Up @@ -241,16 +239,6 @@ capture_stmts
}; }
;

literal_stmts
: L literal R stmts L '/' literal R
{ $$ = {
type: 'FUNC',
name: $2,
attrs: [],
block: $4
}; }
;

nocache_stmts
: L nocache R stmts L '/' nocache R
{ $$ = {
Expand Down
Loading

0 comments on commit 1eec9e2

Please sign in to comment.