-
Notifications
You must be signed in to change notification settings - Fork 31
/
parser.js
43 lines (35 loc) · 1.03 KB
/
parser.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
38
39
40
41
42
43
'use strict';
var util = require('util');
var Transform = require('readable-stream/transform');
var Block = require('./block');
var common = require('./common');
/**
* 1 - newline
* 2 - indentation
* 3 - begin tag
* 4 - task name
* 5 - original content
* 6 - end tag
*
* @type {RegExp}
*/
var regex = /(\n?)([ \t]*)(<!--\s*build:(\w+(?:-\w+)*)\s*-->)\n?([\s\S]*?)\n?(<!--\s*endbuild\s*-->)\n?/ig;
function Parser(tasks, config, file) {
Transform.call(this);
this.tasks = tasks;
this.config = config;
this.file = file;
}
util.inherits(Parser, Transform);
Parser.prototype._transform = function (chunk, enc, done) {
var content = chunk.toString('utf8');
var matches = common.regexMatchAll(content, regex);
matches.forEach(function (match) {
var block = new Block(this.config, this.file, match);
content = content.replace(block.replacement, function () {
return block.compile(this.tasks)
}.bind(this));
}.bind(this));
done(null, content);
};
module.exports = Parser;