Disclaimer: This project is unmaintined and was simply a learning experience on building a lexer/parser.
There are many better options to use than Dust.js for templating!
A custom abstract syntax tree for Dust.js.
npm install --save dustjs-ast
import dust from "dustjs-ast";
dust.parse("<h1>{hello}, {world}!</h1>"); //=> {type: "Template", ...}
dust.print(ast); //=> <h1>{hello}, {world}!</h1>
dust.traverse(ast, {
Section (path) {
// on enter
},
Partial: {
enter (path) {
// on enter
},
exit (path) {
// on exit
}
}
});
import {types as t} from "dustjs-ast";
t.isSection(node)
t.isBlock(node)
t.isReference(node)
...
Block ⇒ Node
A Block is defined as a Node with a body (e.g. children).
Property | Type | Description |
---|---|---|
body | Array<Node> |
Buffer ⇒ Node
Property | Type | Description |
---|---|---|
value | String |
Comment ⇒ Node
Property | Type | Description |
---|---|---|
value | String | {!___hello, world!___!} |
Format ⇒ Node
Property | Type | Description |
---|---|---|
value | String |
Identifier ⇒ Node
Property | Type | Description |
---|---|---|
path | Array<Key> | |
localContext | Boolean |
Inline ⇒ Block
Key ⇒ Node
Property | Type | Description |
---|---|---|
name | String |
NamedBlock ⇒ Block
Property | Type | Description |
---|---|---|
key | Key |
Property | Type | Description |
---|---|---|
type | String | |
loc.start.line | Number | |
loc.start.column | Number | |
loc.end.line | Number | |
loc.end.column | Number |
NumericLiteral ⇒ Node
Property | Type | Description |
---|---|---|
value | Number |
Param ⇒ Node
Property | Type | Description |
---|---|---|
key | Key | |
value | Identifier, Inline, NumericLiteral |
Partial ⇒ Node
A Partial is defined as matching a opening brace followed by a > plus anything that matches with key or inline plus context plus params followed by slash and closing brace.
Property | Type | Description |
---|---|---|
kind | String | |
value | Key, Inline | |
context | ?Identifier | |
params | Array<Param> | |
format.afterStart | ?Format | |
format.beforeParam | Array<Format> | |
format.beforeEnd | ?Format |
Raw ⇒ Node
Property | Type | Description |
---|---|---|
value | String | {`___hello, world!___`} |
Reference ⇒ Node
Property | Type | Description |
---|---|---|
id | Identifier | {reference} |
filters | Array<Key> | {reference|js|s} |
Section ⇒ Block
Note: A self-closed section will always have an empty body. Sections with start and end tags will always have at least one Block in the body.
Property | Type | Description |
---|---|---|
kind | String | {**#**section}{/section} |
id | Identifier | {#section}{/section} |
context | ?Identifier | {#section:one.two.three}{/section} |
params | Array<Param> | {#section param1=1 param2=2 param3=3} |
format.startTagAfterStart | ?Format | {#**___**section}{/section} |
format.startTagBeforeParam | Array<Format> | {#section**param1=1param2=2___**param3=3}{/section} |
format.startTagBeforeEnd | ?Format | {#section**}{/section} and {#section**/} |
format.endTagAfterStart | ?Format | {#section}{/**___**section} |
format.endTagBeforeEnd | ?Format | {#section}{/section___ } |
Special ⇒ Node
Property | Type | Description |
---|---|---|
key | Key |
StringLiteral ⇒ Node
Property | Type | Description |
---|---|---|
value | String |
Template ⇒ Block
Property | Type | Description |
---|---|---|
tokens | Array<Token> |
Property | Type | Description |
---|---|---|
type | String | |
value | String |