-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathupdate-manuscript-from-tests.js
149 lines (129 loc) · 4.29 KB
/
update-manuscript-from-tests.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
const fs = require("fs");
const path = require("path");
const dom = require("htmldom2").DOMParser;
const xpath = require("xpath");
const serializer = require("htmldom2").XMLSerializer;
const axios = require("axios");
const staticPath = (filename) => {
return path.join(__dirname, "static", filename);
};
const scriptsPath = (filename) => {
return path.join(__dirname, "scripts", filename);
};
const mapsPath = (filename) => {
return path.join(__dirname, "..", "JM", "jurism", "juris-abbrevs", filename);
};
const immediatePath = (filename) => {
return path.join(__dirname, filename);
};
const testFixturesPath = (filename) => {
return path.join(__dirname, "..", "JM", "citeproc-js", "fixtures", "styles", "jm-indigobook-law-review", filename);
};
const getResult = (txt) => {
var lst = txt.split("\n");
var startRex = /\>==+\s+RESULT/;
var endRex = /\<==+\s+RESULT/;
var buf = [];
var inRegion = false;
for (var line of lst) {
if (line.match(endRex)) {
inRegion = false;
}
if (inRegion === true) {
buf.push(line);
}
if (line.match(startRex)) {
inRegion = true;
}
}
var result = buf.join("\n").trim();
return result;
}
const getNewResult = (key) => {
var fn = `style_${key}.txt`;
var txt = fs.readFileSync(testFixturesPath(fn)).toString();
var result = getResult(txt);
return result;
};
const injectNewResults = (doc) => {
var nodes = xpath.select('//span[contains(@class, "cite")]', doc);
for (var i=0,ilen=nodes.length; i<ilen; i++) {
var node = nodes[i];
var hasVarNodes = xpath.select(".//var", node).length > 0;
if (hasVarNodes) continue;
var key = node.getAttribute("id");
var result = getNewResult(key);
var frag = new dom().parseFromString(result, "text/html");
// Path here is html / body / actual nodes
var children = frag.childNodes[0].childNodes[0].childNodes;
while (node.childNodes.length) {
node.removeChild(node.childNodes[0]);
}
for (var j=0,jlen=children.length; j<jlen; j++) {
// Need to clone to avoid random lossage when moving across documents, apparently.
var cln = children[j].cloneNode(true);
node.appendChild(cln);
}
}
return doc;
}
const getTopMainTail = (html, label) => {
var linecount = 1;
var ret = {
top: [],
blurb: [],
moretop: [],
main: [],
tail: []
};
var state = "top";
var lst = html.split("\n");
for (var line of lst) {
if (line.trim() === "</main>") {
state = "tail";
}
if (line.trim() === "<!-- END BLURB -->") {
state = "moretop";
}
ret[state].push(line);
if (line.trim() === "<!-- START BLURB -->") {
state = "blurb";
}
if (line.trim() === "<main>") {
state = "main";
}
}
for (var key in ret) {
if (["top", "tail", "blurb", "moretop"].indexOf(key) > -1) {
ret[key] = ret[key].join("\n");
}
}
return ret;
}
const fixAndSave = async () => {
var xml = fs.readFileSync(staticPath("for-briefs-and-memoranda.html")).toString();
var doc = new dom().parseFromString(xml, "text/html");
doc = injectNewResults(doc);
var html = (new serializer()).serializeToString(doc, "text/html");
html = html.replace(/&/g, "&");
var origParts = getTopMainTail(xml, "orig");
var newParts = getTopMainTail(html, "new");
var newBlurb = fs.readFileSync(staticPath("blurb-law-reviews.html")).toString();
if (origParts.main.length !== newParts.main.length) {
console.log("OUCH: misalignment between orig and new line counts");
process.exit();
}
for (var i=0,ilen=origParts.main.length; i<ilen; i++) {
if (newParts.main[i].match(/class="[^"]*cite[^"]*"/)) {
origParts.main[i] = newParts.main[i];
}
}
origParts.main = origParts.main.join("\n");
html = [origParts.top, newBlurb, origParts.moretop, origParts.main, origParts.tail].join("\n");
fs.writeFileSync(staticPath("index.html"), html);
console.log("Done!");
}
const run = async () => {
var nodes = await fixAndSave();
}
run();