forked from highlightjs/highlight.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutility.js
169 lines (138 loc) · 4.29 KB
/
utility.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
'use strict';
var _ = require('lodash');
var bluebird = require('bluebird');
var glob = bluebird.promisify(require('glob'));
var path = require('path');
var Queue = require('gear').Queue;
var REPLACES,
regex = {},
headerRegex = /^\s*\/\*((.|\r?\n)*?)\*/;
REPLACES = {
'case_insensitive': 'cI',
'lexemes': 'l',
'contains': 'c',
'keywords': 'k',
'subLanguage': 'sL',
'className': 'cN',
'begin': 'b',
'beginKeywords': 'bK',
'end': 'e',
'endsWithParent': 'eW',
'illegal': 'i',
'excludeBegin': 'eB',
'excludeEnd': 'eE',
'returnBegin': 'rB',
'returnEnd': 'rE',
'relevance': 'r',
'variants': 'v',
'IDENT_RE': 'IR',
'UNDERSCORE_IDENT_RE': 'UIR',
'NUMBER_RE': 'NR',
'C_NUMBER_RE': 'CNR',
'BINARY_NUMBER_RE': 'BNR',
'RE_STARTERS_RE': 'RSR',
'BACKSLASH_ESCAPE': 'BE',
'APOS_STRING_MODE': 'ASM',
'QUOTE_STRING_MODE': 'QSM',
'PHRASAL_WORDS_MODE': 'PWM',
'C_LINE_COMMENT_MODE': 'CLCM',
'C_BLOCK_COMMENT_MODE': 'CBCM',
'HASH_COMMENT_MODE': 'HCM',
'NUMBER_MODE': 'NM',
'C_NUMBER_MODE': 'CNM',
'BINARY_NUMBER_MODE': 'BNM',
'CSS_NUMBER_MODE': 'CSSNM',
'REGEXP_MODE': 'RM',
'TITLE_MODE': 'TM',
'UNDERSCORE_TITLE_MODE': 'UTM',
'COMMENT': 'C',
'beginRe': 'bR',
'endRe': 'eR',
'illegalRe': 'iR',
'lexemesRe': 'lR',
'terminators': 't',
'terminator_end': 'tE'
};
regex.replaces = new RegExp(
`\\b(${Object.keys(REPLACES).join('|')})\\b`, 'g');
regex.classname = /(block|parentNode)\.cN/g;
regex.header = /^\s*(\/\*((.|\r?\n)*?)\*\/)?\s*/;
function replace(from, to) {
return { regex: from, replace: to };
}
function replaceClassNames(match) {
return REPLACES[match];
}
// All meta data, for each language definition, it store within the headers
// of each file in `src/languages`. `parseHeader` extracts that data and
// turns it into a useful object -- mainly for categories and what language
// this definition requires.
function parseHeader(content) {
var headers,
match = content.match(headerRegex);
if (!match) {
return null;
}
headers = _.compact(match[1].split('\n'));
return _.foldl(headers, function(result, header) {
var keyVal = header.trim().split(': '),
key = keyVal[0],
value = keyVal[1] || '';
if(key !== 'Description' && key !== 'Language') {
value = value.split(/\s*,\s*/);
}
result[key] = value;
return result;
}, {});
}
function filterByQualifiers(blob, languages, categories) {
if(_.isEmpty(languages) && _.isEmpty(categories)) return true;
var language = path.basename(blob.name, '.js'),
fileInfo = parseHeader(blob.result),
fileCategories = fileInfo.Category || [],
containsCategory = _.partial(_.contains, categories);
if(!fileInfo) return false;
return _.contains(languages, language) ||
_.any(fileCategories, containsCategory);
}
// For the filter task in `tools/tasks.js`, this function will look for
// categories and languages specificed from the CLI.
function buildFilterCallback(qualifiers) {
var result = _.partition(qualifiers, { 0: ':' }),
languages = result[1],
categories = _.map(result[0], category => category.slice(1));
return blob => filterByQualifiers(blob, languages, categories);
}
function globDefaults(pattern, encoding) {
encoding = encoding || 'utf8';
// The limit option is a fix for issue #636 when the build script would
// EMFILE error for those systems who had a limit of open files per
// process.
//
// <https://github.com/isagalaev/highlight.js/issues/636>
return { pattern: pattern, limit: 50, encoding: encoding };
}
function getStyleNames() {
var stylesDir = 'src/styles/',
options = { ignore: `${stylesDir}default.css` };
return glob(`${stylesDir}*.css`, options)
.map(function(style) {
var basename = path.basename(style, '.css'),
name = _.startCase(basename),
pathName = path.relative('src', style);
return { path: pathName, name: name };
});
}
function toQueue(tasks, registry) {
return _.map(tasks, task => new Queue({ registry }).tasks(task));
}
module.exports = {
buildFilterCallback: buildFilterCallback,
getStyleNames: getStyleNames,
glob: globDefaults,
parseHeader: parseHeader,
regex: regex,
replace: replace,
replaceClassNames: replaceClassNames,
toQueue: toQueue
};