@tiens.nguyen/gu-cli 1.0.902 → 1.0.904

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.
Files changed (187) hide show
  1. package/NOTICE +18 -0
  2. package/package.json +3 -2
  3. package/skills/core/deep-research/SKILL.md +51 -0
  4. package/skills/core/docx/LICENSE.txt +202 -0
  5. package/skills/core/docx/SKILL.md +97 -0
  6. package/skills/core/docx/scripts/__init__.py +1 -0
  7. package/skills/core/docx/scripts/accept_changes.py +135 -0
  8. package/skills/core/docx/scripts/comment.py +368 -0
  9. package/skills/core/docx/scripts/merge_runs.py +310 -0
  10. package/skills/core/docx/scripts/office/helpers/__init__.py +121 -0
  11. package/skills/core/docx/scripts/office/helpers/pptx_chart.py +283 -0
  12. package/skills/core/docx/scripts/office/helpers/pptx_slide.py +100 -0
  13. package/skills/core/docx/scripts/office/helpers/pptx_theme.py +114 -0
  14. package/skills/core/docx/scripts/office/schemas/ISO-IEC29500-4_2016/dml-chart.xsd +1499 -0
  15. package/skills/core/docx/scripts/office/schemas/ISO-IEC29500-4_2016/dml-chartDrawing.xsd +146 -0
  16. package/skills/core/docx/scripts/office/schemas/ISO-IEC29500-4_2016/dml-diagram.xsd +1085 -0
  17. package/skills/core/docx/scripts/office/schemas/ISO-IEC29500-4_2016/dml-lockedCanvas.xsd +11 -0
  18. package/skills/core/docx/scripts/office/schemas/ISO-IEC29500-4_2016/dml-main.xsd +3081 -0
  19. package/skills/core/docx/scripts/office/schemas/ISO-IEC29500-4_2016/dml-picture.xsd +23 -0
  20. package/skills/core/docx/scripts/office/schemas/ISO-IEC29500-4_2016/dml-spreadsheetDrawing.xsd +185 -0
  21. package/skills/core/docx/scripts/office/schemas/ISO-IEC29500-4_2016/dml-wordprocessingDrawing.xsd +287 -0
  22. package/skills/core/docx/scripts/office/schemas/ISO-IEC29500-4_2016/pml.xsd +1676 -0
  23. package/skills/core/docx/scripts/office/schemas/ISO-IEC29500-4_2016/shared-additionalCharacteristics.xsd +28 -0
  24. package/skills/core/docx/scripts/office/schemas/ISO-IEC29500-4_2016/shared-bibliography.xsd +144 -0
  25. package/skills/core/docx/scripts/office/schemas/ISO-IEC29500-4_2016/shared-commonSimpleTypes.xsd +174 -0
  26. package/skills/core/docx/scripts/office/schemas/ISO-IEC29500-4_2016/shared-customXmlDataProperties.xsd +25 -0
  27. package/skills/core/docx/scripts/office/schemas/ISO-IEC29500-4_2016/shared-customXmlSchemaProperties.xsd +18 -0
  28. package/skills/core/docx/scripts/office/schemas/ISO-IEC29500-4_2016/shared-documentPropertiesCustom.xsd +59 -0
  29. package/skills/core/docx/scripts/office/schemas/ISO-IEC29500-4_2016/shared-documentPropertiesExtended.xsd +56 -0
  30. package/skills/core/docx/scripts/office/schemas/ISO-IEC29500-4_2016/shared-documentPropertiesVariantTypes.xsd +195 -0
  31. package/skills/core/docx/scripts/office/schemas/ISO-IEC29500-4_2016/shared-math.xsd +582 -0
  32. package/skills/core/docx/scripts/office/schemas/ISO-IEC29500-4_2016/shared-relationshipReference.xsd +25 -0
  33. package/skills/core/docx/scripts/office/schemas/ISO-IEC29500-4_2016/sml.xsd +4439 -0
  34. package/skills/core/docx/scripts/office/schemas/ISO-IEC29500-4_2016/vml-main.xsd +570 -0
  35. package/skills/core/docx/scripts/office/schemas/ISO-IEC29500-4_2016/vml-officeDrawing.xsd +509 -0
  36. package/skills/core/docx/scripts/office/schemas/ISO-IEC29500-4_2016/vml-presentationDrawing.xsd +12 -0
  37. package/skills/core/docx/scripts/office/schemas/ISO-IEC29500-4_2016/vml-spreadsheetDrawing.xsd +108 -0
  38. package/skills/core/docx/scripts/office/schemas/ISO-IEC29500-4_2016/vml-wordprocessingDrawing.xsd +96 -0
  39. package/skills/core/docx/scripts/office/schemas/ISO-IEC29500-4_2016/wml.xsd +3646 -0
  40. package/skills/core/docx/scripts/office/schemas/ISO-IEC29500-4_2016/xml.xsd +116 -0
  41. package/skills/core/docx/scripts/office/schemas/ecma/fouth-edition/opc-contentTypes.xsd +42 -0
  42. package/skills/core/docx/scripts/office/schemas/ecma/fouth-edition/opc-coreProperties.xsd +50 -0
  43. package/skills/core/docx/scripts/office/schemas/ecma/fouth-edition/opc-digSig.xsd +49 -0
  44. package/skills/core/docx/scripts/office/schemas/ecma/fouth-edition/opc-relationships.xsd +33 -0
  45. package/skills/core/docx/scripts/office/schemas/mce/mc.xsd +75 -0
  46. package/skills/core/docx/scripts/office/schemas/microsoft/wml-2010.xsd +560 -0
  47. package/skills/core/docx/scripts/office/schemas/microsoft/wml-2012.xsd +67 -0
  48. package/skills/core/docx/scripts/office/schemas/microsoft/wml-2018.xsd +14 -0
  49. package/skills/core/docx/scripts/office/schemas/microsoft/wml-cex-2018.xsd +20 -0
  50. package/skills/core/docx/scripts/office/schemas/microsoft/wml-cid-2016.xsd +13 -0
  51. package/skills/core/docx/scripts/office/schemas/microsoft/wml-sdtdatahash-2020.xsd +4 -0
  52. package/skills/core/docx/scripts/office/schemas/microsoft/wml-symex-2015.xsd +8 -0
  53. package/skills/core/docx/scripts/office/soffice.py +192 -0
  54. package/skills/core/docx/scripts/office/validate.py +210 -0
  55. package/skills/core/docx/scripts/office/validators/__init__.py +15 -0
  56. package/skills/core/docx/scripts/office/validators/base.py +874 -0
  57. package/skills/core/docx/scripts/office/validators/docx.py +519 -0
  58. package/skills/core/docx/scripts/office/validators/pptx.py +549 -0
  59. package/skills/core/docx/scripts/office/validators/redlining.py +461 -0
  60. package/skills/core/docx/scripts/templates/comments.xml +3 -0
  61. package/skills/core/docx/scripts/templates/commentsExtended.xml +3 -0
  62. package/skills/core/docx/scripts/templates/commentsExtensible.xml +3 -0
  63. package/skills/core/docx/scripts/templates/commentsIds.xml +3 -0
  64. package/skills/core/docx/scripts/templates/people.xml +3 -0
  65. package/skills/core/pdf/FORMS.md +294 -0
  66. package/skills/core/pdf/LICENSE.txt +202 -0
  67. package/skills/core/pdf/REFERENCE.md +613 -0
  68. package/skills/core/pdf/SKILL.md +314 -0
  69. package/skills/core/pdf/scripts/check_bounding_boxes.py +65 -0
  70. package/skills/core/pdf/scripts/check_fillable_fields.py +11 -0
  71. package/skills/core/pdf/scripts/convert_pdf_to_images.py +33 -0
  72. package/skills/core/pdf/scripts/create_validation_image.py +37 -0
  73. package/skills/core/pdf/scripts/extract_form_field_info.py +122 -0
  74. package/skills/core/pdf/scripts/extract_form_structure.py +115 -0
  75. package/skills/core/pdf/scripts/fill_fillable_fields.py +98 -0
  76. package/skills/core/pdf/scripts/fill_pdf_form_with_annotations.py +107 -0
  77. package/skills/core/pptx/LICENSE.txt +202 -0
  78. package/skills/core/pptx/SKILL.md +275 -0
  79. package/skills/core/pptx/scripts/__init__.py +0 -0
  80. package/skills/core/pptx/scripts/add_slide.py +463 -0
  81. package/skills/core/pptx/scripts/apply_theme.js +107 -0
  82. package/skills/core/pptx/scripts/clean.py +309 -0
  83. package/skills/core/pptx/scripts/office/helpers/__init__.py +121 -0
  84. package/skills/core/pptx/scripts/office/helpers/pptx_chart.py +283 -0
  85. package/skills/core/pptx/scripts/office/helpers/pptx_slide.py +100 -0
  86. package/skills/core/pptx/scripts/office/helpers/pptx_theme.py +114 -0
  87. package/skills/core/pptx/scripts/office/schemas/ISO-IEC29500-4_2016/dml-chart.xsd +1499 -0
  88. package/skills/core/pptx/scripts/office/schemas/ISO-IEC29500-4_2016/dml-chartDrawing.xsd +146 -0
  89. package/skills/core/pptx/scripts/office/schemas/ISO-IEC29500-4_2016/dml-diagram.xsd +1085 -0
  90. package/skills/core/pptx/scripts/office/schemas/ISO-IEC29500-4_2016/dml-lockedCanvas.xsd +11 -0
  91. package/skills/core/pptx/scripts/office/schemas/ISO-IEC29500-4_2016/dml-main.xsd +3081 -0
  92. package/skills/core/pptx/scripts/office/schemas/ISO-IEC29500-4_2016/dml-picture.xsd +23 -0
  93. package/skills/core/pptx/scripts/office/schemas/ISO-IEC29500-4_2016/dml-spreadsheetDrawing.xsd +185 -0
  94. package/skills/core/pptx/scripts/office/schemas/ISO-IEC29500-4_2016/dml-wordprocessingDrawing.xsd +287 -0
  95. package/skills/core/pptx/scripts/office/schemas/ISO-IEC29500-4_2016/pml.xsd +1676 -0
  96. package/skills/core/pptx/scripts/office/schemas/ISO-IEC29500-4_2016/shared-additionalCharacteristics.xsd +28 -0
  97. package/skills/core/pptx/scripts/office/schemas/ISO-IEC29500-4_2016/shared-bibliography.xsd +144 -0
  98. package/skills/core/pptx/scripts/office/schemas/ISO-IEC29500-4_2016/shared-commonSimpleTypes.xsd +174 -0
  99. package/skills/core/pptx/scripts/office/schemas/ISO-IEC29500-4_2016/shared-customXmlDataProperties.xsd +25 -0
  100. package/skills/core/pptx/scripts/office/schemas/ISO-IEC29500-4_2016/shared-customXmlSchemaProperties.xsd +18 -0
  101. package/skills/core/pptx/scripts/office/schemas/ISO-IEC29500-4_2016/shared-documentPropertiesCustom.xsd +59 -0
  102. package/skills/core/pptx/scripts/office/schemas/ISO-IEC29500-4_2016/shared-documentPropertiesExtended.xsd +56 -0
  103. package/skills/core/pptx/scripts/office/schemas/ISO-IEC29500-4_2016/shared-documentPropertiesVariantTypes.xsd +195 -0
  104. package/skills/core/pptx/scripts/office/schemas/ISO-IEC29500-4_2016/shared-math.xsd +582 -0
  105. package/skills/core/pptx/scripts/office/schemas/ISO-IEC29500-4_2016/shared-relationshipReference.xsd +25 -0
  106. package/skills/core/pptx/scripts/office/schemas/ISO-IEC29500-4_2016/sml.xsd +4439 -0
  107. package/skills/core/pptx/scripts/office/schemas/ISO-IEC29500-4_2016/vml-main.xsd +570 -0
  108. package/skills/core/pptx/scripts/office/schemas/ISO-IEC29500-4_2016/vml-officeDrawing.xsd +509 -0
  109. package/skills/core/pptx/scripts/office/schemas/ISO-IEC29500-4_2016/vml-presentationDrawing.xsd +12 -0
  110. package/skills/core/pptx/scripts/office/schemas/ISO-IEC29500-4_2016/vml-spreadsheetDrawing.xsd +108 -0
  111. package/skills/core/pptx/scripts/office/schemas/ISO-IEC29500-4_2016/vml-wordprocessingDrawing.xsd +96 -0
  112. package/skills/core/pptx/scripts/office/schemas/ISO-IEC29500-4_2016/wml.xsd +3646 -0
  113. package/skills/core/pptx/scripts/office/schemas/ISO-IEC29500-4_2016/xml.xsd +116 -0
  114. package/skills/core/pptx/scripts/office/schemas/ecma/fouth-edition/opc-contentTypes.xsd +42 -0
  115. package/skills/core/pptx/scripts/office/schemas/ecma/fouth-edition/opc-coreProperties.xsd +50 -0
  116. package/skills/core/pptx/scripts/office/schemas/ecma/fouth-edition/opc-digSig.xsd +49 -0
  117. package/skills/core/pptx/scripts/office/schemas/ecma/fouth-edition/opc-relationships.xsd +33 -0
  118. package/skills/core/pptx/scripts/office/schemas/mce/mc.xsd +75 -0
  119. package/skills/core/pptx/scripts/office/schemas/microsoft/wml-2010.xsd +560 -0
  120. package/skills/core/pptx/scripts/office/schemas/microsoft/wml-2012.xsd +67 -0
  121. package/skills/core/pptx/scripts/office/schemas/microsoft/wml-2018.xsd +14 -0
  122. package/skills/core/pptx/scripts/office/schemas/microsoft/wml-cex-2018.xsd +20 -0
  123. package/skills/core/pptx/scripts/office/schemas/microsoft/wml-cid-2016.xsd +13 -0
  124. package/skills/core/pptx/scripts/office/schemas/microsoft/wml-sdtdatahash-2020.xsd +4 -0
  125. package/skills/core/pptx/scripts/office/schemas/microsoft/wml-symex-2015.xsd +8 -0
  126. package/skills/core/pptx/scripts/office/soffice.py +192 -0
  127. package/skills/core/pptx/scripts/office/validate.py +210 -0
  128. package/skills/core/pptx/scripts/office/validators/__init__.py +15 -0
  129. package/skills/core/pptx/scripts/office/validators/base.py +874 -0
  130. package/skills/core/pptx/scripts/office/validators/docx.py +519 -0
  131. package/skills/core/pptx/scripts/office/validators/pptx.py +549 -0
  132. package/skills/core/pptx/scripts/office/validators/redlining.py +461 -0
  133. package/skills/core/pptx/scripts/slide_text.py +132 -0
  134. package/skills/core/pptx/scripts/thumbnail.py +311 -0
  135. package/skills/core/xlsx/LICENSE.txt +202 -0
  136. package/skills/core/xlsx/SKILL.md +99 -0
  137. package/skills/core/xlsx/scripts/office/helpers/__init__.py +121 -0
  138. package/skills/core/xlsx/scripts/office/helpers/pptx_chart.py +283 -0
  139. package/skills/core/xlsx/scripts/office/helpers/pptx_slide.py +100 -0
  140. package/skills/core/xlsx/scripts/office/helpers/pptx_theme.py +114 -0
  141. package/skills/core/xlsx/scripts/office/schemas/ISO-IEC29500-4_2016/dml-chart.xsd +1499 -0
  142. package/skills/core/xlsx/scripts/office/schemas/ISO-IEC29500-4_2016/dml-chartDrawing.xsd +146 -0
  143. package/skills/core/xlsx/scripts/office/schemas/ISO-IEC29500-4_2016/dml-diagram.xsd +1085 -0
  144. package/skills/core/xlsx/scripts/office/schemas/ISO-IEC29500-4_2016/dml-lockedCanvas.xsd +11 -0
  145. package/skills/core/xlsx/scripts/office/schemas/ISO-IEC29500-4_2016/dml-main.xsd +3081 -0
  146. package/skills/core/xlsx/scripts/office/schemas/ISO-IEC29500-4_2016/dml-picture.xsd +23 -0
  147. package/skills/core/xlsx/scripts/office/schemas/ISO-IEC29500-4_2016/dml-spreadsheetDrawing.xsd +185 -0
  148. package/skills/core/xlsx/scripts/office/schemas/ISO-IEC29500-4_2016/dml-wordprocessingDrawing.xsd +287 -0
  149. package/skills/core/xlsx/scripts/office/schemas/ISO-IEC29500-4_2016/pml.xsd +1676 -0
  150. package/skills/core/xlsx/scripts/office/schemas/ISO-IEC29500-4_2016/shared-additionalCharacteristics.xsd +28 -0
  151. package/skills/core/xlsx/scripts/office/schemas/ISO-IEC29500-4_2016/shared-bibliography.xsd +144 -0
  152. package/skills/core/xlsx/scripts/office/schemas/ISO-IEC29500-4_2016/shared-commonSimpleTypes.xsd +174 -0
  153. package/skills/core/xlsx/scripts/office/schemas/ISO-IEC29500-4_2016/shared-customXmlDataProperties.xsd +25 -0
  154. package/skills/core/xlsx/scripts/office/schemas/ISO-IEC29500-4_2016/shared-customXmlSchemaProperties.xsd +18 -0
  155. package/skills/core/xlsx/scripts/office/schemas/ISO-IEC29500-4_2016/shared-documentPropertiesCustom.xsd +59 -0
  156. package/skills/core/xlsx/scripts/office/schemas/ISO-IEC29500-4_2016/shared-documentPropertiesExtended.xsd +56 -0
  157. package/skills/core/xlsx/scripts/office/schemas/ISO-IEC29500-4_2016/shared-documentPropertiesVariantTypes.xsd +195 -0
  158. package/skills/core/xlsx/scripts/office/schemas/ISO-IEC29500-4_2016/shared-math.xsd +582 -0
  159. package/skills/core/xlsx/scripts/office/schemas/ISO-IEC29500-4_2016/shared-relationshipReference.xsd +25 -0
  160. package/skills/core/xlsx/scripts/office/schemas/ISO-IEC29500-4_2016/sml.xsd +4439 -0
  161. package/skills/core/xlsx/scripts/office/schemas/ISO-IEC29500-4_2016/vml-main.xsd +570 -0
  162. package/skills/core/xlsx/scripts/office/schemas/ISO-IEC29500-4_2016/vml-officeDrawing.xsd +509 -0
  163. package/skills/core/xlsx/scripts/office/schemas/ISO-IEC29500-4_2016/vml-presentationDrawing.xsd +12 -0
  164. package/skills/core/xlsx/scripts/office/schemas/ISO-IEC29500-4_2016/vml-spreadsheetDrawing.xsd +108 -0
  165. package/skills/core/xlsx/scripts/office/schemas/ISO-IEC29500-4_2016/vml-wordprocessingDrawing.xsd +96 -0
  166. package/skills/core/xlsx/scripts/office/schemas/ISO-IEC29500-4_2016/wml.xsd +3646 -0
  167. package/skills/core/xlsx/scripts/office/schemas/ISO-IEC29500-4_2016/xml.xsd +116 -0
  168. package/skills/core/xlsx/scripts/office/schemas/ecma/fouth-edition/opc-contentTypes.xsd +42 -0
  169. package/skills/core/xlsx/scripts/office/schemas/ecma/fouth-edition/opc-coreProperties.xsd +50 -0
  170. package/skills/core/xlsx/scripts/office/schemas/ecma/fouth-edition/opc-digSig.xsd +49 -0
  171. package/skills/core/xlsx/scripts/office/schemas/ecma/fouth-edition/opc-relationships.xsd +33 -0
  172. package/skills/core/xlsx/scripts/office/schemas/mce/mc.xsd +75 -0
  173. package/skills/core/xlsx/scripts/office/schemas/microsoft/wml-2010.xsd +560 -0
  174. package/skills/core/xlsx/scripts/office/schemas/microsoft/wml-2012.xsd +67 -0
  175. package/skills/core/xlsx/scripts/office/schemas/microsoft/wml-2018.xsd +14 -0
  176. package/skills/core/xlsx/scripts/office/schemas/microsoft/wml-cex-2018.xsd +20 -0
  177. package/skills/core/xlsx/scripts/office/schemas/microsoft/wml-cid-2016.xsd +13 -0
  178. package/skills/core/xlsx/scripts/office/schemas/microsoft/wml-sdtdatahash-2020.xsd +4 -0
  179. package/skills/core/xlsx/scripts/office/schemas/microsoft/wml-symex-2015.xsd +8 -0
  180. package/skills/core/xlsx/scripts/office/soffice.py +192 -0
  181. package/skills/core/xlsx/scripts/office/validate.py +210 -0
  182. package/skills/core/xlsx/scripts/office/validators/__init__.py +15 -0
  183. package/skills/core/xlsx/scripts/office/validators/base.py +874 -0
  184. package/skills/core/xlsx/scripts/office/validators/docx.py +519 -0
  185. package/skills/core/xlsx/scripts/office/validators/pptx.py +549 -0
  186. package/skills/core/xlsx/scripts/office/validators/redlining.py +461 -0
  187. package/skills/core/xlsx/scripts/recalc.py +456 -0
