-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpatch-errata.js
executable file
·236 lines (198 loc) · 8.54 KB
/
patch-errata.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
#!/usr/local/bin/node
// vi:syntax=javascript
/* **********************************************************************
MIT License
Copyright (c) 2018 Adam Roach
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
********************************************************************** */
const fetch = require('node-fetch');
const parse = require('node-html-parser').parse;
const fs = require('fs');
const escape = require('escape-html');
let strayErrata = [];
const rfcNum = process.argv[2];
console.log("Processing errata for RFC " + rfcNum);
(async function main() {
try {
const rfcUrl = 'https://www.ietf.org/rfc/rfc' + rfcNum + '.txt';
const errataUrl = 'https://www.rfc-editor.org/errata/rfc' + rfcNum;
const outFile = "out/rfc" + rfcNum + ".html";
const errataHtml = await (await fetch(errataUrl)).text();
const errata = parseErrata(errataHtml);
let rfcText = await (await fetch(rfcUrl)).text();
rfcText = "<pre>\n" + escape(rfcText) + "</pre>";
errata.sort(function(a, b){return a['Errata ID'] - b['Errata ID']});
errata.forEach(erratum => {
if (['Verified', 'Held for Document Update'].
includes(erratum.Status)) {
rfcText = patch(rfcText, erratum);
}
});
if (strayErrata.length > 0) {
rfcText = "<div style='" +
"border:dashed;" +
"margin:8px;"+
"padding:24px;"+
"background-color:#FFFFAF;" +
"'>"+
"<b><i>This document has been updated by the " +
"following errata, which cannot be shown in-line " +
"in the document. Please see the end of this file "+
"for additional details: " +
strayErrata.join(', ') + "</i></b></div>" + rfcText;
}
fs.writeFile(outFile, rfcText, err => {if (err) throw err});
} catch(e) {
console.log("Exception: " + e);
}
})();
function parseErrata(html) {
const diff =
/^Section (.+?) says:(.*?)\nIt should say:(.*?)(\nNotes:(.*))?$/s;
const root = parse(html, {pre: true});
const content = root.querySelector("div.singlepost.entry");
let record = {body:''};
let records = [];
content.childNodes.forEach(e => {
if (e.tagName == 'p' && e.firstChild.rawText.includes("Errata ID")) {
e.structuredText.split("\n").forEach(line => {
let tag, value;
[tag, value] = line.split(": ");
record[tag] = value;
})
}
else if (e.tagName == 'div' && e.classNames.includes('special_hr')) {
// End of record marker -- extract diff information, if possible
const match = diff.exec(record.body);
if (match) {
record.section = match[1];
record.oldText = match[2];
record.newText = match[3];
record.notes = match[5];
} else {
const section = /section (\d[\d\.]*)/im.exec(record.body);
if (section) {
record.section = section[1];
}
}
records.push(record);
record = {body:''};
}
// If we've already read the start of the record and haven't hit
// the end, add any text we encounter to the body of the errata
else if (record.hasOwnProperty("Errata ID") && e.structuredText) {
record.body += e.rawText + "\n";
}
});
return records;
}
function patch(text, e) {
// If we can find the old text, replace it.
if (e.hasOwnProperty("oldText")) {
const locator = new RegExp('(' + e.oldText.
replace(/([^a-zA-Z0-9\s])/g, "\\$1").
replace(/\s+/g,"\\s+") + ')');
const match = locator.exec(text);
if (match) {
let annotation = "\n<font color='red'><s>" + match[1] +
"</s></font>" +
"</pre>" +
"<div style='" +
"overflow:auto;" +
"border:dashed;" +
"margin:8px;"+
"padding:24px;"+
"background-color:#FFFFAF;" +
"'>"+
"<b><i>The preceding text has been updated by "+
e.Type +
" <a href='https://www.rfc-editor.org/errata/eid" +
e['Errata ID'] + "'>Errata " + e['Errata ID'] +
"</a> to instead read:</i></b>"+
"<pre style='margin:20px'>" +
e.newText +
"</pre>";
if (e.notes) {
annotation += "<i><b>Errata Notes: </b>" + e.notes + "</i>";
}
annotation += "</div><pre>\n";
console.log(" Patching errata " + e["Errata ID"] + " in-line");
return text.replace(locator, annotation);
}
}
// If we know which section the errata belongs to, then place it
// at the top of that section
if (e.hasOwnProperty('section')) {
const section = e.section.replace('.','\\.');
const locator = new RegExp("(\n" + section + "[\. ].*?\n)");
const match = locator.exec(text);
if (match) {
console.log(" Tagging errata " + e["Errata ID"] +
" at start of section " + e.section);
let annotation = "\n</pre>" +
"<div style='" +
"overflow:auto;" +
"border:dashed;" +
"margin:8px;"+
"padding:24px;"+
"background-color:#FFFFAF;" +
"'>"+
"<b><i>" + e.Type + " " +
"<a href='https://www.rfc-editor.org/errata/eid" +
e['Errata ID'] + "'>Errata " + e['Errata ID'] +
"</a> updates this section as follows:</i></b><br/>";
if (e.oldText && e.newText) {
annotation += "<br/><i><b>Old Text:</b></i><br/>" +
"<pre style='margin:20px'>" +
e.oldText + "</pre>";
annotation += "<br/><i><b>New Text:</b></i><br/>" +
"<pre style='margin:20px'>" +
e.newText + "</pre>";
} else {
annotation += "<pre style='margin:20px'>" + e.body + "</pre>";
}
if (e.notes) {
annotation += "<i><b>Errata Notes: </b>" + e.notes + "</i>";
}
annotation += "</div><pre>\n";
return text.replace(locator, "$1" + annotation);
}
}
// In the worst case, append the errata to the end of the document
// and point to it from the very top.
console.log(" Could not match " + e["Errata ID"] +
" -- placing at end of document." );
let annotation = "\n</pre>" +
"<div style='" +
"overflow:auto;" +
"border:dashed;" +
"margin:8px;"+
"padding:24px;"+
"background-color:#FFFFAF;" +
"'>"+
"<b><i>" + e.Type + " " +
"<a href='https://www.rfc-editor.org/errata/eid" +
e['Errata ID'] + "'>Errata " + e['Errata ID'] +
"</a> updates this document as follows:</i></b><br/>";
annotation += "<pre style='margin:20px'>" + e.body + "</pre>";
annotation += "</div><pre>\n";
strayErrata.push(e.Type +
" <a href='https://www.rfc-editor.org/errata/eid" +
e['Errata ID'] + "'>Errata " + e['Errata ID'] +
"</a>");
return text + annotation;
}