-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
412 lines (357 loc) · 12.9 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
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
const path = require('path');
const fs = require('fs');
const { createGitReader } = require('@discoveryjs/scan-git');
const CSSWG_PATH = path.resolve('./csswg-drafts');
const CSSWG_BRANCH = 'main';
// const knownProperties = new Set(require('./real-web-css/scripts/usage/Declaration.json').valid);
const ignoreDirs = new Set([
'indexes',
'css-module-bikeshed'
]);
const blockStartRx = /^(\s*)<(\S+)\s+class=(?:'([^']+)'|"([^"]+)"|(\S+))>/i;
const blockEndRx = /^\s*<\/(\S+?)>/;
const prodValueEndEx = /<(dfn|dl|dd)|<\/(pre|div|dt)|\)<\/span|\n\n/g;
const specs = [];
const defs = [];
const prods = [];
const idls = [];
const propWriters = {
default: function(dict, key, value) {
if (key in dict === false) {
dict[key] = value;
} else {
dict[key] = dict[key] ? dict[key] + '\n' + value : value;
}
},
array: function(dict, key, value) {
if (key in dict === false) {
dict[key] = value ? [value] : [];
} else {
dict[key].push(value);
}
},
commaSeparatedArray: function(dict, key, value) {
value.trim().split(/\s*,\s*/).forEach(value =>
propWriters.array(dict, key, value)
);
}
};
const entryKeyType = {
spec: {
editor: propWriters.array,
formerEditor: propWriters.array,
'!contributors': propWriters.array,
previousVersion: propWriters.array,
ignoredTerms: propWriters.commaSeparatedArray,
ignoredVars: propWriters.commaSeparatedArray,
linkDefaults: propWriters.commaSeparatedArray,
issueTracking: propWriters.array,
canIUseUrl: propWriters.array,
ignoreCanIUseUrlFailure: propWriters.array,
'!changeLog': propWriters.array
}
};
function writePropValue(dict, key, type, value) {
const propWriter = (entryKeyType[type] && entryKeyType[type][key]) || propWriters.default;
propWriter(dict, key, value);
}
function processTextBlock(lines, type) {
const props = {};
const keyValueRx = /^\s*(\S.+?):\s*(.*)/;
let prevProp = '';
for (let i = 0; i < lines.length; i++) {
const line = lines[i].trimRight();
const keyValueMatch = line.match(keyValueRx);
if (keyValueMatch && !/https?$/.test(keyValueMatch[1])) {
const key = keyValueMatch[1]
.toLowerCase()
.replace(/ /gi, ' ') // Applies to
.replace(/<.+?>/g, '') // <a href="#values">Value</dfn>
.replace(/\s+(\S)/g, (m, ch) => ch.toUpperCase());
let value = keyValueMatch[2];
writePropValue(props, key, type, value);
prevProp = key;
} else {
if (prevProp) {
writePropValue(props, prevProp, type, line.trim());
} else {
console.warn('[WTF]!: (line ' + i + ')', line)
}
}
}
return props;
}
function processTableBlock(lines, type) {
return processTextBlock(
lines
.join('\n')
.replace(/([ \t]+)<tr>\n\s*<t[hd]>(.+?)\n\s*<td>\s*/g, '$1$2 ')
.replace(/<\/t[rdh]>/g, '')
.replace(/\s*<tbody> *\n*|\s*<\/tbody>\s*/g, '')
.replace(/^\s*<(\S+)>(.+?:)<\/\1>/m, '$2')
.replace(/(\n *)+/g, '$1')
.split(/\n+/),
type
);
}
function cleanupValue(value) {
return value
// FIXME?
.replace(/<!--(.|\s)+?-->/g, '')
.replace(/<\/?(nobr|var|code|br|em|i)>(?!>)/g, '')
.replace(/<var .+?>/g, '')
.replace(/<span.*?>(?!>)|<\/span>/g, '')
// FIXME: 1 entry
.replace(/ ?/g, ' ')
// FIXME: 8 entry
.replace(/<?/g, '<')
.replace(/>?/g, '>')
// FIXME: 1 entry
.replace(/&?/g, '&')
// ok
.replace(/∞?|Infinity/g, '∞')
.replace(/<\/?a>/g, '')
.replace(/''(?:[^/]+\/)?(\S+?)''/g, '$1')
.replace(/<<(.+?)>>/g, '<$1>');
}
function cleanupPropValue(dict, prop) {
if (prop in dict) {
dict[prop] = cleanupValue(dict[prop]);
}
}
function normalizeOffset(text){
if (text.indexOf('\n') === -1) {
return text;
}
text = text
.replace(/\t/g, ' ')
// cut first empty lines
.replace(/^(?:\s*[\n])+?( *)/, '$1')
.trimRight();
// fix empty strings
text = text.replace(/\n +\n/g, '\n\n');
// normalize text offset
var minOffset = 1000;
var lines = text.split(/\n+/);
for (var i = 1; i < lines.length; i++) {
var m = lines[i].match(/^\s*/);
if (m[0].length < minOffset) {
minOffset = m[0].length;
}
if (minOffset == 0) {
break;
}
}
if (minOffset > 0) {
text = text.replace(new RegExp('(^|\\n) {' + minOffset + '}', 'g'), '$1');
}
return text;
}
const blocks = {
'metadata': 'spec',
'propdef': 'prop',
'propdef-shorthand': 'prop',
'propdef-partial': 'prop-partial',
'descdef-mq': 'media-query',
'descdef': 'descriptor',
'idl': 'idl'
};
function processBs(fn) {
const relfn = path.relative(CSSWG_PATH, fn);
const specEntry = {
file: relfn,
id: path.dirname(relfn),
props: {
title: path.dirname(relfn)
}
};
// if (relfn !== 'css-backgrounds-4/Overview.bs') {
if (relfn !== 'css-fonts-3/Fonts.src.html') {
// uncomment to process a single file
// return;
}
specs.push(specEntry);
let content = fs.readFileSync(fn, 'utf8');
let offset = 0;
const linesOffset = [];
const lines = content.split(/(\r\n?|\n)/).filter((part, idx) => {
if (idx % 2 === 0) {
linesOffset.push(offset);
}
offset += part.length;
return idx % 2 === 0;
});
for (let i = 0; i < lines.length; i++) {
const [, title] = lines[i].match(/<title>(.+)<\/title>/) || [];
if (title) {
specEntry.props.title = title;
continue;
}
// NOTE: much better is to search productions in <pre class="prod"> blocks,
// however many specs do not wrap productions in such blocks; Suppose that's
// should be fixed. Until that use dirty and hacky approach.
const prodMatch = lines[i].match(/<dfn( .*?)?>(.+?)<\/dfn>\s*=/);
if (prodMatch) {
const normName = prodMatch[2]
.replace(/<\/?var>/g, '')
.replace(/</g, '<')
.replace(/>/g, '>');
// const [a, b] = [content.substr(linesOffset[i] + prodMatch.index, prodMatch[0].length), prodMatch[0]];
// a !== b && console.log(xcount, relfn, '[a:' + a + ']', '[b:' + b + ']');
if (/^@|[>)]$/.test(normName) || normName === 'content-list') {
let attrEntries = prodMatch[1] && prodMatch[1]
.match(/\s+[^=]+(?:=(?:"[^"]+"|'[^']+'|\S+))?/g);
const valueStart = linesOffset[i] + prodMatch.index + prodMatch[0].length;
let valueEnd;
prodValueEndEx.lastIndex = valueStart;
valueEnd = prodValueEndEx.exec(content).index;
if (content.charAt(valueEnd) === ')') {
valueEnd++;
}
prods.push({
type: 'prod',
name: normName.replace(/[<>]/g, ''),
dfnName: normName,
source: {
spec: path.dirname(relfn),
line: i + 1
},
attrs: attrEntries && attrEntries.reduce((res, attr) => {
const m = attr.trim().match(/([^=]+)(?:=(['"])(.+?)\2|=(\S+))?/);
res[m[1]] = m[3] || m[4] || true;
return res;
}, {}),
value: normalizeOffset(
cleanupValue(content.substring(valueStart, valueEnd).trim())
.replace(/<a (.|\s)+?>|<\/a>/g, '')
)
});
}
}
const blockStart = lines[i].match(blockStartRx);
if (blockStart) {
const [, blockStartOffset, el, cls1, cls2, cls3] = blockStart;
const blockStartEnd = blockStart.index + blockStart[0].length;
let type = (cls1 || cls2 || cls3).replace(/\s+/g, '-');
let singleLineBlock = false;
const blockStartContent = lines[i]
.slice(blockStart.index + blockStart[0].length)
.trim()
.replace(new RegExp('</' + el + '>$'), m => {
singleLineBlock = true;
return '';
});
if (!blocks.hasOwnProperty(type) || el === 'code') {
continue;
}
type = blocks[type];
const blockEnd = blockStartOffset + '</' + el + '>';
const blockLines = [];
let entry = Object.create(null);
if (type === 'spec') {
entry = specEntry;
} else {
entry.type = type;
entry.source = {
spec: path.dirname(relfn),
line: i + 1 // lines from 1
};
}
if (blockStartContent) {
blockLines.push(blockStartContent);
}
if (!singleLineBlock) {
for (i++; i < lines.length; i++) {
if (!lines[i]) {
continue;
}
const [blockEndMatch] = lines[i].match(blockEndRx) || [''];
if (blockEndMatch === blockEnd) {
break;
}
// FIXME: css-font-4
if (el === 'pre' && lines[i].indexOf('};</pre>') !== -1) {
blockLines.push('};');
break;
}
blockLines.push(lines[i]);
}
};
if (type === 'idl') {
entry.content = normalizeOffset(blockLines.join('\n').trim())
// FIXME
.replace(/<dfn.+?>|<\/dfn>/g, '')
.replace(/<!--(.|\s)+?-->/g, '');
idls.push(entry);
continue;
}
entry.props = el === 'table'
? processTableBlock(blockLines, type)
: processTextBlock(blockLines, type);
if (type === 'spec') {
if (!entry.props.title) {
entry.props.title = entry.id;
}
} else {
cleanupPropValue(entry.props, 'value');
cleanupPropValue(entry.props, 'newValues');
cleanupPropValue(entry.props, 'initial');
cleanupPropValue(entry.props, 'appliesTo');
cleanupPropValue(entry.props, 'computedValue');
cleanupPropValue(entry.props, 'animationType');
cleanupPropValue(entry.props, 'animatable');
cleanupPropValue(entry.props, 'percentages');
(entry.props.name || 'unknown').replace(/<[^>]+>/g, '').split(/\s*,\s*/).map(name => {
defs.push({
...entry,
name,
props: { ...entry.props, name }
});
});
}
}
}
}
module.exports = async function generateData() {
fs.readdirSync(CSSWG_PATH).forEach(function(p) {
const fpath = path.join(CSSWG_PATH, p);
if (fs.statSync(fpath).isDirectory()) {
let fn;
if (ignoreDirs.has(p)) {
return;
}
if (fs.existsSync(fn = path.join(fpath, 'Overview.bs'))) {
processBs(fn);
} else if (
fs.existsSync(fn = path.join(fpath, 'Overview.src.html')) ||
fs.existsSync(fn = path.join(fpath, 'Fonts.src.html'))
) {
processBs(fn);
// console.log('HTML:', fn);
// TODO: process html files too
} else {
console.log('Skip:', fpath);
// console.log('SKIP:', fpath);
}
}
});
const gitReader = await createGitReader(path.join(CSSWG_PATH, '.git'));
const commit = await gitReader.readCommit(CSSWG_BRANCH);
return {
source: {
home: 'https://github.com/w3c/csswg-drafts/',
commit: commit.oid,
commitShort: commit.oid.slice(0, 10),
commitDate: new Date(commit.author.timestamp * 1000).toISOString(),
checkDate: new Date().toISOString(),
branch: 'main'
},
specs,
defs,
prods,
idls
};
}
if (process.mainModule === module) {
console.log(generateData());
}