-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
71 lines (64 loc) · 1.78 KB
/
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
'use strict';
/**
* Adds a `.capture` method to a [snapdragon][] `Parser` instance. Wraps
* the `.set` method to simplify the interface for creating parsers.
*
* ```js
* var Snapdragon = require('snapdragon');
* var capture = require('snapdragon-capture');
* var parser = new Snapdragon.Parser();
* parser.use(capture());
* ```
*
* @api public
*/
module.exports = function(options) {
return function(snapdragon) {
if (snapdragon.isSnapdragon) {
snapdragon.parser.define('capture', capture);
snapdragon.define('capture', function() {
return this.parser.capture.apply(this.parser, arguments);
});
} else if (snapdragon.isParser) {
snapdragon.define('capture', capture);
} else {
throw new Error('expected an instance of snapdragon or snapdragon.parser');
}
};
};
/**
* Create a node of the given `type` using the specified regex or function.
*
* ```js
* parser
* .capture('slash', /^\//)
* .capture('comma', /^,/)
* .capture('foo', function() {
* var pos = this.position();
* var match = this.match(/^\./);
* if (match) {
* return pos(this.node(match[0]));
* }
* });
* ```
* @param {String} `type`
* @param {RegExp|Function} `regex` Pass the regex to use for capturing. Pass a function if you need access to the parser instance.
* @return {Object} Returns the parser instance for chaining
* @api public
*/
function capture(type, regex) {
if (typeof regex === 'function') {
return this.set.apply(this, arguments);
}
this.regex.set(type, regex);
this.set(type, function() {
var pos = this.position();
var match = this.match(regex);
if (match) {
var node = pos(this.node(match[0], type));
node.match = match;
return node;
}
}.bind(this));
return this;
}