@@ -0,0 +1,461 @@
1
+ """
2
+ Validator for tracked changes in Word documents.
3
+
4
+ Detects untracked edits in a Word document: text that differs from the
5
+ original without a <w:ins>/<w:del> wrapper recording it. The tracked changes
6
+ that are new relative to the original are undone, and the result is compared
7
+ against the original; whatever text still differs was edited without being
8
+ tracked.
9
+
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.
16
+ """
17
+
18
+ import re
19
+ import subprocess
20
+ import tempfile
21
+ import zipfile
22
+ from pathlib import Path
23
+
24
+ import defusedxml.ElementTree as ET
25
+ from defusedxml.common import DefusedXmlException
26
+
27
+ from helpers import rendered_text, safe_extract
28
+
29
+
30
+ XML_ERRORS = (ET.ParseError, DefusedXmlException, LookupError, ValueError)
31
+
32
+
33
+ class RedliningValidator:
34
+
35
+ def __init__(self, unpacked_dir, original_docx, verbose=False):
36
+ self.unpacked_dir = Path(unpacked_dir)
37
+ self.original_docx = Path(original_docx)
38
+ self.verbose = verbose
39
+ self.namespaces = {
40
+ "w": "http://schemas.openxmlformats.org/wordprocessingml/2006/main"
41
+ }
42
+
43
+ def repair(self) -> int:
44
+ return 0
45
+
46
+ def validate(self):
47
+ modified_file = self.unpacked_dir / "word" / "document.xml"
48
+ if not modified_file.exists():
49
+ print(f"FAILED - Modified document.xml not found at {modified_file}")
50
+ return False
51
+
52
+ with tempfile.TemporaryDirectory() as temp_dir:
53
+ temp_path = Path(temp_dir)
54
+
55
+ try:
56
+ with zipfile.ZipFile(self.original_docx, "r") as zip_ref:
57
+ safe_extract(zip_ref, temp_path)
58
+ except Exception as e:
59
+ print(f"FAILED - Error unpacking original docx: {e}")
60
+ return False
61
+
62
+ original_file = temp_path / "word" / "document.xml"
63
+ if not original_file.exists():
64
+ print(
65
+ f"FAILED - Original document.xml not found in {self.original_docx}"
66
+ )
67
+ return False
68
+
69
+ sides = (("original", temp_path), ("modified", self.unpacked_dir))
70
+ change_count, failures, differences = 0, [], []
71
+ for part in self._parts_to_compare(temp_path, self.unpacked_dir):
72
+ roots = []
73
+ for side, unpacked in sides:
74
+ try:
75
+ roots.append(self._parse_part(unpacked / part))
76
+ except XML_ERRORS as e:
77
+ failures.append(
78
+ f"FAILED - Error parsing {part} of the {side} document: {e}"
79
+ )
80
+ if len(roots) < len(sides):
81
+ continue
82
+ new_changes, original_text, modified_text = self._compare_part(*roots)
83
+ change_count += len(new_changes)
84
+ if modified_text != original_text:
85
+ differences.append((part, original_text, modified_text))
86
+
87
+ if differences:
88
+ failures.append(self._generate_detailed_diff(differences))
89
+ if failures:
90
+ print("\n".join(failures))
91
+ return False
92
+
93
+ if self.verbose:
94
+ print(
95
+ f"PASSED - All {change_count} change(s) against the original "
96
+ "are properly tracked"
97
+ )
98
+ return True
99
+
100
+ OTHER_PARTS = (
101
+ "word/header*.xml",
102
+ "word/footer*.xml",
103
+ "word/footnotes.xml",
104
+ "word/endnotes.xml",
105
+ )
106
+
107
+ def _parts_to_compare(self, original_dir, modified_dir):
108
+ others = set()
109
+ for unpacked in (Path(original_dir), Path(modified_dir)):
110
+ for pattern in self.OTHER_PARTS:
111
+ others.update(
112
+ path.relative_to(unpacked).as_posix()
113
+ for path in unpacked.glob(pattern)
114
+ )
115
+ return ["word/document.xml", *sorted(others)]
116
+
117
+ def _compare_part(self, original_root, modified_root):
118
+ for root in (original_root, modified_root):
119
+ self._flatten_text_elements(root)
120
+ new_changes = self._new_tracked_changes(original_root, modified_root)
121
+ self._remove_tracked_changes(modified_root, new_changes)
122
+ return (
123
+ new_changes,
124
+ self._extract_text_content(original_root),
125
+ self._extract_text_content(modified_root),
126
+ )
127
+
128
+ def _parse_part(self, path):
129
+ if not path.is_file():
130
+ return ET.fromstring("<absent/>")
131
+ return ET.parse(path).getroot()
132
+
133
+ def _flatten_text_elements(self, root):
134
+ for elem in self._text_elements(root):
135
+ if len(elem):
136
+ elem.text = "".join(elem.itertext())
137
+ for child in list(elem):
138
+ elem.remove(child)
139
+
140
+ def _tracked_change_elements(self, root):
141
+ w = self.namespaces["w"]
142
+ tags = (f"{{{w}}}ins", f"{{{w}}}del", f"{{{w}}}moveFrom", f"{{{w}}}moveTo")
143
+ return [elem for elem in root.iter() if elem.tag in tags]
144
+
145
+ def _rendered_text(self, elem):
146
+ preserve = elem.get("{http://www.w3.org/XML/1998/namespace}space") == "preserve"
147
+ return rendered_text(elem.text or "", preserve)
148
+
149
+ def _text_elements(self, elem):
150
+ w = self.namespaces["w"]
151
+ return [
152
+ node
153
+ for node in elem.iter()
154
+ if node.tag in (f"{{{w}}}t", f"{{{w}}}delText")
155
+ ]
156
+
157
+ def _tracked_change_key(self, elem):
158
+ w = self.namespaces["w"]
159
+ text = "".join(self._rendered_text(node) for node in self._text_elements(elem))
160
+ return (elem.tag, elem.get(f"{{{w}}}author"), elem.get(f"{{{w}}}date"), text)
161
+
162
+ def _new_tracked_changes(self, original_root, modified_root):
163
+ original = self._tracked_change_elements(original_root)
164
+ modified = self._tracked_change_elements(modified_root)
165
+
166
+ pool = {}
167
+ for elem in original:
168
+ pool.setdefault(self._tracked_change_key(elem), []).append(elem)
169
+
170
+ matched, leftover = set(), []
171
+ for elem in modified:
172
+ bucket = pool.get(self._tracked_change_key(elem))
173
+ if bucket:
174
+ matched.add(bucket.pop())
175
+ else:
176
+ leftover.append(elem)
177
+
178
+ def group(elem):
179
+ return self._tracked_change_key(elem)[:3]
180
+
181
+ def text_of(elems):
182
+ return "".join(self._tracked_change_key(e)[3] for e in elems)
183
+
184
+ unmatched_original = {}
185
+ for elem in original:
186
+ if elem not in matched:
187
+ unmatched_original.setdefault(group(elem), []).append(elem)
188
+
189
+ by_group = {}
190
+ for elem in leftover:
191
+ by_group.setdefault(group(elem), []).append(elem)
192
+
193
+ new = set()
194
+ for key, elems in by_group.items():
195
+ rebuilt = text_of(elems)
196
+ if rebuilt and rebuilt == text_of(unmatched_original.get(key, [])):
197
+ continue
198
+ new.update(elems)
199
+ return new
200
+
201
+ def _generate_detailed_diff(self, differences):
202
+ error_parts = [
203
+ "FAILED - Document text doesn't match after removing the tracked changes",
204
+ "",
205
+ "Likely causes:",
206
+ " 1. Modified text inside another author's <w:ins> or <w:del> tags",
207
+ " 2. Made edits without proper tracked changes",
208
+ " 3. Didn't nest <w:del> inside <w:ins> when deleting another's insertion",
209
+ " 4. Rewrote another author's <w:ins>/<w:del> and changed its text on",
210
+ " the way. A tracked change from the original is recognised by its",
211
+ " author, date and text; anything that doesn't reproduce one exactly",
212
+ " reads as new, and the text it carried is reported missing.",
213
+ "",
214
+ "For pre-redlined documents, use correct patterns:",
215
+ " - To reject another's INSERTION: Nest <w:del> inside their <w:ins>",
216
+ " - To reject PART of one: nest <w:del> around only the runs you reject.",
217
+ " Their <w:ins> may be split around it, so long as the pieces keep",
218
+ " their author and date and still spell out the same text.",
219
+ " - To restore another's DELETION: Add new <w:ins> AFTER their <w:del>",
220
+ "",
221
+ ]
222
+
223
+ explain_page_fields = False
224
+ for part, original_text, modified_text in differences:
225
+ body = part == "word/document.xml"
226
+ heading = "Differences:" if body else f"Differences in {part}:"
227
+ if error_parts[-1]:
228
+ error_parts.append("")
229
+ git_diff = self._get_git_word_diff(original_text, modified_text)
230
+ if git_diff:
231
+ error_parts.extend([heading, "=" * len(heading), git_diff])
232
+ if re.search(r"\{(%s)\}" % "|".join(self.PAGE_FIELDS), git_diff):
233
+ explain_page_fields = True
234
+ else:
235
+ error_parts.extend(
236
+ [heading, "Unable to generate word diff (git not available)"]
237
+ )
238
+ if explain_page_fields:
239
+ error_parts.extend(["", self.PAGE_FIELD_NOTE])
240
+
241
+ return "\n".join(error_parts)
242
+
243
+ def _get_git_word_diff(self, original_text, modified_text):
244
+ try:
245
+ with tempfile.TemporaryDirectory() as temp_dir:
246
+ temp_path = Path(temp_dir)
247
+
248
+ original_file = temp_path / "original.txt"
249
+ modified_file = temp_path / "modified.txt"
250
+
251
+ original_file.write_text(original_text, encoding="utf-8")
252
+ modified_file.write_text(modified_text, encoding="utf-8")
253
+
254
+ result = subprocess.run(
255
+ [
256
+ "git",
257
+ "diff",
258
+ "--word-diff=plain",
259
+ "--word-diff-regex=.",
260
+ "-U0",
261
+ "--no-index",
262
+ str(original_file),
263
+ str(modified_file),
264
+ ],
265
+ capture_output=True,
266
+ encoding="utf-8",
267
+ errors="replace",
268
+ )
269
+
270
+ if result.stdout.strip():
271
+ lines = result.stdout.split("\n")
272
+ content_lines = []
273
+ in_content = False
274
+ for line in lines:
275
+ if line.startswith("@@"):
276
+ in_content = True
277
+ continue
278
+ if in_content and line.strip():
279
+ content_lines.append(line)
280
+
281
+ if content_lines:
282
+ return "\n".join(content_lines)
283
+
284
+ result = subprocess.run(
285
+ [
286
+ "git",
287
+ "diff",
288
+ "--word-diff=plain",
289
+ "-U0",
290
+ "--no-index",
291
+ str(original_file),
292
+ str(modified_file),
293
+ ],
294
+ capture_output=True,
295
+ encoding="utf-8",
296
+ errors="replace",
297
+ )
298
+
299
+ if result.stdout.strip():
300
+ lines = result.stdout.split("\n")
301
+ content_lines = []
302
+ in_content = False
303
+ for line in lines:
304
+ if line.startswith("@@"):
305
+ in_content = True
306
+ continue
307
+ if in_content and line.strip():
308
+ content_lines.append(line)
309
+ return "\n".join(content_lines)
310
+
311
+ except (subprocess.CalledProcessError, FileNotFoundError, Exception):
312
+ pass
313
+
314
+ return None
315
+
316
+ def _remove_tracked_changes(self, root, targets):
317
+ w = self.namespaces["w"]
318
+ ins_tag = f"{{{w}}}ins"
319
+ del_tag = f"{{{w}}}del"
320
+ move_from_tag = f"{{{w}}}moveFrom"
321
+ move_to_tag = f"{{{w}}}moveTo"
322
+
323
+ self._merge_split_paragraphs(root, targets)
324
+
325
+ for parent in root.iter():
326
+ to_remove = []
327
+ for child in parent:
328
+ if child.tag in (ins_tag, move_to_tag) and child in targets:
329
+ to_remove.append(child)
330
+ for elem in to_remove:
331
+ parent.remove(elem)
332
+
333
+ renamed = {
334
+ f"{{{w}}}delText": f"{{{w}}}t",
335
+ f"{{{w}}}delInstrText": f"{{{w}}}instrText",
336
+ }
337
+
338
+ for parent in root.iter():
339
+ to_process = []
340
+ for child in parent:
341
+ if child.tag in (del_tag, move_from_tag) and child in targets:
342
+ to_process.append((child, list(parent).index(child)))
343
+
344
+ for del_elem, del_index in reversed(to_process):
345
+ for elem in del_elem.iter():
346
+ elem.tag = renamed.get(elem.tag, elem.tag)
347
+
348
+ for child in reversed(list(del_elem)):
349
+ parent.insert(del_index, child)
350
+ parent.remove(del_elem)
351
+
352
+ range_tags = (
353
+ f"{{{w}}}moveFromRangeStart",
354
+ f"{{{w}}}moveFromRangeEnd",
355
+ f"{{{w}}}moveToRangeStart",
356
+ f"{{{w}}}moveToRangeEnd",
357
+ )
358
+ for parent in root.iter():
359
+ to_remove = [
360
+ c
361
+ for c in parent
362
+ if c.tag in range_tags and len(c) == 0 and not (c.text or "").strip()
363
+ ]
364
+ for elem in to_remove:
365
+ parent.remove(elem)
366
+
367
+ def _merge_split_paragraphs(self, root, targets):
368
+ w = self.namespaces["w"]
369
+ p_tag = f"{{{w}}}p"
370
+ ppr_tag = f"{{{w}}}pPr"
371
+ marker_path = f"{ppr_tag}/{{{w}}}rPr/{{{w}}}ins"
372
+
373
+ for parent in list(root.iter()):
374
+ for para in reversed([child for child in parent if child.tag == p_tag]):
375
+ if not any(m in targets for m in para.findall(marker_path)):
376
+ continue
377
+ siblings = list(parent)
378
+ index = siblings.index(para)
379
+ following = siblings[index + 1] if index + 1 < len(siblings) else None
380
+ if following is None or following.tag != p_tag:
381
+ continue
382
+ if any(self._text_elements(c) for c in para if c.tag == ppr_tag):
383
+ continue
384
+ insert_at = 1 if len(following) and following[0].tag == ppr_tag else 0
385
+ for child in reversed([c for c in para if c.tag != ppr_tag]):
386
+ following.insert(insert_at, child)
387
+ parent.remove(para)
388
+
389
+ def _extract_text_content(self, root):
390
+ p_tag = f"{{{self.namespaces['w']}}}p"
391
+
392
+ paragraphs = []
393
+ for p_elem in root.findall(f".//{p_tag}"):
394
+ paragraph_text = self._paragraph_text(p_elem)
395
+ if paragraph_text:
396
+ paragraphs.append(paragraph_text)
397
+
398
+ return "\n".join(paragraphs)
399
+
400
+ PAGE_FIELDS = {"PAGE", "NUMPAGES", "SECTIONPAGES"}
401
+ PAGE_NUMBER = re.compile(r"-? ?[0-9]{1,6} ?-?|[ivxlcdm]{1,8}|[a-z]{1,2}|", re.I)
402
+ PAGE_FIELD_NOTE = (
403
+ "Note: {PAGE}, {NUMPAGES} or {SECTIONPAGES} above stands for a page-number\n"
404
+ "field. Word recomputes the number it displays, so the number stored in\n"
405
+ "the file is not compared, but adding or removing the field still counts."
406
+ )
407
+
408
+ def _paragraph_text(self, p_elem):
409
+ w = self.namespaces["w"]
410
+ t_tag, instr_tag = f"{{{w}}}t", f"{{{w}}}instrText"
411
+ fld_char, fld_simple = f"{{{w}}}fldChar", f"{{{w}}}fldSimple"
412
+
413
+ def page_field(instruction, field):
414
+ words = instruction.split()
415
+ locked = field.get(f"{{{w}}}fldLock") in ("1", "true", "on")
416
+ if words and words[0].upper() in self.PAGE_FIELDS and not locked:
417
+ return words[0].upper()
418
+
419
+ def shown(name, result):
420
+ return "{%s}" % name if self.PAGE_NUMBER.fullmatch(result) else result
421
+
422
+ parts, fields = [], []
423
+ skipped = set()
424
+
425
+ def sink():
426
+ for *_, result in reversed(fields):
427
+ if result is not None:
428
+ return result
429
+ return parts
430
+
431
+ for elem in p_elem.iter():
432
+ if elem.tag == fld_simple:
433
+ name = page_field(elem.get(f"{{{w}}}instr", ""), elem)
434
+ if name:
435
+ texts = [self._rendered_text(t) for t in elem.iter(t_tag)]
436
+ sink().append(shown(name, "".join(texts).strip()))
437
+ skipped.update(elem.iter(t_tag))
438
+ elif elem.tag == fld_char:
439
+ kind = elem.get(f"{{{w}}}fldCharType")
440
+ if kind == "begin":
441
+ fields.append([elem, "", False, None])
442
+ elif kind == "separate" and fields and not fields[-1][2]:
443
+ fields[-1][2] = True
444
+ if page_field(fields[-1][1], fields[-1][0]):
445
+ fields[-1][3] = []
446
+ elif kind == "end" and fields:
447
+ begin, instruction, _, result = fields.pop()
448
+ name = page_field(instruction, begin)
449
+ if name:
450
+ sink().append(shown(name, "".join(result or []).strip()))
451
+ elif elem.tag == instr_tag and fields and not fields[-1][2]:
452
+ fields[-1][1] += elem.text or ""
453
+ elif elem.tag == t_tag and elem not in skipped:
454
+ sink().append(self._rendered_text(elem))
455
+ for *_, result in fields:
456
+ parts.extend(result or [])
457
+ return "".join(parts)
458
+
459
+
460
+ if __name__ == "__main__":
461
+ raise RuntimeError("This module should not be run directly.")