-
Notifications
You must be signed in to change notification settings - Fork 161
/
ecmarkupify.js
67 lines (53 loc) · 1.9 KB
/
ecmarkupify.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
// Usage: traceur-runner ecmarkupify.js input.html output.html
const fs = require('fs');
const jsdom = require('jsdom');
const EmuSpec = require('ecmarkup/lib/Spec');
export default ecmarkupify;
// Can't use usual trick of module.parent === null because we intend to be run via traceur-runner
if (module.parent == require.main) {
ecmarkupify(process.argv[2], process.argv[3]);
}
function ecmarkupify(inputFile, outputFile) {
const inputText = fs.readFileSync(inputFile, { encoding: 'utf-8' });
const doc = jsdom.jsdom(inputText);
hackEmuAlgElements(doc);
const spec = new EmuSpec(inputFile, fetch, doc);
return Promise.all([
spec.loadES6Biblio(),
spec.loadBiblios()
])
.then(() => {
addAllAOIDsToBiblio(spec);
spec.buildAlgs();
fs.writeFileSync(outputFile, jsdom.serializeDocument(spec.doc));
});
}
function fetch(path) {
return new Promise((resolve, reject) => {
fs.readFile(path, { encoding: 'utf-8' }, (err, contents) => {
if (err) {
reject(err);
}
resolve(contents);
});
});
}
function hackEmuAlgElements(doc) {
// Work around https://github.com/tabatkins/bikeshed/issues/380 by authoring the .bs file with <pre is="emu-alg">
// (i.e. `doc` will contain <pre is="emu-alg">), which we here convert to real <emu-alg> elements.
const algs = Array.from(doc.querySelectorAll('pre[is="emu-alg"]'));
for (const preAlg of algs) {
const realAlg = doc.createElement('emu-alg');
realAlg.innerHTML = preAlg.innerHTML;
preAlg.parentNode.replaceChild(realAlg, preAlg);
}
}
function addAllAOIDsToBiblio(spec) {
// Allow aoid="" anywhere. Ecmarkup's default configuration only scans for them when building, and even then only on
// <emu-clause> and <emu-alg>.
const aos = Array.from(spec.doc.querySelectorAll('[aoid]'));
for (const ao of aos) {
const aoid = ao.getAttribute('aoid');
spec.biblio.ops[aoid] = '#' + ao.id;
}
}