@iasiv5/dsh-skills 1.0.6 → 1.2.0

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 (192) hide show
  1. package/NOTICE.md +7 -10
  2. package/README.md +1 -1
  3. package/package.json +1 -1
  4. package/skills/answer-me-with-html/SKILL.md +3 -1
  5. package/skills/answer-me-with-html/references/settings.md +1 -1
  6. package/skills/answer-me-with-html/references/video.md +1 -0
  7. package/skills/answer-me-with-html/scripts/am.mjs +2567 -2064
  8. package/skills/docx/LICENSE.txt +202 -0
  9. package/skills/docx/SKILL.md +91 -0
  10. package/skills/docx/scripts/__init__.py +1 -0
  11. package/skills/docx/scripts/accept_changes.py +135 -0
  12. package/skills/docx/scripts/comment.py +368 -0
  13. package/skills/docx/scripts/merge_runs.py +310 -0
  14. package/skills/docx/scripts/office/helpers/__init__.py +111 -0
  15. package/skills/docx/scripts/office/helpers/pptx_chart.py +170 -0
  16. package/skills/docx/scripts/office/helpers/pptx_slide.py +60 -0
  17. package/skills/docx/scripts/office/helpers/pptx_theme.py +114 -0
  18. package/skills/docx/scripts/office/schemas/ISO-IEC29500-4_2016/dml-chart.xsd +1499 -0
  19. package/skills/docx/scripts/office/schemas/ISO-IEC29500-4_2016/dml-chartDrawing.xsd +146 -0
  20. package/skills/docx/scripts/office/schemas/ISO-IEC29500-4_2016/dml-diagram.xsd +1085 -0
  21. package/skills/docx/scripts/office/schemas/ISO-IEC29500-4_2016/dml-lockedCanvas.xsd +11 -0
  22. package/skills/docx/scripts/office/schemas/ISO-IEC29500-4_2016/dml-main.xsd +3081 -0
  23. package/skills/docx/scripts/office/schemas/ISO-IEC29500-4_2016/dml-picture.xsd +23 -0
  24. package/skills/docx/scripts/office/schemas/ISO-IEC29500-4_2016/dml-spreadsheetDrawing.xsd +185 -0
  25. package/skills/docx/scripts/office/schemas/ISO-IEC29500-4_2016/dml-wordprocessingDrawing.xsd +287 -0
  26. package/skills/docx/scripts/office/schemas/ISO-IEC29500-4_2016/pml.xsd +1676 -0
  27. package/skills/docx/scripts/office/schemas/ISO-IEC29500-4_2016/shared-additionalCharacteristics.xsd +28 -0
  28. package/skills/docx/scripts/office/schemas/ISO-IEC29500-4_2016/shared-bibliography.xsd +144 -0
  29. package/skills/docx/scripts/office/schemas/ISO-IEC29500-4_2016/shared-commonSimpleTypes.xsd +174 -0
  30. package/skills/docx/scripts/office/schemas/ISO-IEC29500-4_2016/shared-customXmlDataProperties.xsd +25 -0
  31. package/skills/docx/scripts/office/schemas/ISO-IEC29500-4_2016/shared-customXmlSchemaProperties.xsd +18 -0
  32. package/skills/docx/scripts/office/schemas/ISO-IEC29500-4_2016/shared-documentPropertiesCustom.xsd +59 -0
  33. package/skills/docx/scripts/office/schemas/ISO-IEC29500-4_2016/shared-documentPropertiesExtended.xsd +56 -0
  34. package/skills/docx/scripts/office/schemas/ISO-IEC29500-4_2016/shared-documentPropertiesVariantTypes.xsd +195 -0
  35. package/skills/docx/scripts/office/schemas/ISO-IEC29500-4_2016/shared-math.xsd +582 -0
  36. package/skills/docx/scripts/office/schemas/ISO-IEC29500-4_2016/shared-relationshipReference.xsd +25 -0
  37. package/skills/docx/scripts/office/schemas/ISO-IEC29500-4_2016/sml.xsd +4439 -0
  38. package/skills/docx/scripts/office/schemas/ISO-IEC29500-4_2016/vml-main.xsd +570 -0
  39. package/skills/docx/scripts/office/schemas/ISO-IEC29500-4_2016/vml-officeDrawing.xsd +509 -0
  40. package/skills/docx/scripts/office/schemas/ISO-IEC29500-4_2016/vml-presentationDrawing.xsd +12 -0
  41. package/skills/docx/scripts/office/schemas/ISO-IEC29500-4_2016/vml-spreadsheetDrawing.xsd +108 -0
  42. package/skills/docx/scripts/office/schemas/ISO-IEC29500-4_2016/vml-wordprocessingDrawing.xsd +96 -0
  43. package/skills/docx/scripts/office/schemas/ISO-IEC29500-4_2016/wml.xsd +3646 -0
  44. package/skills/docx/scripts/office/schemas/ISO-IEC29500-4_2016/xml.xsd +116 -0
  45. package/skills/docx/scripts/office/schemas/ecma/fouth-edition/opc-contentTypes.xsd +42 -0
  46. package/skills/docx/scripts/office/schemas/ecma/fouth-edition/opc-coreProperties.xsd +50 -0
  47. package/skills/docx/scripts/office/schemas/ecma/fouth-edition/opc-digSig.xsd +49 -0
  48. package/skills/docx/scripts/office/schemas/ecma/fouth-edition/opc-relationships.xsd +33 -0
  49. package/skills/docx/scripts/office/schemas/mce/mc.xsd +75 -0
  50. package/skills/docx/scripts/office/schemas/microsoft/wml-2010.xsd +560 -0
  51. package/skills/docx/scripts/office/schemas/microsoft/wml-2012.xsd +67 -0
  52. package/skills/docx/scripts/office/schemas/microsoft/wml-2018.xsd +14 -0
  53. package/skills/docx/scripts/office/schemas/microsoft/wml-cex-2018.xsd +20 -0
  54. package/skills/docx/scripts/office/schemas/microsoft/wml-cid-2016.xsd +13 -0
  55. package/skills/docx/scripts/office/schemas/microsoft/wml-sdtdatahash-2020.xsd +4 -0
  56. package/skills/docx/scripts/office/schemas/microsoft/wml-symex-2015.xsd +8 -0
  57. package/skills/docx/scripts/office/soffice.py +192 -0
  58. package/skills/docx/scripts/office/validate.py +173 -0
  59. package/skills/docx/scripts/office/validators/__init__.py +15 -0
  60. package/skills/docx/scripts/office/validators/base.py +875 -0
  61. package/skills/docx/scripts/office/validators/docx.py +466 -0
  62. package/skills/docx/scripts/office/validators/pptx.py +441 -0
  63. package/skills/docx/scripts/office/validators/redlining.py +299 -0
  64. package/skills/docx/scripts/templates/comments.xml +3 -0
  65. package/skills/docx/scripts/templates/commentsExtended.xml +3 -0
  66. package/skills/docx/scripts/templates/commentsExtensible.xml +3 -0
  67. package/skills/docx/scripts/templates/commentsIds.xml +3 -0
  68. package/skills/docx/scripts/templates/people.xml +3 -0
  69. package/skills/impeccable/SKILL.md +87 -0
  70. package/skills/impeccable/reference/adapt.md +318 -0
  71. package/skills/impeccable/reference/adapt.native.md +58 -0
  72. package/skills/impeccable/reference/android.md +46 -0
  73. package/skills/impeccable/reference/animate.md +89 -0
  74. package/skills/impeccable/reference/audit.md +150 -0
  75. package/skills/impeccable/reference/audit.native.md +143 -0
  76. package/skills/impeccable/reference/bolder.md +33 -0
  77. package/skills/impeccable/reference/clarify.md +94 -0
  78. package/skills/impeccable/reference/colorize.md +86 -0
  79. package/skills/impeccable/reference/component-review.md +63 -0
  80. package/skills/impeccable/reference/craft-floor.md +44 -0
  81. package/skills/impeccable/reference/craft.md +5 -0
  82. package/skills/impeccable/reference/critique.md +819 -0
  83. package/skills/impeccable/reference/degraded/asset-producer.md +42 -0
  84. package/skills/impeccable/reference/degraded/documenter.md +24 -0
  85. package/skills/impeccable/reference/degraded/finish-reviewer.md +38 -0
  86. package/skills/impeccable/reference/degraded/manual-edit-applier.md +93 -0
  87. package/skills/impeccable/reference/delight.md +70 -0
  88. package/skills/impeccable/reference/distill.md +111 -0
  89. package/skills/impeccable/reference/doctor.md +54 -0
  90. package/skills/impeccable/reference/document.md +416 -0
  91. package/skills/impeccable/reference/extract.md +69 -0
  92. package/skills/impeccable/reference/generate.md +101 -0
  93. package/skills/impeccable/reference/harden.md +362 -0
  94. package/skills/impeccable/reference/hooks.md +120 -0
  95. package/skills/impeccable/reference/init.md +133 -0
  96. package/skills/impeccable/reference/ios.md +51 -0
  97. package/skills/impeccable/reference/layout.md +84 -0
  98. package/skills/impeccable/reference/live-setup.md +104 -0
  99. package/skills/impeccable/reference/live.md +327 -0
  100. package/skills/impeccable/reference/mode-operate.md +21 -0
  101. package/skills/impeccable/reference/mode-persuade.md +19 -0
  102. package/skills/impeccable/reference/mode-read.md +21 -0
  103. package/skills/impeccable/reference/new-work.md +152 -0
  104. package/skills/impeccable/reference/onboard.md +235 -0
  105. package/skills/impeccable/reference/operate.md +61 -0
  106. package/skills/impeccable/reference/optimize.md +257 -0
  107. package/skills/impeccable/reference/overdrive.md +127 -0
  108. package/skills/impeccable/reference/polish.md +105 -0
  109. package/skills/impeccable/reference/quieter.md +99 -0
  110. package/skills/impeccable/reference/region-map.md +26 -0
  111. package/skills/impeccable/reference/routing.md +24 -0
  112. package/skills/impeccable/reference/shape.md +59 -0
  113. package/skills/impeccable/reference/typeset.md +90 -0
  114. package/skills/impeccable/reference/visualize.md +47 -0
  115. package/skills/impeccable/scripts/VERSION +1 -0
  116. package/skills/impeccable/scripts/command-metadata.json +98 -0
  117. package/skills/impeccable/scripts/data/font-index-failures.json +121 -0
  118. package/skills/impeccable/scripts/data/font-index.json +1 -0
  119. package/skills/impeccable/scripts/engine-checksums.txt +9 -0
  120. package/skills/impeccable/scripts/impeccable +291 -0
  121. package/skills/impeccable/scripts/impeccable.cmd +337 -0
  122. package/skills/impeccable/scripts/live-browser-dom.js +320 -0
  123. package/skills/impeccable/scripts/live-browser-ignores.js +246 -0
  124. package/skills/impeccable/scripts/live-browser-session.js +183 -0
  125. package/skills/impeccable/scripts/live-browser.js +13872 -0
  126. package/skills/impeccable/scripts/modern-screenshot.umd.js +14 -0
  127. package/skills/pdf/LICENSE.txt +202 -0
  128. package/skills/pdf/SKILL.md +314 -0
  129. package/skills/pdf/forms.md +294 -0
  130. package/skills/pdf/reference.md +612 -0
  131. package/skills/pdf/scripts/check_bounding_boxes.py +65 -0
  132. package/skills/pdf/scripts/check_fillable_fields.py +11 -0
  133. package/skills/pdf/scripts/convert_pdf_to_images.py +33 -0
  134. package/skills/pdf/scripts/create_validation_image.py +37 -0
  135. package/skills/pdf/scripts/extract_form_field_info.py +122 -0
  136. package/skills/pdf/scripts/extract_form_structure.py +115 -0
  137. package/skills/pdf/scripts/fill_fillable_fields.py +98 -0
  138. package/skills/pdf/scripts/fill_pdf_form_with_annotations.py +107 -0
  139. package/skills/xlsx/LICENSE.txt +202 -0
  140. package/skills/xlsx/SKILL.md +99 -0
  141. package/skills/xlsx/scripts/office/helpers/__init__.py +111 -0
  142. package/skills/xlsx/scripts/office/helpers/pptx_chart.py +170 -0
  143. package/skills/xlsx/scripts/office/helpers/pptx_slide.py +60 -0
  144. package/skills/xlsx/scripts/office/helpers/pptx_theme.py +114 -0
  145. package/skills/xlsx/scripts/office/schemas/ISO-IEC29500-4_2016/dml-chart.xsd +1499 -0
  146. package/skills/xlsx/scripts/office/schemas/ISO-IEC29500-4_2016/dml-chartDrawing.xsd +146 -0
  147. package/skills/xlsx/scripts/office/schemas/ISO-IEC29500-4_2016/dml-diagram.xsd +1085 -0
  148. package/skills/xlsx/scripts/office/schemas/ISO-IEC29500-4_2016/dml-lockedCanvas.xsd +11 -0
  149. package/skills/xlsx/scripts/office/schemas/ISO-IEC29500-4_2016/dml-main.xsd +3081 -0
  150. package/skills/xlsx/scripts/office/schemas/ISO-IEC29500-4_2016/dml-picture.xsd +23 -0
  151. package/skills/xlsx/scripts/office/schemas/ISO-IEC29500-4_2016/dml-spreadsheetDrawing.xsd +185 -0
  152. package/skills/xlsx/scripts/office/schemas/ISO-IEC29500-4_2016/dml-wordprocessingDrawing.xsd +287 -0
  153. package/skills/xlsx/scripts/office/schemas/ISO-IEC29500-4_2016/pml.xsd +1676 -0
  154. package/skills/xlsx/scripts/office/schemas/ISO-IEC29500-4_2016/shared-additionalCharacteristics.xsd +28 -0
  155. package/skills/xlsx/scripts/office/schemas/ISO-IEC29500-4_2016/shared-bibliography.xsd +144 -0
  156. package/skills/xlsx/scripts/office/schemas/ISO-IEC29500-4_2016/shared-commonSimpleTypes.xsd +174 -0
  157. package/skills/xlsx/scripts/office/schemas/ISO-IEC29500-4_2016/shared-customXmlDataProperties.xsd +25 -0
  158. package/skills/xlsx/scripts/office/schemas/ISO-IEC29500-4_2016/shared-customXmlSchemaProperties.xsd +18 -0
  159. package/skills/xlsx/scripts/office/schemas/ISO-IEC29500-4_2016/shared-documentPropertiesCustom.xsd +59 -0
  160. package/skills/xlsx/scripts/office/schemas/ISO-IEC29500-4_2016/shared-documentPropertiesExtended.xsd +56 -0
  161. package/skills/xlsx/scripts/office/schemas/ISO-IEC29500-4_2016/shared-documentPropertiesVariantTypes.xsd +195 -0
  162. package/skills/xlsx/scripts/office/schemas/ISO-IEC29500-4_2016/shared-math.xsd +582 -0
  163. package/skills/xlsx/scripts/office/schemas/ISO-IEC29500-4_2016/shared-relationshipReference.xsd +25 -0
  164. package/skills/xlsx/scripts/office/schemas/ISO-IEC29500-4_2016/sml.xsd +4439 -0
  165. package/skills/xlsx/scripts/office/schemas/ISO-IEC29500-4_2016/vml-main.xsd +570 -0
  166. package/skills/xlsx/scripts/office/schemas/ISO-IEC29500-4_2016/vml-officeDrawing.xsd +509 -0
  167. package/skills/xlsx/scripts/office/schemas/ISO-IEC29500-4_2016/vml-presentationDrawing.xsd +12 -0
  168. package/skills/xlsx/scripts/office/schemas/ISO-IEC29500-4_2016/vml-spreadsheetDrawing.xsd +108 -0
  169. package/skills/xlsx/scripts/office/schemas/ISO-IEC29500-4_2016/vml-wordprocessingDrawing.xsd +96 -0
  170. package/skills/xlsx/scripts/office/schemas/ISO-IEC29500-4_2016/wml.xsd +3646 -0
  171. package/skills/xlsx/scripts/office/schemas/ISO-IEC29500-4_2016/xml.xsd +116 -0
  172. package/skills/xlsx/scripts/office/schemas/ecma/fouth-edition/opc-contentTypes.xsd +42 -0
  173. package/skills/xlsx/scripts/office/schemas/ecma/fouth-edition/opc-coreProperties.xsd +50 -0
  174. package/skills/xlsx/scripts/office/schemas/ecma/fouth-edition/opc-digSig.xsd +49 -0
  175. package/skills/xlsx/scripts/office/schemas/ecma/fouth-edition/opc-relationships.xsd +33 -0
  176. package/skills/xlsx/scripts/office/schemas/mce/mc.xsd +75 -0
  177. package/skills/xlsx/scripts/office/schemas/microsoft/wml-2010.xsd +560 -0
  178. package/skills/xlsx/scripts/office/schemas/microsoft/wml-2012.xsd +67 -0
  179. package/skills/xlsx/scripts/office/schemas/microsoft/wml-2018.xsd +14 -0
  180. package/skills/xlsx/scripts/office/schemas/microsoft/wml-cex-2018.xsd +20 -0
  181. package/skills/xlsx/scripts/office/schemas/microsoft/wml-cid-2016.xsd +13 -0
  182. package/skills/xlsx/scripts/office/schemas/microsoft/wml-sdtdatahash-2020.xsd +4 -0
  183. package/skills/xlsx/scripts/office/schemas/microsoft/wml-symex-2015.xsd +8 -0
  184. package/skills/xlsx/scripts/office/soffice.py +192 -0
  185. package/skills/xlsx/scripts/office/validate.py +173 -0
  186. package/skills/xlsx/scripts/office/validators/__init__.py +15 -0
  187. package/skills/xlsx/scripts/office/validators/base.py +875 -0
  188. package/skills/xlsx/scripts/office/validators/docx.py +466 -0
  189. package/skills/xlsx/scripts/office/validators/pptx.py +441 -0
  190. package/skills/xlsx/scripts/office/validators/redlining.py +299 -0
  191. package/skills/xlsx/scripts/recalc.py +308 -0
  192. package/skills-manifest.json +36 -0
