Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function run(tpl, options) {
// console.log()
// console.log(tpl);
// console.log(options);
var ast = Handlebars.parse(tpl);
// console.log(ast);
// console.log(JSON.stringify(ast, null, 4));
var data = Mock4Tpl.gen(ast, null, options)
// console.log(JSON.stringify(data, null, 4));
return data
}
function convertTemplateToObject(template) {
var statements = Handlebars.parse(template).statements;
var html = '';
for (var i = 0, len = statements.length; i < len; i++) {
var stat = statements[i];
// AST.ContentNode
if (stat.type === 'content') {
html += stat.string;
}
// AST.MustacheNode or AST.BlockNode
else {
html += '{{STAT ' + i + '}}';
}
}
html = encode(html);
}
const contents = file.contents.toString();
let compiled = null;
// binary files
if (['favicon.ico'].indexOf(file.path.substr(file.path.lastIndexOf('/') + 1)) !== -1) {
// binary file are stored as is
compiled =
"{\"compiler\":[7,\">= 4.0.0\"],\"main\":function(container,depth0,helpers,partials,data) {\n" +
" return " + JSON.stringify(contents) + ";\n" +
"},\"useData\":false}\n";
} else {
try {
compiled = handlebars.precompile(
handlebars.parse(contents),
compilerOptions
).toString();
} catch (err) {
console.log(err);
this.emit('error', new PluginError("Handlebars plugin", err, {
fileName: file.path
}));
return callback();
}
}
file.contents = Buffer.from(compiled);
file.templatePath = file.relative;
file.path = gutil.replaceExtension(file.path, '.js');
callback(null, file);
exports.ast_block = function(test) {
var tpl = Mock.heredoc(function() {
/*
{{#each list}}
{{title}}
{{/each}}
*/
})
var ast = Handlebars.parse(tpl)
var blocks = {}
Bind.AST.handle(ast, blocks)
// console.log(JSON.stringify(ast, null, 4))
// console.log(JSON.stringify(blocks, null, 4))
// 开始占位符,定位 Block 区域
test.equal(ast.statements[0].type, 'content')
test.equal(ast.statements[0].string.indexOf('<script'), 0)
// 结束占位符
test.equal(ast.statements[4].type, 'content')
test.equal(ast.statements[4].string.indexOf('<script'), 0)
// 路径占位符,定位 Block 对应的数据路径
var block = ast.statements[3]
var helper = helpers[file] = require(Path.resolve(Path.join(options.helpers, file)) );
output.push('Handlebars.registerHelper("' + file + '", ' + helper.toString() + ');');
});
}
try {
// delete any cached versions of the template
compiled_hbs = handlebars.precompile( file_contents );
output.push(options.namespace + '["'+ nm +'"] = Handlebars.template('+ compiled_hbs +');\n');
output.push('Handlebars.partials["'+ nm.replace(/\//g, '.') +'"] = ' + options.namespace + '["'+ nm +'"];\n');
if(options.vars) {
var vars = recursiveVarSearch( handlebars.parse( file_contents ).statements, [], undefined, [] );
vars = _.uniq(vars);
output.push(options.namespace + '["'+ nm +'"].vars = ' + JSON.stringify(vars)+';\n');
}
} catch( e ){
console.error( 'Error processing'+ nm, e);
return '/* Unable to compile ' + nm + ' */\n';
}
return output.join('');
};
RawHandlebars.compile = function (string) {
let ast = Handlebars.parse(string);
replaceGet(ast);
// this forces us to rewrite helpers
let options = { data: true, stringParams: true };
let environment = new RawHandlebars.Compiler().compile(ast, options);
let templateSpec = new RawHandlebars.JavaScriptCompiler().compile(
environment,
options,
undefined,
true
);
let t = RawHandlebars.template(templateSpec);
t.isMethod = false;
return t;
RawHandlebars.compile = function (string) {
var ast = Handlebars.parse(string);
replaceGet(ast);
// this forces us to rewrite helpers
var options = { data: true, stringParams: true };
var environment = new RawHandlebars.Compiler().compile(ast, options);
var templateSpec = new RawHandlebars.JavaScriptCompiler().compile(
environment,
options,
undefined,
true
);
var t = RawHandlebars.template(templateSpec);
t.isMethod = false;
return t;
it("should be aliased", () =>
{
var hbs = "{{! comment }} content {{!-- comment --}}";
hbs = handlebars.parse(hbs);
hbs = aliases.stringify(hbs);
expect(hbs).to.equal("handlebars-html-parser-0-alias content handlebars-html-parser-2-alias");
});
});
var path = require('path');
var readline = require('readline');
var templateFile = args[0];
var templateFileBasename = path.basename(templateFile, '.hbs');
var outputFile = args[1] || path.join(process.cwd(), templateFileBasename);
var basename = path.basename(process.cwd());
var template;
try {
template = fs.readFileSync(templateFile, 'utf8');
} catch (error) {
console.log(`\n Error: there was a problem reading ${templateFile}`);
process.exit(1);
}
var ast = Handlebars.parse(template).body;
var prompts = {};
var answers = {};
(function prepare(ast, parent) {
var id;
ast.forEach(statement => {
switch (statement.type) {
case 'CommentStatement':
id = `comment-${Math.random().toString(16).slice(2)}`;
if (parent[id]) {
return;
}
parent[id] = {
export function preprocess(html: string, options: PreprocessOptions = {}): AST.Template {
let mode = options.mode || 'precompile';
let ast: HBS.Program;
if (typeof html === 'object') {
ast = html;
} else if (mode === 'codemod') {
ast = handlebars.parseWithoutProcessing(html, options.parseOptions) as HBS.Program;
} else {
ast = handlebars.parse(html, options.parseOptions) as HBS.Program;
}
let entityParser = undefined;
if (mode === 'codemod') {
entityParser = new EntityParser({});
}
let program = new TokenizerEventHandlers(html, entityParser).acceptTemplate(ast);
if (options && options.plugins && options.plugins.ast) {
for (let i = 0, l = options.plugins.ast.length; i < l; i++) {
let transform = options.plugins.ast[i];
let env = assign({}, options, { syntax }, { plugins: undefined });
let pluginResult = transform(env);