Parse basic template blocks in a string, like Handlebars block expressions but returns an object. Delimiters are customizable.
Install with npm
npm i parse-blocks --save
npm test
var blocks = require('parse-blocks');
Given the following arbitrary blocks in fixtures/c.txt
:
The following:
var str = fs.readFileSync('fixtures/c.txt', 'utf8');
console.log(blocks(str));
Results in:
{ definedoc:
{ foo:
{ args: [ 'a.b' ],
content: 'This is content foo',
orig:
[ '{{#definedoc "foo" a.b}}',
'This is content foo',
'{{/definedoc}}' ] },
bar:
{ args: [ 'a.c' ],
content: 'This is content bar',
orig:
[ '{{#definedoc "bar" a.c}}',
'This is content bar',
'{{/definedoc}}' ] },
baz:
{ args: [ 'a.d' ],
content: 'This is content baz',
orig:
[ '{{#definedoc "baz" a.d}}',
'This is content baz',
'{{/definedoc}}' ] } } }
Context can be passed as thisArg
when three arguments are passed, and/or on options.locals
.
Example
blocks(str, {locals: {a: {name: 'AAA'}}});
And this block:
Results in the following object:
{ apidoc:
{ aaa:
{ args: [ 'a' ],
context: { name: 'AAA' },
content: 'This is content <%= name %>',
orig:
[ '{{#apidoc "aaa" a}}',
'This is content <%= name %>',
'{{/apidoc}}' ] }
The content
can now be used to pass to any template engine with context
as locals.
Pull requests and stars are always welcome. For bugs and feature requests, please create an issue
Jon Schlinkert
Copyright (c) 2014 Jon Schlinkert
Released under the MIT license
This file was generated by verb on December 02, 2014.