-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathgit-diff-to-ecmarkup.cjs
More file actions
executable file
·224 lines (213 loc) · 7.27 KB
/
Copy pathgit-diff-to-ecmarkup.cjs
File metadata and controls
executable file
·224 lines (213 loc) · 7.27 KB
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
#!/usr/bin/env node
/** Usage:
git diff origin/main..HEAD --numstat | sed 's/.*\t//' | while read f; do
t="$(mktemp -p "$(pwd)" "$f.XXXXXX")"
git diff origin/main..HEAD -U99999 --minimal --word-diff=plain -- "$f" | sed '1,/^@@/d' | ../proposal-intl-keep-trailing-zeros/git-diff-to-ecmarkup.cjs > "$t"
# chmod --reference="$f" "$t"
chmod a+r "$t"
mv -f "$t" "$f"
done
*/
const { createInterface } = require("node:readline");
const ecmaOpenPunc = '(["*|~`';
const ecmaClosePunc = ')]"*|~`';
const isPuncBalanced = str => {
const stack = [];
for (const ch of str) {
const i = ecmaOpenPunc.indexOf(ch);
if (ecmaClosePunc.includes(ch)) {
const x = stack.pop();
if (x === ch) continue;
if (i < 0) return false;
if (x !== undefined) stack.push(x);
}
if (i >= 0) stack.push(ecmaClosePunc[i]);
}
return stack.length === 0;
};
// https://html.spec.whatwg.org/multipage/syntax.html#void-elements
const voidElementNames = new Set(
"area, base, br, col, embed, hr, img, input, link, meta, source, track, wbr".split(
", ",
),
);
const tagsWellFormed = arr => {
const stack = [];
for (const tag of arr) {
if (tag.startsWith("</")) {
if (stack.pop() !== tag.slice(2)) return false;
} else {
stack.push(tag.slice(1));
}
}
return stack.length === 0;
};
const collapseTags = arr => {
for (let i = 1; i < arr.length; i++) {
if (arr[i].startsWith("</") && arr[i - 1] === `<${arr[i].slice(2)}`)
arr.splice(--i, 2);
}
return arr;
};
const makeTextPart = text => ({
kind: "",
text,
selfContained: true,
tags: [],
});
const fixupParts = parts => {
// Move a whitespace or emu-alg step prefix outside of ins/del text.
const prefixed =
parts[0]?.text.match(/^ *$/) && parts[1]?.text.match(/^( *(?:1\. ?)?)(.*)/);
if (prefixed) {
parts[0].text += prefixed[1];
parts[1].text = prefixed[2];
if (!parts[1].text) parts.splice(1, 1);
}
for (let i = parts.length - 1; i >= 1; i--) {
if (!parts[i].kind) continue;
let a = parts[i - 1].text;
let b = parts[i].text;
// Move a whitespace suffix to after ins/del text.
const wsSuffix = i + 1 < parts.length && b.match(/\s+$/)?.[0];
if (wsSuffix) {
if (!parts[i + 1].kind) {
parts[i + 1].text = `${wsSuffix}${parts[i + 1].text}`;
} else {
parts.splice(i + 1, 0, makeTextPart(wsSuffix));
}
b = parts[i].text = b.slice(0, -wsSuffix.length);
}
// Extract shared suffix punctuation from adjacent ins/del text.
if (!parts[i - 1].kind) continue;
let x = -1;
while (a.at(x) === b.at(x) && a.at(x).match(/\W/)) x--;
for (x++; x; x++) {
if (![a, b].every(s => isPuncBalanced(s.slice(0, x)))) continue;
const tail = a.slice(x);
if (i + 1 < parts.length && !parts[i + 1].kind) {
parts[i + 1].text = `${tail}${parts[i + 1].text}`;
} else {
parts.splice(i + 1, 0, makeTextPart(tail));
}
a = parts[i - 1].text = a.slice(0, x);
b = parts[i].text = b.slice(0, x);
break;
}
// Extract a shared word-like prefix from adjacent ins/del text.
const head = a.match(/^\w+/)?.[0];
if (head && b.match(/^\w+/)?.[0] === head) {
if (i - 2 >= 0) {
parts[i - 2].text += head;
} else {
parts.unshift(makeTextPart(head));
i++;
}
for (const j of [i, i - 1]) {
const newText = parts[j].text.slice(head.length);
if (newText) {
parts[j].text = newText;
} else {
parts.splice(j, 1);
}
}
}
}
};
(async () => {
let delTags = [];
let delLines = [];
let insTags = [];
let insLines = [];
const kindFrom2Chars = new Map(Object.entries({ "[-": "del", "{+": "ins" }));
for await (const line of createInterface({ input: process.stdin })) {
const parts = line
// Split on `git diff --word-diff=plain` markers.
.split(/(\[-.*?-\]|\{\+.*?\+\})/g)
// Remove empty text, except at start of line.
.filter((x, i) => x || i === 0)
.map(strPart => {
const kind = kindFrom2Chars.get(strPart.slice(0, 2)) || "";
const text = kind ? strPart.slice(2, -2) : strPart;
const tags =
strPart
// https://html.spec.whatwg.org/multipage/parsing.html#tag-open-state
.match(/<[/]?[a-z][^\t\n\f >]*/g)
?.filter(tag => !voidElementNames.has(tag.replace(/^<\/*/, ""))) ||
[];
const selfContained = tagsWellFormed(tags);
return { kind, text, selfContained, tags };
});
const delLine = parts
.map(part => (part.kind === "ins" ? "" : part.text))
.join("");
const insLine = parts
.map(part => (part.kind === "del" ? "" : part.text))
.join("");
const byLine = parts.some(part => part.kind && !part.selfContained);
const isDel = parts.some(part => part.kind === "del");
const isIns = parts.some(part => part.kind === "ins");
// Ecmarkup rejects operation parameter name changes, so detect those
// for line-level ins/del.
const isOpParam =
parts.some(part => part.kind) &&
[delLine, insLine].every(L => /^ *(\w+ )*_\w+_: .*,$/.test(L)) &&
!parts[0].kind &&
!parts[0].text.includes(":");
if (byLine || isOpParam || delLines.length || insLines.length) {
// When possible, emit a single pair of ins/del lines.
const unbuffered =
isOpParam || [delLine, insLine].every(L => !L || /^ *1\. /.test(L));
if (unbuffered && !delLines.length && !insLines.length) {
const insDelLines = { del: delLine, ins: insLine };
for (const [tag, line] of Object.entries(insDelLines)) {
const taggedLine = line.replace(/\S.*/, s => {
const i = s.startsWith("1. ") ? 3 : 0;
return `${s.slice(0, i)}<${tag}>${s.slice(i).replace(/ {2,}/g, " ")}</${tag}>`;
});
if (line) console.log(taggedLine);
}
continue;
}
// Otherwise buffer the lines, but pure deletions ["", del:"..."] skip the
// insert buffer and pure inserts ["", ins:"..."] skip the delete buffer.
if (delLine || parts.length < 2) delLines.push(delLine);
if (insLine || parts.length < 2) insLines.push(insLine);
// Flush the buffer once the tag structure is well-formed.
delTags = collapseTags([
...delTags,
...parts.flatMap(p => (p.kind === "del" && p.tags) || []),
]);
insTags = collapseTags([
...insTags,
...parts.flatMap(p => (p.kind === "ins" && p.tags) || []),
]);
if (!delTags.length && !insTags.length) {
// Longest indentation wins.
const indentation = [delLines, insLines]
.map(lines => lines[0]?.match(/^\s*/)?.[0] || "")
.sort((a, b) => b.length - a.length)[0];
for (const L of [
`${indentation}<del class="block">`,
...delLines,
`${indentation}</del>`,
`${indentation}<ins class="block">`,
...insLines,
`${indentation}</ins>`,
]) {
console.log(L);
}
delLines = [];
insLines = [];
}
continue;
}
// Any ins/del content is confined to this single line.
fixupParts(parts);
console.log(
parts
.map(({ kind, text }) => (!kind ? text : `<${kind}>${text}</${kind}>`))
.join(""),
);
}
})();