@@ -0,0 +1,466 @@
1
+ """
2
+ Validator for Word document XML files against XSD schemas.
3
+ """
4
+
5
+ import random
6
+ import re
7
+ import tempfile
8
+ import zipfile
9
+ from pathlib import Path
10
+
11
+ import defusedxml.minidom
12
+ import lxml.etree
13
+
14
+ from helpers import safe_extract
15
+
16
+ from .base import BaseSchemaValidator
17
+
18
+
19
+ class DOCXSchemaValidator(BaseSchemaValidator):
20
+
21
+ WORD_2006_NAMESPACE = "http://schemas.openxmlformats.org/wordprocessingml/2006/main"
22
+ W14_NAMESPACE = "http://schemas.microsoft.com/office/word/2010/wordml"
23
+ W16CID_NAMESPACE = "http://schemas.microsoft.com/office/word/2016/wordml/cid"
24
+
25
+ ELEMENT_RELATIONSHIP_TYPES = {}
26
+
27
+ def validate(self):
28
+ if not self.validate_xml():
29
+ return False
30
+
31
+ all_valid = True
32
+ if not self.validate_namespaces():
33
+ all_valid = False
34
+
35
+ if not self.validate_unique_ids():
36
+ all_valid = False
37
+
38
+ if not self.validate_file_references():
39
+ all_valid = False
40
+
41
+ if not self.validate_content_types():
42
+ all_valid = False
43
+
44
+ if not self.validate_against_xsd():
45
+ all_valid = False
46
+
47
+ if not self.validate_whitespace_preservation():
48
+ all_valid = False
49
+
50
+ if not self.validate_deletions():
51
+ all_valid = False
52
+
53
+ if not self.validate_insertions():
54
+ all_valid = False
55
+
56
+ if not self.validate_all_relationship_ids():
57
+ all_valid = False
58
+
59
+ if not self.validate_id_constraints():
60
+ all_valid = False
61
+
62
+ if not self.validate_comment_markers():
63
+ all_valid = False
64
+
65
+ self.compare_paragraph_counts()
66
+
67
+ return all_valid
68
+
69
+ def validate_whitespace_preservation(self):
70
+ errors = []
71
+
72
+ for xml_file in self.xml_files:
73
+ if xml_file.name != "document.xml":
74
+ continue
75
+
76
+ try:
77
+ root = lxml.etree.parse(str(xml_file)).getroot()
78
+
79
+ for elem in root.iter(f"{{{self.WORD_2006_NAMESPACE}}}t"):
80
+ if elem.text:
81
+ text = elem.text
82
+ if re.search(r"^[ \t\n\r]", text) or re.search(
83
+ r"[ \t\n\r]$", text
84
+ ):
85
+ xml_space_attr = f"{{{self.XML_NAMESPACE}}}space"
86
+ if (
87
+ xml_space_attr not in elem.attrib
88
+ or elem.attrib[xml_space_attr] != "preserve"
89
+ ):
90
+ text_preview = (
91
+ repr(text)[:50] + "..."
92
+ if len(repr(text)) > 50
93
+ else repr(text)
94
+ )
95
+ errors.append(
96
+ f" {xml_file.relative_to(self.unpacked_dir)}: "
97
+ f"Line {elem.sourceline}: w:t element with whitespace missing xml:space='preserve': {text_preview}"
98
+ )
99
+
100
+ except (lxml.etree.XMLSyntaxError, Exception) as e:
101
+ errors.append(
102
+ f" {xml_file.relative_to(self.unpacked_dir)}: Error: {e}"
103
+ )
104
+
105
+ if errors:
106
+ print(f"FAILED - Found {len(errors)} whitespace preservation violations:")
107
+ for error in errors:
108
+ print(error)
109
+ return False
110
+ else:
111
+ if self.verbose:
112
+ print("PASSED - All whitespace is properly preserved")
113
+ return True
114
+
115
+ def validate_deletions(self):
116
+ errors = []
117
+
118
+ for xml_file in self.xml_files:
119
+ if xml_file.name != "document.xml":
120
+ continue
121
+
122
+ try:
123
+ root = lxml.etree.parse(str(xml_file)).getroot()
124
+ namespaces = {"w": self.WORD_2006_NAMESPACE}
125
+
126
+ for t_elem in root.xpath(".//w:del//w:t", namespaces=namespaces):
127
+ if t_elem.text:
128
+ text_preview = (
129
+ repr(t_elem.text)[:50] + "..."
130
+ if len(repr(t_elem.text)) > 50
131
+ else repr(t_elem.text)
132
+ )
133
+ errors.append(
134
+ f" {xml_file.relative_to(self.unpacked_dir)}: "
135
+ f"Line {t_elem.sourceline}: <w:t> found within <w:del>: {text_preview}"
136
+ )
137
+
138
+ for instr_elem in root.xpath(
139
+ ".//w:del//w:instrText", namespaces=namespaces
140
+ ):
141
+ text_preview = (
142
+ repr(instr_elem.text or "")[:50] + "..."
143
+ if len(repr(instr_elem.text or "")) > 50
144
+ else repr(instr_elem.text or "")
145
+ )
146
+ errors.append(
147
+ f" {xml_file.relative_to(self.unpacked_dir)}: "
148
+ f"Line {instr_elem.sourceline}: <w:instrText> found within <w:del> (use <w:delInstrText>): {text_preview}"
149
+ )
150
+
151
+ except (lxml.etree.XMLSyntaxError, Exception) as e:
152
+ errors.append(
153
+ f" {xml_file.relative_to(self.unpacked_dir)}: Error: {e}"
154
+ )
155
+
156
+ if errors:
157
+ print(f"FAILED - Found {len(errors)} deletion validation violations:")
158
+ for error in errors:
159
+ print(error)
160
+ return False
161
+ else:
162
+ if self.verbose:
163
+ print("PASSED - No w:t elements found within w:del elements")
164
+ return True
165
+
166
+ def count_paragraphs_in_unpacked(self):
167
+ count = 0
168
+
169
+ for xml_file in self.xml_files:
170
+ if xml_file.name != "document.xml":
171
+ continue
172
+
173
+ try:
174
+ root = lxml.etree.parse(str(xml_file)).getroot()
175
+ paragraphs = root.findall(f".//{{{self.WORD_2006_NAMESPACE}}}p")
176
+ count = len(paragraphs)
177
+ except Exception as e:
178
+ print(f"Error counting paragraphs in unpacked document: {e}")
179
+
180
+ return count
181
+
182
+ def count_paragraphs_in_original(self):
183
+ original = self.original_file
184
+ if original is None:
185
+ return 0
186
+
187
+ count = 0
188
+
189
+ try:
190
+ with tempfile.TemporaryDirectory() as temp_dir:
191
+ with zipfile.ZipFile(original, "r") as zip_ref:
192
+ safe_extract(zip_ref, Path(temp_dir))
193
+
194
+ doc_xml_path = temp_dir + "/word/document.xml"
195
+ root = lxml.etree.parse(doc_xml_path).getroot()
196
+
197
+ paragraphs = root.findall(f".//{{{self.WORD_2006_NAMESPACE}}}p")
198
+ count = len(paragraphs)
199
+
200
+ except Exception as e:
201
+ print(f"Error counting paragraphs in original document: {e}")
202
+
203
+ return count
204
+
205
+ def validate_insertions(self):
206
+ errors = []
207
+
208
+ for xml_file in self.xml_files:
209
+ if xml_file.name != "document.xml":
210
+ continue
211
+
212
+ try:
213
+ root = lxml.etree.parse(str(xml_file)).getroot()
214
+ namespaces = {"w": self.WORD_2006_NAMESPACE}
215
+
216
+ invalid_elements = root.xpath(
217
+ ".//w:ins//w:delText[not(ancestor::w:del)]", namespaces=namespaces
218
+ )
219
+
220
+ for elem in invalid_elements:
221
+ text_preview = (
222
+ repr(elem.text or "")[:50] + "..."
223
+ if len(repr(elem.text or "")) > 50
224
+ else repr(elem.text or "")
225
+ )
226
+ errors.append(
227
+ f" {xml_file.relative_to(self.unpacked_dir)}: "
228
+ f"Line {elem.sourceline}: <w:delText> within <w:ins>: {text_preview}"
229
+ )
230
+
231
+ except (lxml.etree.XMLSyntaxError, Exception) as e:
232
+ errors.append(
233
+ f" {xml_file.relative_to(self.unpacked_dir)}: Error: {e}"
234
+ )
235
+
236
+ if errors:
237
+ print(f"FAILED - Found {len(errors)} insertion validation violations:")
238
+ for error in errors:
239
+ print(error)
240
+ return False
241
+ else:
242
+ if self.verbose:
243
+ print("PASSED - No w:delText elements within w:ins elements")
244
+ return True
245
+
246
+ def compare_paragraph_counts(self):
247
+ new_count = self.count_paragraphs_in_unpacked()
248
+ if self.original_file is None:
249
+ print(f"\nParagraphs: {new_count}")
250
+ return
251
+
252
+ original_count = self.count_paragraphs_in_original()
253
+ diff = new_count - original_count
254
+ diff_str = f"+{diff}" if diff > 0 else str(diff)
255
+ print(f"\nParagraphs: {original_count} → {new_count} ({diff_str})")
256
+
257
+ def _parse_id_value(self, val: str, base: int = 16) -> int:
258
+ return int(val, base)
259
+
260
+ def validate_id_constraints(self):
261
+ errors = []
262
+ para_id_attr = f"{{{self.W14_NAMESPACE}}}paraId"
263
+ durable_id_attr = f"{{{self.W16CID_NAMESPACE}}}durableId"
264
+
265
+ for xml_file in self.xml_files:
266
+ try:
267
+ for elem in lxml.etree.parse(str(xml_file)).iter():
268
+ if val := elem.get(para_id_attr):
269
+ try:
270
+ if self._parse_id_value(val, base=16) >= 0x80000000:
271
+ errors.append(
272
+ f" {xml_file.name}:{elem.sourceline}: paraId={val} >= 0x80000000"
273
+ )
274
+ except ValueError:
275
+ errors.append(
276
+ f" {xml_file.name}:{elem.sourceline}: "
277
+ f"paraId={val} is not valid hex"
278
+ )
279
+
280
+ if val := elem.get(durable_id_attr):
281
+ if xml_file.name == "numbering.xml":
282
+ try:
283
+ if self._parse_id_value(val, base=10) >= 0x7FFFFFFF:
284
+ errors.append(
285
+ f" {xml_file.name}:{elem.sourceline}: "
286
+ f"durableId={val} >= 0x7FFFFFFF"
287
+ )
288
+ except ValueError:
289
+ errors.append(
290
+ f" {xml_file.name}:{elem.sourceline}: "
291
+ f"durableId={val} must be decimal in numbering.xml"
292
+ )
293
+ else:
294
+ try:
295
+ if self._parse_id_value(val, base=16) >= 0x7FFFFFFF:
296
+ errors.append(
297
+ f" {xml_file.name}:{elem.sourceline}: "
298
+ f"durableId={val} >= 0x7FFFFFFF"
299
+ )
300
+ except ValueError:
301
+ errors.append(
302
+ f" {xml_file.name}:{elem.sourceline}: "
303
+ f"durableId={val} is not valid hex"
304
+ )
305
+ except lxml.etree.XMLSyntaxError:
306
+ continue
307
+
308
+ if errors:
309
+ print(f"FAILED - {len(errors)} ID constraint violations:")
310
+ for e in errors:
311
+ print(e)
312
+ elif self.verbose:
313
+ print("PASSED - All paraId/durableId values within constraints")
314
+ return not errors
315
+
316
+ def validate_comment_markers(self):
317
+ errors = []
318
+
319
+ document_xml = None
320
+ comments_xml = None
321
+ for xml_file in self.xml_files:
322
+ if xml_file.name == "document.xml" and "word" in str(xml_file):
323
+ document_xml = xml_file
324
+ elif xml_file.name == "comments.xml":
325
+ comments_xml = xml_file
326
+
327
+ if not document_xml:
328
+ if self.verbose:
329
+ print("PASSED - No document.xml found (skipping comment validation)")
330
+ return True
331
+
332
+ try:
333
+ doc_root = lxml.etree.parse(str(document_xml)).getroot()
334
+ namespaces = {"w": self.WORD_2006_NAMESPACE}
335
+
336
+ range_starts = {
337
+ elem.get(f"{{{self.WORD_2006_NAMESPACE}}}id")
338
+ for elem in doc_root.xpath(
339
+ ".//w:commentRangeStart", namespaces=namespaces
340
+ )
341
+ }
342
+ range_ends = {
343
+ elem.get(f"{{{self.WORD_2006_NAMESPACE}}}id")
344
+ for elem in doc_root.xpath(
345
+ ".//w:commentRangeEnd", namespaces=namespaces
346
+ )
347
+ }
348
+ references = {
349
+ elem.get(f"{{{self.WORD_2006_NAMESPACE}}}id")
350
+ for elem in doc_root.xpath(
351
+ ".//w:commentReference", namespaces=namespaces
352
+ )
353
+ }
354
+
355
+ orphaned_ends = range_ends - range_starts
356
+ for comment_id in sorted(
357
+ orphaned_ends, key=lambda x: int(x) if x and x.isdigit() else 0
358
+ ):
359
+ errors.append(
360
+ f' document.xml: commentRangeEnd id="{comment_id}" has no matching commentRangeStart'
361
+ )
362
+
363
+ orphaned_starts = range_starts - range_ends
364
+ for comment_id in sorted(
365
+ orphaned_starts, key=lambda x: int(x) if x and x.isdigit() else 0
366
+ ):
367
+ errors.append(
368
+ f' document.xml: commentRangeStart id="{comment_id}" has no matching commentRangeEnd'
369
+ )
370
+
371
+ comment_ids = set()
372
+ if comments_xml and comments_xml.exists():
373
+ comments_root = lxml.etree.parse(str(comments_xml)).getroot()
374
+ comment_ids = {
375
+ elem.get(f"{{{self.WORD_2006_NAMESPACE}}}id")
376
+ for elem in comments_root.xpath(
377
+ ".//w:comment", namespaces=namespaces
378
+ )
379
+ }
380
+
381
+ marker_ids = range_starts | range_ends | references
382
+ invalid_refs = marker_ids - comment_ids
383
+ for comment_id in sorted(
384
+ invalid_refs, key=lambda x: int(x) if x and x.isdigit() else 0
385
+ ):
386
+ if comment_id:
387
+ errors.append(
388
+ f' document.xml: marker id="{comment_id}" references non-existent comment'
389
+ )
390
+
391
+ except (lxml.etree.XMLSyntaxError, Exception) as e:
392
+ errors.append(f" Error parsing XML: {e}")
393
+
394
+ if errors:
395
+ print(f"FAILED - {len(errors)} comment marker violations:")
396
+ for error in errors:
397
+ print(error)
398
+ return False
399
+ else:
400
+ if self.verbose:
401
+ print("PASSED - All comment markers properly paired")
402
+ return True
403
+
404
+ def repair(self) -> int:
405
+ repairs = super().repair()
406
+ repairs += self.repair_durableId()
407
+ return repairs
408
+
409
+ def repair_durableId(self) -> int:
410
+ DURABLE_ID_ATTRS = ("w16cid:durableId", "w16cex:durableId")
411
+ repairs = 0
412
+ renames: dict = {}
413
+
414
+ for xml_file in self.xml_files:
415
+ try:
416
+ content = xml_file.read_text(encoding="utf-8")
417
+ dom = defusedxml.minidom.parseString(content)
418
+ is_numbering = xml_file.name == "numbering.xml"
419
+ base = 10 if is_numbering else 16
420
+ pending = []
421
+ seen_in_file = set()
422
+ modified = False
423
+
424
+ for elem in dom.getElementsByTagName("*"):
425
+ for attr_name in DURABLE_ID_ATTRS:
426
+ if not elem.hasAttribute(attr_name):
427
+ continue
428
+
429
+ durable_id = elem.getAttribute(attr_name)
430
+ try:
431
+ key = self._parse_id_value(durable_id, base=base)
432
+ needs_repair = key >= 0x7FFFFFFF
433
+ except ValueError:
434
+ key = durable_id
435
+ needs_repair = True
436
+
437
+ if needs_repair:
438
+ if key in seen_in_file:
439
+ value = random.randint(1, 0x7FFFFFFE)
440
+ else:
441
+ seen_in_file.add(key)
442
+ if key not in renames:
443
+ renames[key] = random.randint(1, 0x7FFFFFFE)
444
+ value = renames[key]
445
+ new_id = str(value) if is_numbering else f"{value:08X}"
446
+
447
+ elem.setAttribute(attr_name, new_id)
448
+ pending.append(
449
+ f" Repaired: {xml_file.name}: durableId {durable_id} → {new_id}"
450
+ )
451
+ modified = True
452
+
453
+ if modified:
454
+ xml_file.write_bytes(dom.toxml(encoding="UTF-8"))
455
+ for message in pending:
456
+ print(message)
457
+ repairs += len(pending)
458
+
459
+ except Exception:
460
+ pass
461
+
462
+ return repairs
463
+
464
+
465
+ if __name__ == "__main__":
466
+ raise RuntimeError("This module should not be run directly.")