Skip to content

Instantly share code, notes, and snippets.

@shawnbot
Created November 14, 2016 18:53
Show Gist options
  • Save shawnbot/b92f28c5b84aaee2922f1d9d3e20a18c to your computer and use it in GitHub Desktop.
Save shawnbot/b92f28c5b84aaee2922f1d9d3e20a18c to your computer and use it in GitHub Desktop.
#!/usr/bin/env node
'use strict';
// note: meta-template is not published on npm yet, so you will
// need to clone it and run `npm install && npm link` first!
const mt = require('meta-template');
var input = '{% if foo %}{{ foo }}{% else %}no foo!{% endif %}';
var ast = mt.parse.string(input);
// or read from stdin with something like:
// process.stdin.pipe(require('concat-stream')(buffer => {
// ast = mt.parse.buffer(buffer);
// }));
mt.ast.walk(ast, node => {
if (node.type === 'TemplateData') {
// you can do whatever you want with node.value here, e.g.
// node.value = '(' + node.value + ')';
}
});
var out = mt.format.php(ast);
console.log(out);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment