README.md
1# pug-parser
2
3The pug parser (takes an array of tokens and converts it to an abstract syntax tree)
4
5[](https://travis-ci.org/pugjs/pug-parser)
6[](https://david-dm.org/pugjs/pug?path=packages/pug-parser)
7[](https://david-dm.org/pugjs/pug?path=packages/pug-parser&type=dev)
8[](https://www.npmjs.org/package/pug-parser)
9
10## Installation
11
12 npm install pug-parser
13
14## Usage
15
16```js
17var parse = require('pug-parser');
18```
19
20### `parse(tokens, options)`
21
22Convert Pug tokens to an abstract syntax tree (AST).
23
24`options` can contain the following properties:
25
26- `filename` (string): The name of the Pug file; it is included in the produced AST nodes and error handling, if provided.
27- `plugins` (array): An array of plugins, in the order they should be applied.
28- `src` (string): The source of the Pug file; it is used in error handling if provided.
29
30```js
31var lex = require('pug-lexer');
32
33var filename = 'my-file.pug';
34var src = 'div(data-foo="bar")';
35var tokens = lex(src, {filename});
36
37var ast = parse(tokens, {filename, src});
38
39console.log(JSON.stringify(ast, null, ' '))
40```
41
42```json
43{
44 "type": "Block",
45 "nodes": [
46 {
47 "type": "Tag",
48 "name": "div",
49 "selfClosing": false,
50 "block": {
51 "type": "Block",
52 "nodes": [],
53 "line": 1,
54 "filename": "my-file.pug"
55 },
56 "attrs": [
57 {
58 "name": "data-foo",
59 "val": "\"bar\"",
60 "line": 1,
61 "column": 5,
62 "filename": "my-file.pug",
63 "mustEscape": true
64 }
65 ],
66 "attributeBlocks": [],
67 "isInline": false,
68 "line": 1,
69 "column": 1,
70 "filename": "my-file.pug"
71 }
72 ],
73 "line": 0,
74 "filename": "my-file.pug"
75}
76```
77
78### `new parse.Parser(tokens, options)`
79
80Constructor for a Parser class. This is not meant to be used directly unless you know what you are doing.
81
82`options` may contain the following properties:
83
84- `filename` (string): The name of the Pug file; it is included in the produced AST nodes and error handling, if provided.
85- `plugins` (array): An array of plugins, in the order they should be applied.
86- `src` (string): The source of the Pug file; it is used in error handling if provided.
87
88## License
89
90 MIT
91