-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathindex.js
37 lines (33 loc) · 843 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
'use strict';
const fs = require('fs');
const parser = require('nunjucks/src/parser');
const ast = require('../ast');
const parseString = (str, opts) => {
if (opts && opts.trim === true) {
str = str.trim();
}
var node = ast.normalize(parser.parse(str));
if (opts && opts.verbose) {
node = ast.walk(node, n => {
n.parent = n.parent ? n.parent.type : null
});
} else if (opts && opts.clean === true) {
node = ast.clean(node);
}
return node;
};
const parseBuffer = (buffer, opts) => {
return parseString(buffer.toString(), opts);
};
const parseFile = (filename, opts, done) => {
fs.readFile(filename, (error, buffer) => {
return error
? done(error)
: done(null, parseBuffer(buffer, opts));
});
};
module.exports = {
string: parseString,
buffer: parseBuffer,
file: parseFile,
};