@iasiv5/dsh-skills 1.3.4 → 1.3.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/skills/answer-me-with-html/SKILL.md +3 -3
- package/skills/answer-me-with-html/references/video.md +1 -0
- package/skills/answer-me-with-html/scripts/am.mjs +1671 -1131
- package/skills/darwin-skill/README.md +136 -121
- package/skills/darwin-skill/README_EN.md +138 -123
- package/skills/darwin-skill/SKILL.md +256 -421
- package/skills/darwin-skill/_archive/hero-2.0.gif +0 -0
- package/skills/darwin-skill/assets/banner-en.svg +1 -1
- package/skills/darwin-skill/assets/banner.svg +2 -2
- package/skills/darwin-skill/assets/chart-loop-en.html +2 -2
- package/skills/darwin-skill/assets/chart-loop-en.png +0 -0
- package/skills/darwin-skill/assets/chart-loop.html +2 -2
- package/skills/darwin-skill/assets/chart-loop.png +0 -0
- package/skills/darwin-skill/assets/chart-phases-en.html +12 -12
- package/skills/darwin-skill/assets/chart-phases-en.png +0 -0
- package/skills/darwin-skill/assets/chart-phases.html +13 -13
- package/skills/darwin-skill/assets/chart-phases.png +0 -0
- package/skills/darwin-skill/assets/darwin-3.0-launch.mp4 +0 -0
- package/skills/darwin-skill/assets/hero.gif +0 -0
- package/skills/darwin-skill/assets/result-card-example.png +0 -0
- package/skills/darwin-skill/docs/index.html +93 -357
- package/skills/darwin-skill/references/about-author.md +101 -0
- package/skills/darwin-skill/references/eval-protocol.md +156 -0
- package/skills/darwin-skill/references/skilllens-evidence.md +2 -1
- package/skills/darwin-skill/references/v3-evidence.md +114 -0
- package/skills/darwin-skill/scripts/darwin.py +400 -0
- package/skills/darwin-skill/scripts/screenshot.mjs +3 -2
- package/skills/darwin-skill/showcase.html +93 -357
- package/skills/darwin-skill/templates/result-card.html +36 -20
- package/skills/darwin-skill/templates/result-card.png +0 -0
- package/skills/darwin-skill/test-prompts.json +36 -12
- package/skills/docx/SKILL.md +5 -4
- package/skills/docx/scripts/office/helpers/__init__.py +22 -4
- package/skills/docx/scripts/office/helpers/pptx_chart.py +124 -11
- package/skills/docx/scripts/office/helpers/pptx_slide.py +43 -3
- package/skills/docx/scripts/office/soffice.py +85 -1
- package/skills/docx/scripts/office/validate.py +50 -7
- package/skills/docx/scripts/office/validators/base.py +16 -4
- package/skills/docx/scripts/office/validators/docx.py +59 -6
- package/skills/docx/scripts/office/validators/pptx.py +116 -8
- package/skills/docx/scripts/office/validators/redlining.py +292 -113
- package/skills/impeccable/SKILL.md +4 -2
- package/skills/impeccable/reference/adapt.md +1 -1
- package/skills/impeccable/reference/animate.md +17 -0
- package/skills/impeccable/reference/critique-rubric.md +496 -0
- package/skills/impeccable/reference/critique.md +12 -504
- package/skills/impeccable/reference/degraded/documenter.md +1 -1
- package/skills/impeccable/reference/degraded/motion-reviewer.md +29 -0
- package/skills/impeccable/reference/document.md +3 -1
- package/skills/impeccable/reference/foundations.md +44 -0
- package/skills/impeccable/reference/hooks.md +3 -3
- package/skills/impeccable/reference/live-setup.md +1 -1
- package/skills/impeccable/reference/live.md +26 -12
- package/skills/impeccable/reference/new-work-comp.md +24 -0
- package/skills/impeccable/reference/new-work-world.md +20 -0
- package/skills/impeccable/reference/new-work.md +6 -34
- package/skills/impeccable/reference/typeset.md +6 -0
- package/skills/impeccable/reference/visualize.md +1 -1
- package/skills/impeccable/scripts/VERSION +1 -1
- package/skills/impeccable/scripts/engine-checksums.txt +5 -5
- package/skills/impeccable/scripts/live-browser.js +51 -22
- package/skills/pdf/{forms.md → FORMS.md} +1 -1
- package/skills/pdf/{reference.md → REFERENCE.md} +3 -2
- package/skills/xlsx/SKILL.md +19 -14
- package/skills/xlsx/scripts/office/helpers/__init__.py +22 -4
- package/skills/xlsx/scripts/office/helpers/pptx_chart.py +124 -11
- package/skills/xlsx/scripts/office/helpers/pptx_slide.py +43 -3
- package/skills/xlsx/scripts/office/soffice.py +85 -1
- package/skills/xlsx/scripts/office/validate.py +50 -7
- package/skills/xlsx/scripts/office/validators/base.py +16 -4
- package/skills/xlsx/scripts/office/validators/docx.py +59 -6
- package/skills/xlsx/scripts/office/validators/pptx.py +116 -8
- package/skills/xlsx/scripts/office/validators/redlining.py +292 -113
- package/skills/xlsx/scripts/recalc.py +731 -136
- package/skills-manifest.json +1 -1
- /package/skills/darwin-skill/{assets → _archive}/chart-ratchet-en.html +0 -0
- /package/skills/darwin-skill/{assets → _archive}/chart-ratchet-en.png +0 -0
- /package/skills/darwin-skill/{assets → _archive}/chart-ratchet.html +0 -0
- /package/skills/darwin-skill/{assets → _archive}/chart-ratchet.png +0 -0
- /package/skills/darwin-skill/{templates → _archive}/result-card-dark.html +0 -0
- /package/skills/darwin-skill/{templates → _archive}/result-card-white.html +0 -0
|
@@ -1,19 +1,24 @@
|
|
|
1
1
|
"""
|
|
2
2
|
Validator for tracked changes in Word documents.
|
|
3
3
|
|
|
4
|
-
Detects untracked edits in
|
|
4
|
+
Detects untracked edits in a Word document: text that differs from the
|
|
5
5
|
original without a <w:ins>/<w:del> wrapper recording it. The tracked changes
|
|
6
6
|
that are new relative to the original are undone, and the result is compared
|
|
7
7
|
against the original; whatever text still differs was edited without being
|
|
8
8
|
tracked.
|
|
9
9
|
|
|
10
|
-
|
|
11
|
-
|
|
10
|
+
The body (word/document.xml) is compared, and so is each header, footer,
|
|
11
|
+
footnotes and endnotes part, against the original's part of the same name.
|
|
12
|
+
Other parts, comments among them, are not checked. A page-number field
|
|
13
|
+
whose cached result is a plain number counts as its name in braces
|
|
14
|
+
("{PAGE}"), not as that number, which any application that repaginates the
|
|
15
|
+
document rewrites.
|
|
12
16
|
"""
|
|
13
17
|
|
|
14
|
-
import
|
|
18
|
+
import re
|
|
15
19
|
import tempfile
|
|
16
20
|
import zipfile
|
|
21
|
+
from difflib import SequenceMatcher
|
|
17
22
|
from pathlib import Path
|
|
18
23
|
|
|
19
24
|
import defusedxml.ElementTree as ET
|
|
@@ -22,12 +27,35 @@ from defusedxml.common import DefusedXmlException
|
|
|
22
27
|
from helpers import rendered_text, safe_extract
|
|
23
28
|
|
|
24
29
|
|
|
30
|
+
XML_ERRORS = (ET.ParseError, DefusedXmlException, LookupError, ValueError)
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
class _WorkSpent(Exception):
|
|
34
|
+
|
|
35
|
+
pass
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
class _Matcher(SequenceMatcher):
|
|
39
|
+
|
|
40
|
+
def __init__(self, a, b, allowance):
|
|
41
|
+
self.allowance = allowance
|
|
42
|
+
super().__init__(None, a, b, autojunk=False)
|
|
43
|
+
|
|
44
|
+
def find_longest_match(self, alo=0, ahi=None, blo=0, bhi=None):
|
|
45
|
+
items = self.a[alo:ahi]
|
|
46
|
+
self.allowance[0] -= len(items) + sum(len(self.b2j.get(x, ())) for x in items)
|
|
47
|
+
if self.allowance[0] < 0:
|
|
48
|
+
raise _WorkSpent
|
|
49
|
+
return super().find_longest_match(alo, ahi, blo, bhi)
|
|
50
|
+
|
|
51
|
+
|
|
25
52
|
class RedliningValidator:
|
|
26
53
|
|
|
27
54
|
def __init__(self, unpacked_dir, original_docx, verbose=False):
|
|
28
55
|
self.unpacked_dir = Path(unpacked_dir)
|
|
29
56
|
self.original_docx = Path(original_docx)
|
|
30
57
|
self.verbose = verbose
|
|
58
|
+
self._work_left = [self.WORK_ALLOWED]
|
|
31
59
|
self.namespaces = {
|
|
32
60
|
"w": "http://schemas.openxmlformats.org/wordprocessingml/2006/main"
|
|
33
61
|
}
|
|
@@ -58,39 +86,81 @@ class RedliningValidator:
|
|
|
58
86
|
)
|
|
59
87
|
return False
|
|
60
88
|
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
89
|
+
sides = (("original", temp_path), ("modified", self.unpacked_dir))
|
|
90
|
+
change_count, failures, differences = 0, [], []
|
|
91
|
+
for part in self._parts_to_compare(temp_path, self.unpacked_dir):
|
|
92
|
+
roots = []
|
|
93
|
+
for side, unpacked in sides:
|
|
94
|
+
try:
|
|
95
|
+
roots.append(self._parse_part(unpacked / part))
|
|
96
|
+
except XML_ERRORS as e:
|
|
97
|
+
failures.append(
|
|
98
|
+
f"FAILED - Error parsing {part} of the {side} document: {e}"
|
|
99
|
+
)
|
|
100
|
+
if len(roots) < len(sides):
|
|
101
|
+
continue
|
|
102
|
+
new_changes, original_text, modified_text = self._compare_part(*roots)
|
|
103
|
+
change_count += len(new_changes)
|
|
104
|
+
if modified_text != original_text:
|
|
105
|
+
differences.append((part, original_text, modified_text))
|
|
106
|
+
|
|
107
|
+
if differences:
|
|
108
|
+
failures.append(self._generate_detailed_diff(differences))
|
|
109
|
+
if failures:
|
|
110
|
+
print("\n".join(failures))
|
|
81
111
|
return False
|
|
82
112
|
|
|
83
113
|
if self.verbose:
|
|
84
114
|
print(
|
|
85
|
-
f"PASSED - All {
|
|
115
|
+
f"PASSED - All {change_count} change(s) against the original "
|
|
86
116
|
"are properly tracked"
|
|
87
117
|
)
|
|
88
118
|
return True
|
|
89
119
|
|
|
120
|
+
OTHER_PARTS = (
|
|
121
|
+
"word/header*.xml",
|
|
122
|
+
"word/footer*.xml",
|
|
123
|
+
"word/footnotes.xml",
|
|
124
|
+
"word/endnotes.xml",
|
|
125
|
+
)
|
|
126
|
+
|
|
127
|
+
def _parts_to_compare(self, original_dir, modified_dir):
|
|
128
|
+
others = set()
|
|
129
|
+
for unpacked in (Path(original_dir), Path(modified_dir)):
|
|
130
|
+
for pattern in self.OTHER_PARTS:
|
|
131
|
+
others.update(
|
|
132
|
+
path.relative_to(unpacked).as_posix()
|
|
133
|
+
for path in unpacked.glob(pattern)
|
|
134
|
+
)
|
|
135
|
+
return ["word/document.xml", *sorted(others)]
|
|
136
|
+
|
|
137
|
+
def _compare_part(self, original_root, modified_root):
|
|
138
|
+
for root in (original_root, modified_root):
|
|
139
|
+
self._flatten_text_elements(root)
|
|
140
|
+
new_changes = self._new_tracked_changes(original_root, modified_root)
|
|
141
|
+
self._remove_tracked_changes(modified_root, new_changes)
|
|
142
|
+
return (
|
|
143
|
+
new_changes,
|
|
144
|
+
self._extract_text_content(original_root),
|
|
145
|
+
self._extract_text_content(modified_root),
|
|
146
|
+
)
|
|
147
|
+
|
|
148
|
+
def _parse_part(self, path):
|
|
149
|
+
if not path.is_file():
|
|
150
|
+
return ET.fromstring("<absent/>")
|
|
151
|
+
return ET.parse(path).getroot()
|
|
152
|
+
|
|
153
|
+
def _flatten_text_elements(self, root):
|
|
154
|
+
for elem in self._text_elements(root):
|
|
155
|
+
if len(elem):
|
|
156
|
+
elem.text = "".join(elem.itertext())
|
|
157
|
+
for child in list(elem):
|
|
158
|
+
elem.remove(child)
|
|
159
|
+
|
|
90
160
|
def _tracked_change_elements(self, root):
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
return [elem for elem in root.iter() if elem.tag in
|
|
161
|
+
w = self.namespaces["w"]
|
|
162
|
+
tags = (f"{{{w}}}ins", f"{{{w}}}del", f"{{{w}}}moveFrom", f"{{{w}}}moveTo")
|
|
163
|
+
return [elem for elem in root.iter() if elem.tag in tags]
|
|
94
164
|
|
|
95
165
|
def _rendered_text(self, elem):
|
|
96
166
|
preserve = elem.get("{http://www.w3.org/XML/1998/namespace}space") == "preserve"
|
|
@@ -148,7 +218,8 @@ class RedliningValidator:
|
|
|
148
218
|
new.update(elems)
|
|
149
219
|
return new
|
|
150
220
|
|
|
151
|
-
def _generate_detailed_diff(self,
|
|
221
|
+
def _generate_detailed_diff(self, differences):
|
|
222
|
+
self._work_left = [self.WORK_ALLOWED]
|
|
152
223
|
error_parts = [
|
|
153
224
|
"FAILED - Document text doesn't match after removing the tracked changes",
|
|
154
225
|
"",
|
|
@@ -170,130 +241,238 @@ class RedliningValidator:
|
|
|
170
241
|
"",
|
|
171
242
|
]
|
|
172
243
|
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
error_parts
|
|
244
|
+
explain_page_fields = False
|
|
245
|
+
for part, original_text, modified_text in differences:
|
|
246
|
+
body = part == "word/document.xml"
|
|
247
|
+
heading = "Differences:" if body else f"Differences in {part}:"
|
|
248
|
+
if error_parts[-1]:
|
|
249
|
+
error_parts.append("")
|
|
250
|
+
word_diff = self._word_diff(original_text, modified_text)
|
|
251
|
+
error_parts.extend([heading, "=" * len(heading), word_diff])
|
|
252
|
+
if original_text.replace("\n", "") == modified_text.replace("\n", ""):
|
|
253
|
+
error_parts.append("Only paragraph breaks differ here.")
|
|
254
|
+
if re.search(r"\{(%s)\}" % "|".join(self.PAGE_FIELDS), word_diff):
|
|
255
|
+
explain_page_fields = True
|
|
256
|
+
if explain_page_fields:
|
|
257
|
+
error_parts.extend(["", self.PAGE_FIELD_NOTE])
|
|
258
|
+
if self._work_left[0] < 0:
|
|
259
|
+
error_parts.extend(["", self.WORK_SPENT_NOTE])
|
|
178
260
|
|
|
179
261
|
return "\n".join(error_parts)
|
|
180
262
|
|
|
181
|
-
def
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
263
|
+
def _word_diff(self, original_text, modified_text):
|
|
264
|
+
out = []
|
|
265
|
+
steps = self._steps(original_text.split("\n"), modified_text.split("\n"))
|
|
266
|
+
for tag, was, now in steps:
|
|
267
|
+
if tag == "replace" and len(was) == len(now):
|
|
268
|
+
out.extend(self._mark_changes(x, y) for x, y in zip(was, now) if x != y)
|
|
269
|
+
elif tag != "equal":
|
|
270
|
+
out.append(self._mark_changes("\n".join(was), "\n".join(now)))
|
|
271
|
+
return "\n".join(out)
|
|
272
|
+
|
|
273
|
+
def _mark_changes(self, old, new, by_character=False, trim=True):
|
|
274
|
+
|
|
275
|
+
def mark(text, opening, closing):
|
|
276
|
+
marked = re.sub(r"[^\n]+", lambda m: opening + m[0] + closing, text)
|
|
277
|
+
return marked if marked.strip("\n") or opening == "{+" else ""
|
|
278
|
+
|
|
279
|
+
pattern = "(?s)." if by_character else r"\S+\s*|\s+"
|
|
280
|
+
out = []
|
|
281
|
+
steps = self._steps(re.findall(pattern, old), re.findall(pattern, new), trim)
|
|
282
|
+
for tag, was, now in steps:
|
|
283
|
+
lone_word = len(was) == len(now) == 1
|
|
284
|
+
was, now = "".join(was), "".join(now)
|
|
285
|
+
if tag == "equal":
|
|
286
|
+
out.append(now)
|
|
287
|
+
elif tag == "replace" and not by_character:
|
|
288
|
+
long_word = lone_word and min(len(was), len(now)) > self.LONG_WORD
|
|
289
|
+
marked = self._mark_changes(was, now, by_character=True, trim=long_word)
|
|
290
|
+
out.append(marked)
|
|
291
|
+
else:
|
|
292
|
+
out.append(mark(was, "[-", "-]") + mark(now, "{+", "+}"))
|
|
293
|
+
return "".join(out)
|
|
206
294
|
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
content_lines = []
|
|
210
|
-
in_content = False
|
|
211
|
-
for line in lines:
|
|
212
|
-
if line.startswith("@@"):
|
|
213
|
-
in_content = True
|
|
214
|
-
continue
|
|
215
|
-
if in_content and line.strip():
|
|
216
|
-
content_lines.append(line)
|
|
217
|
-
|
|
218
|
-
if content_lines:
|
|
219
|
-
return "\n".join(content_lines)
|
|
220
|
-
|
|
221
|
-
result = subprocess.run(
|
|
222
|
-
[
|
|
223
|
-
"git",
|
|
224
|
-
"diff",
|
|
225
|
-
"--word-diff=plain",
|
|
226
|
-
"-U0",
|
|
227
|
-
"--no-index",
|
|
228
|
-
str(original_file),
|
|
229
|
-
str(modified_file),
|
|
230
|
-
],
|
|
231
|
-
capture_output=True,
|
|
232
|
-
text=True,
|
|
233
|
-
)
|
|
295
|
+
WORK_ALLOWED = 10_000_000
|
|
296
|
+
LONG_WORD = 200
|
|
234
297
|
|
|
235
|
-
|
|
236
|
-
lines = result.stdout.split("\n")
|
|
237
|
-
content_lines = []
|
|
238
|
-
in_content = False
|
|
239
|
-
for line in lines:
|
|
240
|
-
if line.startswith("@@"):
|
|
241
|
-
in_content = True
|
|
242
|
-
continue
|
|
243
|
-
if in_content and line.strip():
|
|
244
|
-
content_lines.append(line)
|
|
245
|
-
return "\n".join(content_lines)
|
|
298
|
+
def _steps(self, a, b, trim=True):
|
|
246
299
|
|
|
247
|
-
|
|
248
|
-
|
|
300
|
+
def shared_start(x, y):
|
|
301
|
+
return next(
|
|
302
|
+
(i for i, (p, q) in enumerate(zip(x, y)) if p != q), min(len(x), len(y))
|
|
303
|
+
)
|
|
249
304
|
|
|
250
|
-
|
|
305
|
+
head = shared_start(a, b) if trim else 0
|
|
306
|
+
tail = shared_start(a[head:][::-1], b[head:][::-1]) if trim else 0
|
|
307
|
+
a_mid, b_mid = a[head : len(a) - tail], b[head : len(b) - tail]
|
|
308
|
+
try:
|
|
309
|
+
if self._work_left[0] < len(a_mid):
|
|
310
|
+
self._work_left[0] = -1
|
|
311
|
+
raise _WorkSpent
|
|
312
|
+
numbers = {}
|
|
313
|
+
numbered = [
|
|
314
|
+
[numbers.setdefault(x, len(numbers)) for x in s] for s in (a_mid, b_mid)
|
|
315
|
+
]
|
|
316
|
+
opcodes = _Matcher(*numbered, self._work_left).get_opcodes()
|
|
317
|
+
steps = [(tag, a_mid[i:j], b_mid[m:n]) for tag, i, j, m, n in opcodes]
|
|
318
|
+
except _WorkSpent:
|
|
319
|
+
steps = [("replace", a_mid, b_mid)]
|
|
320
|
+
return [
|
|
321
|
+
("equal", a[:head], b[:head]),
|
|
322
|
+
*steps,
|
|
323
|
+
("equal", a[len(a) - tail :], b[len(b) - tail :]),
|
|
324
|
+
]
|
|
251
325
|
|
|
252
326
|
def _remove_tracked_changes(self, root, targets):
|
|
253
|
-
|
|
254
|
-
|
|
327
|
+
w = self.namespaces["w"]
|
|
328
|
+
ins_tag = f"{{{w}}}ins"
|
|
329
|
+
del_tag = f"{{{w}}}del"
|
|
330
|
+
move_from_tag = f"{{{w}}}moveFrom"
|
|
331
|
+
move_to_tag = f"{{{w}}}moveTo"
|
|
332
|
+
|
|
333
|
+
self._merge_split_paragraphs(root, targets)
|
|
255
334
|
|
|
256
335
|
for parent in root.iter():
|
|
257
336
|
to_remove = []
|
|
258
337
|
for child in parent:
|
|
259
|
-
if child.tag
|
|
338
|
+
if child.tag in (ins_tag, move_to_tag) and child in targets:
|
|
260
339
|
to_remove.append(child)
|
|
261
340
|
for elem in to_remove:
|
|
262
341
|
parent.remove(elem)
|
|
263
342
|
|
|
264
|
-
|
|
265
|
-
|
|
343
|
+
renamed = {
|
|
344
|
+
f"{{{w}}}delText": f"{{{w}}}t",
|
|
345
|
+
f"{{{w}}}delInstrText": f"{{{w}}}instrText",
|
|
346
|
+
}
|
|
266
347
|
|
|
267
348
|
for parent in root.iter():
|
|
268
349
|
to_process = []
|
|
269
350
|
for child in parent:
|
|
270
|
-
if child.tag
|
|
351
|
+
if child.tag in (del_tag, move_from_tag) and child in targets:
|
|
271
352
|
to_process.append((child, list(parent).index(child)))
|
|
272
353
|
|
|
273
354
|
for del_elem, del_index in reversed(to_process):
|
|
274
355
|
for elem in del_elem.iter():
|
|
275
|
-
|
|
276
|
-
elem.tag = t_tag
|
|
356
|
+
elem.tag = renamed.get(elem.tag, elem.tag)
|
|
277
357
|
|
|
278
358
|
for child in reversed(list(del_elem)):
|
|
279
359
|
parent.insert(del_index, child)
|
|
280
360
|
parent.remove(del_elem)
|
|
281
361
|
|
|
362
|
+
range_tags = (
|
|
363
|
+
f"{{{w}}}moveFromRangeStart",
|
|
364
|
+
f"{{{w}}}moveFromRangeEnd",
|
|
365
|
+
f"{{{w}}}moveToRangeStart",
|
|
366
|
+
f"{{{w}}}moveToRangeEnd",
|
|
367
|
+
)
|
|
368
|
+
for parent in root.iter():
|
|
369
|
+
to_remove = [
|
|
370
|
+
c
|
|
371
|
+
for c in parent
|
|
372
|
+
if c.tag in range_tags and len(c) == 0 and not (c.text or "").strip()
|
|
373
|
+
]
|
|
374
|
+
for elem in to_remove:
|
|
375
|
+
parent.remove(elem)
|
|
376
|
+
|
|
377
|
+
def _merge_split_paragraphs(self, root, targets):
|
|
378
|
+
w = self.namespaces["w"]
|
|
379
|
+
p_tag = f"{{{w}}}p"
|
|
380
|
+
ppr_tag = f"{{{w}}}pPr"
|
|
381
|
+
marker_path = f"{ppr_tag}/{{{w}}}rPr/{{{w}}}ins"
|
|
382
|
+
|
|
383
|
+
for parent in list(root.iter()):
|
|
384
|
+
for para in reversed([child for child in parent if child.tag == p_tag]):
|
|
385
|
+
if not any(m in targets for m in para.findall(marker_path)):
|
|
386
|
+
continue
|
|
387
|
+
siblings = list(parent)
|
|
388
|
+
index = siblings.index(para)
|
|
389
|
+
following = siblings[index + 1] if index + 1 < len(siblings) else None
|
|
390
|
+
if following is None or following.tag != p_tag:
|
|
391
|
+
continue
|
|
392
|
+
if any(self._text_elements(c) for c in para if c.tag == ppr_tag):
|
|
393
|
+
continue
|
|
394
|
+
insert_at = 1 if len(following) and following[0].tag == ppr_tag else 0
|
|
395
|
+
for child in reversed([c for c in para if c.tag != ppr_tag]):
|
|
396
|
+
following.insert(insert_at, child)
|
|
397
|
+
parent.remove(para)
|
|
398
|
+
|
|
282
399
|
def _extract_text_content(self, root):
|
|
283
400
|
p_tag = f"{{{self.namespaces['w']}}}p"
|
|
284
|
-
t_tag = f"{{{self.namespaces['w']}}}t"
|
|
285
401
|
|
|
286
402
|
paragraphs = []
|
|
287
403
|
for p_elem in root.findall(f".//{p_tag}"):
|
|
288
|
-
|
|
289
|
-
for t_elem in p_elem.findall(f".//{t_tag}"):
|
|
290
|
-
text_parts.append(self._rendered_text(t_elem))
|
|
291
|
-
paragraph_text = "".join(text_parts)
|
|
404
|
+
paragraph_text = self._paragraph_text(p_elem)
|
|
292
405
|
if paragraph_text:
|
|
293
406
|
paragraphs.append(paragraph_text)
|
|
294
407
|
|
|
295
408
|
return "\n".join(paragraphs)
|
|
296
409
|
|
|
410
|
+
PAGE_FIELDS = {"PAGE", "NUMPAGES", "SECTIONPAGES"}
|
|
411
|
+
PAGE_NUMBER = re.compile(r"-? ?[0-9]{1,6} ?-?|[ivxlcdm]{1,8}|[a-z]{1,2}|", re.I)
|
|
412
|
+
PAGE_FIELD_NOTE = (
|
|
413
|
+
"Note: {PAGE}, {NUMPAGES} or {SECTIONPAGES} above stands for a page-number\n"
|
|
414
|
+
"field. Word recomputes the number it displays, so the number stored in\n"
|
|
415
|
+
"the file is not compared, but adding or removing the field still counts."
|
|
416
|
+
)
|
|
417
|
+
|
|
418
|
+
WORK_SPENT_NOTE = (
|
|
419
|
+
"Note: the texts were too long or repetitive to compare closely all the\n"
|
|
420
|
+
"way through, so a passage shown whole, as removed and then added, may\n"
|
|
421
|
+
"differ in only a few characters. Compare such a passage with the\n"
|
|
422
|
+
"original directly to find the edit."
|
|
423
|
+
)
|
|
424
|
+
|
|
425
|
+
def _paragraph_text(self, p_elem):
|
|
426
|
+
w = self.namespaces["w"]
|
|
427
|
+
t_tag, instr_tag = f"{{{w}}}t", f"{{{w}}}instrText"
|
|
428
|
+
fld_char, fld_simple = f"{{{w}}}fldChar", f"{{{w}}}fldSimple"
|
|
429
|
+
|
|
430
|
+
def page_field(instruction, field):
|
|
431
|
+
words = instruction.split()
|
|
432
|
+
locked = field.get(f"{{{w}}}fldLock") in ("1", "true", "on")
|
|
433
|
+
if words and words[0].upper() in self.PAGE_FIELDS and not locked:
|
|
434
|
+
return words[0].upper()
|
|
435
|
+
|
|
436
|
+
def shown(name, result):
|
|
437
|
+
return "{%s}" % name if self.PAGE_NUMBER.fullmatch(result) else result
|
|
438
|
+
|
|
439
|
+
parts, fields = [], []
|
|
440
|
+
skipped = set()
|
|
441
|
+
|
|
442
|
+
def sink():
|
|
443
|
+
for *_, result in reversed(fields):
|
|
444
|
+
if result is not None:
|
|
445
|
+
return result
|
|
446
|
+
return parts
|
|
447
|
+
|
|
448
|
+
for elem in p_elem.iter():
|
|
449
|
+
if elem.tag == fld_simple:
|
|
450
|
+
name = page_field(elem.get(f"{{{w}}}instr", ""), elem)
|
|
451
|
+
if name:
|
|
452
|
+
texts = [self._rendered_text(t) for t in elem.iter(t_tag)]
|
|
453
|
+
sink().append(shown(name, "".join(texts).strip()))
|
|
454
|
+
skipped.update(elem.iter(t_tag))
|
|
455
|
+
elif elem.tag == fld_char:
|
|
456
|
+
kind = elem.get(f"{{{w}}}fldCharType")
|
|
457
|
+
if kind == "begin":
|
|
458
|
+
fields.append([elem, "", False, None])
|
|
459
|
+
elif kind == "separate" and fields and not fields[-1][2]:
|
|
460
|
+
fields[-1][2] = True
|
|
461
|
+
if page_field(fields[-1][1], fields[-1][0]):
|
|
462
|
+
fields[-1][3] = []
|
|
463
|
+
elif kind == "end" and fields:
|
|
464
|
+
begin, instruction, _, result = fields.pop()
|
|
465
|
+
name = page_field(instruction, begin)
|
|
466
|
+
if name:
|
|
467
|
+
sink().append(shown(name, "".join(result or []).strip()))
|
|
468
|
+
elif elem.tag == instr_tag and fields and not fields[-1][2]:
|
|
469
|
+
fields[-1][1] += elem.text or ""
|
|
470
|
+
elif elem.tag == t_tag and elem not in skipped:
|
|
471
|
+
sink().append(self._rendered_text(elem))
|
|
472
|
+
for *_, result in fields:
|
|
473
|
+
parts.extend(result or [])
|
|
474
|
+
return "".join(parts)
|
|
475
|
+
|
|
297
476
|
|
|
298
477
|
if __name__ == "__main__":
|
|
299
478
|
raise RuntimeError("This module should not be run directly.")
|