-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
47 lines (40 loc) · 1.43 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
'use strict';
var crypto = require('crypto');
module.exports = dssVariableParser;
/**
* Get parser to extract "@variable {name} - {description}"
*
* @param {boolean} [strict=true] - ignore definitions of undefined variables
* @return {function} A DSS parser
*/
function dssVariableParser(strict) {
var fileVariables = {},
fileVariablesRx = /^[\$|@]([a-zA-Z0-9_-]+):([^\;]+)\;/gim,
lineSplitRx = /(\s(-\s)?)/,
variables = {},
match, hash, tokens, name;
if (typeof strict === 'undefined') {
strict = true;
}
return function(i, line, block, css) {
// Extract all defined variables in this CSS file (once per file)
hash = crypto.createHash('md5').update(css).digest('hex');
if (!fileVariables[hash]) {
while ((match = fileVariablesRx.exec(css)) !== null) {
variables[match[1].trim()] = match[2].trim();
}
fileVariables[hash] = variables;
}
// Extract variable name and description from comment block
tokens = line.split(lineSplitRx, 2);
name = tokens[0].trim();
if (variables.hasOwnProperty(name) || !strict) {
return {
name: name,
// Description is line with variable name and any delimiter replaced
description: line.replace(tokens.join(''), ''),
value: variables[name]
};
}
};
}