@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,309 @@
1
+ """Remove unreferenced files from an unpacked PPTX directory.
2
+
3
+ Usage: python clean.py <unpacked_dir>
4
+
5
+ Example:
6
+ python clean.py unpacked/
7
+
8
+ This script removes:
9
+ - Orphaned slides (not in sldIdLst) and their relationships
10
+ - [trash] directory (unreferenced files)
11
+ - Orphaned .rels files for deleted resources
12
+ - Unreferenced media, embeddings, charts, diagrams, drawings, ink files
13
+ - Unreferenced theme files
14
+ - Unreferenced notes slides
15
+ - Content-Type overrides for deleted files
16
+ """
17
+
18
+ import posixpath
19
+ import re
20
+ import sys
21
+ from pathlib import Path
22
+
23
+ import defusedxml.minidom
24
+
25
+ from office.helpers import SLIDE_REL_TYPE, opc_target, rels_source_part
26
+
27
+
28
+ def _slide_rids(pres_rels_path: Path, unpacked_dir: Path) -> dict[str, str]:
29
+ source_part = rels_source_part(pres_rels_path, unpacked_dir)
30
+ rels_dom = defusedxml.minidom.parse(str(pres_rels_path))
31
+
32
+ rids: dict[str, str] = {}
33
+ for rel in rels_dom.getElementsByTagName("Relationship"):
34
+ if rel.getAttribute("Type") != SLIDE_REL_TYPE:
35
+ continue
36
+ part = opc_target(
37
+ rel.getAttribute("Target"), source_part, rel.getAttribute("TargetMode")
38
+ )
39
+ if part is not None:
40
+ rids[rel.getAttribute("Id")] = part
41
+ return rids
42
+
43
+
44
+ def get_slides_in_sldidlst(unpacked_dir: Path) -> set[str]:
45
+ pres_path = unpacked_dir / "ppt" / "presentation.xml"
46
+ pres_rels_path = unpacked_dir / "ppt" / "_rels" / "presentation.xml.rels"
47
+
48
+ if not pres_path.exists() or not pres_rels_path.exists():
49
+ return set()
50
+
51
+ rid_to_slide = _slide_rids(pres_rels_path, unpacked_dir)
52
+
53
+ pres_content = pres_path.read_text(encoding="utf-8")
54
+ referenced_rids = set(re.findall(r'<p:sldId[^>]*r:id="([^"]+)"', pres_content))
55
+
56
+ return {
57
+ posixpath.basename(rid_to_slide[rid])
58
+ for rid in referenced_rids
59
+ if rid in rid_to_slide
60
+ }
61
+
62
+
63
+ class RefusedToClean(Exception):
64
+ """The package does not look the way a readable package should."""
65
+
66
+
67
+ def remove_orphaned_slides(unpacked_dir: Path) -> list[str]:
68
+ slides_dir = unpacked_dir / "ppt" / "slides"
69
+ slides_rels_dir = slides_dir / "_rels"
70
+ pres_rels_path = unpacked_dir / "ppt" / "_rels" / "presentation.xml.rels"
71
+
72
+ if not slides_dir.exists():
73
+ return []
74
+
75
+ referenced_slides = get_slides_in_sldidlst(unpacked_dir)
76
+ on_disk = sorted(slides_dir.glob("slide*.xml"))
77
+
78
+ if on_disk and not any(s.name in referenced_slides for s in on_disk):
79
+ listed = re.findall(
80
+ r'<p:sldId[^>]*r:id="([^"]+)"',
81
+ (unpacked_dir / "ppt" / "presentation.xml").read_text(encoding="utf-8")
82
+ if (unpacked_dir / "ppt" / "presentation.xml").exists()
83
+ else "",
84
+ )
85
+ if listed:
86
+ raise RefusedToClean(
87
+ f"<p:sldIdLst> lists {len(listed)} slide(s) and none of the "
88
+ f"{len(on_disk)} slide(s) on disk match any of them. Refusing to "
89
+ f"delete them all — this is a parse failure, not an empty deck."
90
+ )
91
+
92
+ removed = []
93
+
94
+ for slide_file in on_disk:
95
+ if slide_file.name not in referenced_slides:
96
+ rel_path = slide_file.relative_to(unpacked_dir)
97
+ slide_file.unlink()
98
+ removed.append(str(rel_path))
99
+
100
+ rels_file = slides_rels_dir / f"{slide_file.name}.rels"
101
+ if rels_file.exists():
102
+ rels_file.unlink()
103
+ removed.append(str(rels_file.relative_to(unpacked_dir)))
104
+
105
+ if removed and pres_rels_path.exists():
106
+ rels_dom = defusedxml.minidom.parse(str(pres_rels_path))
107
+ source_part = rels_source_part(pres_rels_path, unpacked_dir)
108
+ changed = False
109
+
110
+ for rel in list(rels_dom.getElementsByTagName("Relationship")):
111
+ if rel.getAttribute("Type") != SLIDE_REL_TYPE:
112
+ continue
113
+ part = opc_target(
114
+ rel.getAttribute("Target"), source_part, rel.getAttribute("TargetMode")
115
+ )
116
+ if part is None:
117
+ continue
118
+ if posixpath.basename(part) not in referenced_slides:
119
+ if rel.parentNode:
120
+ rel.parentNode.removeChild(rel)
121
+ changed = True
122
+
123
+ if changed:
124
+ with open(pres_rels_path, "wb") as f:
125
+ f.write(rels_dom.toxml(encoding="utf-8"))
126
+
127
+ return removed
128
+
129
+
130
+ def remove_trash_directory(unpacked_dir: Path) -> list[str]:
131
+ trash_dir = unpacked_dir / "[trash]"
132
+ removed = []
133
+
134
+ if trash_dir.exists() and trash_dir.is_dir():
135
+ for file_path in trash_dir.iterdir():
136
+ if file_path.is_file():
137
+ rel_path = file_path.relative_to(unpacked_dir)
138
+ removed.append(str(rel_path))
139
+ file_path.unlink()
140
+ trash_dir.rmdir()
141
+
142
+ return removed
143
+
144
+
145
+ def _referenced_by(rels_files, unpacked_dir: Path) -> set:
146
+ referenced = set()
147
+
148
+ for rels_file in rels_files:
149
+ source_part = rels_source_part(rels_file, unpacked_dir)
150
+ dom = defusedxml.minidom.parse(str(rels_file))
151
+ for rel in dom.getElementsByTagName("Relationship"):
152
+ part = opc_target(
153
+ rel.getAttribute("Target"), source_part, rel.getAttribute("TargetMode")
154
+ )
155
+ if part is not None:
156
+ referenced.add(Path(part))
157
+
158
+ return referenced
159
+
160
+
161
+ def remove_orphaned_rels_files(unpacked_dir: Path) -> list[str]:
162
+ resource_dirs = ["charts", "diagrams", "drawings"]
163
+ removed = []
164
+
165
+ for dir_name in resource_dirs:
166
+ rels_dir = unpacked_dir / "ppt" / dir_name / "_rels"
167
+ if not rels_dir.exists():
168
+ continue
169
+
170
+ for rels_file in rels_dir.glob("*.rels"):
171
+ resource_file = rels_dir.parent / rels_file.name.replace(".rels", "")
172
+ if not resource_file.exists():
173
+ rels_file.unlink()
174
+ removed.append(str(rels_file.relative_to(unpacked_dir)))
175
+
176
+ return removed
177
+
178
+
179
+ def get_referenced_files(unpacked_dir: Path) -> set:
180
+ return _referenced_by(sorted(unpacked_dir.rglob("*.rels")), unpacked_dir)
181
+
182
+
183
+ def remove_orphaned_files(unpacked_dir: Path, referenced: set) -> list[str]:
184
+ resource_dirs = ["media", "embeddings", "charts", "diagrams", "tags", "drawings", "ink"]
185
+ removed = []
186
+
187
+ for dir_name in resource_dirs:
188
+ dir_path = unpacked_dir / "ppt" / dir_name
189
+ if not dir_path.exists():
190
+ continue
191
+
192
+ for file_path in dir_path.glob("*"):
193
+ if not file_path.is_file():
194
+ continue
195
+ rel_path = file_path.relative_to(unpacked_dir)
196
+ if rel_path not in referenced:
197
+ file_path.unlink()
198
+ removed.append(str(rel_path))
199
+
200
+ theme_dir = unpacked_dir / "ppt" / "theme"
201
+ if theme_dir.exists():
202
+ for file_path in theme_dir.glob("theme*.xml"):
203
+ rel_path = file_path.relative_to(unpacked_dir)
204
+ if rel_path not in referenced:
205
+ file_path.unlink()
206
+ removed.append(str(rel_path))
207
+ theme_rels = theme_dir / "_rels" / f"{file_path.name}.rels"
208
+ if theme_rels.exists():
209
+ theme_rels.unlink()
210
+ removed.append(str(theme_rels.relative_to(unpacked_dir)))
211
+
212
+ notes_dir = unpacked_dir / "ppt" / "notesSlides"
213
+ if notes_dir.exists():
214
+ for file_path in notes_dir.glob("*.xml"):
215
+ if not file_path.is_file():
216
+ continue
217
+ rel_path = file_path.relative_to(unpacked_dir)
218
+ if rel_path not in referenced:
219
+ file_path.unlink()
220
+ removed.append(str(rel_path))
221
+
222
+ notes_rels_dir = notes_dir / "_rels"
223
+ if notes_rels_dir.exists():
224
+ for file_path in notes_rels_dir.glob("*.rels"):
225
+ notes_file = notes_dir / file_path.name.replace(".rels", "")
226
+ if not notes_file.exists():
227
+ file_path.unlink()
228
+ removed.append(str(file_path.relative_to(unpacked_dir)))
229
+
230
+ return removed
231
+
232
+
233
+ def update_content_types(unpacked_dir: Path, removed_files: list[str]) -> None:
234
+ ct_path = unpacked_dir / "[Content_Types].xml"
235
+ if not ct_path.exists():
236
+ return
237
+
238
+ dom = defusedxml.minidom.parse(str(ct_path))
239
+ changed = False
240
+
241
+ for override in list(dom.getElementsByTagName("Override")):
242
+ part_name = override.getAttribute("PartName").lstrip("/")
243
+ if part_name in removed_files:
244
+ if override.parentNode:
245
+ override.parentNode.removeChild(override)
246
+ changed = True
247
+
248
+ if changed:
249
+ with open(ct_path, "wb") as f:
250
+ f.write(dom.toxml(encoding="utf-8"))
251
+
252
+
253
+ def clean_unused_files(unpacked_dir: Path) -> list[str]:
254
+ all_removed = []
255
+
256
+ if list(unpacked_dir.rglob("*.rels")) and not get_referenced_files(unpacked_dir):
257
+ raise RefusedToClean(
258
+ "no relationship in this package names a part we can resolve. "
259
+ "Refusing to treat every file as unreferenced."
260
+ )
261
+
262
+ slides_removed = remove_orphaned_slides(unpacked_dir)
263
+ all_removed.extend(slides_removed)
264
+
265
+ trash_removed = remove_trash_directory(unpacked_dir)
266
+ all_removed.extend(trash_removed)
267
+
268
+ while True:
269
+ removed_rels = remove_orphaned_rels_files(unpacked_dir)
270
+ referenced = get_referenced_files(unpacked_dir)
271
+ removed_files = remove_orphaned_files(unpacked_dir, referenced)
272
+
273
+ total_removed = removed_rels + removed_files
274
+ if not total_removed:
275
+ break
276
+
277
+ all_removed.extend(total_removed)
278
+
279
+ if all_removed:
280
+ update_content_types(unpacked_dir, all_removed)
281
+
282
+ return all_removed
283
+
284
+
285
+ if __name__ == "__main__":
286
+ if len(sys.argv) != 2:
287
+ print("Usage: python clean.py <unpacked_dir>", file=sys.stderr)
288
+ print("Example: python clean.py unpacked/", file=sys.stderr)
289
+ sys.exit(1)
290
+
291
+ unpacked_dir = Path(sys.argv[1])
292
+
293
+ if not unpacked_dir.exists():
294
+ print(f"Error: {unpacked_dir} not found", file=sys.stderr)
295
+ sys.exit(1)
296
+
297
+ try:
298
+ removed = clean_unused_files(unpacked_dir)
299
+ except (RefusedToClean, ValueError) as e:
300
+ print(f"Error: {e}", file=sys.stderr)
301
+ print("Nothing was deleted.", file=sys.stderr)
302
+ sys.exit(1)
303
+
304
+ if removed:
305
+ print(f"Removed {len(removed)} unreferenced files:")
306
+ for f in removed:
307
+ print(f" {f}")
308
+ else:
309
+ print("No unreferenced files found")
@@ -0,0 +1,121 @@
1
+ import os
2
+ import posixpath
3
+ import re
4
+ import stat
5
+ import tempfile
6
+ import urllib.parse
7
+ import zipfile
8
+ from pathlib import Path
9
+
10
+ OOXML_FAMILY = {
11
+ ".docx": "docx",
12
+ ".dotx": "docx",
13
+ ".pptx": "pptx",
14
+ ".potx": "pptx",
15
+ ".xlsx": "xlsx",
16
+ ".xltx": "xlsx",
17
+ }
18
+
19
+ _SCHEME_RE = re.compile(r"^[A-Za-z][A-Za-z0-9+.\-]*:")
20
+
21
+ SLIDE_REL_TYPE = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/slide"
22
+
23
+
24
+ def one_line(text: str, limit: int) -> str:
25
+ return "".join(c if c.isprintable() else c.encode("unicode_escape").decode() for c in text)[:limit]
26
+
27
+
28
+ def opc_target(target: str, source_part: str, target_mode: str = "") -> str | None:
29
+ if not target:
30
+ return None
31
+ if target_mode.lower() == "external":
32
+ return None
33
+ target = urllib.parse.unquote(target)
34
+
35
+ if _SCHEME_RE.match(target):
36
+ return None
37
+
38
+ if "\\" in target:
39
+ raise ValueError(f"relationship target is not a POSIX part name: {target!r}")
40
+
41
+ if target.startswith("/"):
42
+ joined = target.lstrip("/")
43
+ else:
44
+ joined = posixpath.join(posixpath.dirname(source_part), target)
45
+
46
+ parts: list[str] = []
47
+ for segment in posixpath.normpath(joined).split("/"):
48
+ if segment in ("", "."):
49
+ continue
50
+ if segment == "..":
51
+ if not parts:
52
+ raise ValueError(f"relationship target escapes the package: {target!r}")
53
+ parts.pop()
54
+ else:
55
+ parts.append(segment)
56
+
57
+ if not parts:
58
+ raise ValueError(f"relationship target resolves to nothing: {target!r}")
59
+ return "/".join(parts)
60
+
61
+
62
+ def rels_source_part(rels_file: Path, unpacked_dir: Path) -> str:
63
+ owner_dir = rels_file.parent.parent.relative_to(unpacked_dir)
64
+ part = posixpath.join(owner_dir.as_posix(), rels_file.name[: -len(".rels")])
65
+ return part.removeprefix("./")
66
+
67
+
68
+ def part_text(data: bytes) -> str:
69
+ return data.decode("utf-8", "surrogateescape")
70
+
71
+
72
+ XML_SPACE = " \t\r\n"
73
+
74
+
75
+ def rendered_text(text: str, preserve: bool) -> str:
76
+ return text if preserve else text.strip(XML_SPACE)
77
+
78
+
79
+ def safe_extract(zf: zipfile.ZipFile, dest: Path) -> None:
80
+ dest = dest.resolve()
81
+ for m in zf.infolist():
82
+ if stat.S_ISLNK(m.external_attr >> 16):
83
+ raise ValueError(f"symlink archive entry not allowed: {m.filename!r}")
84
+ target = (dest / m.filename).resolve()
85
+ if not target.is_relative_to(dest):
86
+ raise ValueError(f"unsafe archive entry: {m.filename!r}")
87
+ zf.extract(m, dest)
88
+
89
+
90
+ def folder_entries(package: Path) -> list[str]:
91
+ with zipfile.ZipFile(package) as zf:
92
+ return [info.filename for info in zf.infolist() if info.is_dir()]
93
+
94
+
95
+ def rezip(src_dir: Path, out_path: Path) -> None:
96
+ files = sorted(p for p in src_dir.rglob("*") if p.is_file())
97
+ ct = src_dir / "[Content_Types].xml"
98
+ fd, tmp_name = tempfile.mkstemp(
99
+ prefix=out_path.name + ".", suffix=".tmp", dir=out_path.parent
100
+ )
101
+ tmp_out = Path(tmp_name)
102
+ try:
103
+ with os.fdopen(fd, "wb") as fh:
104
+ with zipfile.ZipFile(fh, "w", zipfile.ZIP_DEFLATED) as zf:
105
+ if ct.exists():
106
+ zf.write(ct, ct.relative_to(src_dir), compress_type=zipfile.ZIP_STORED)
107
+ for f in files:
108
+ if f == ct:
109
+ continue
110
+ zf.write(f, f.relative_to(src_dir))
111
+ if out_path.exists():
112
+ mode = out_path.stat().st_mode & 0o777
113
+ else:
114
+ umask = os.umask(0)
115
+ os.umask(umask)
116
+ mode = 0o666 & ~umask
117
+ os.chmod(tmp_out, mode)
118
+ os.replace(tmp_out, out_path)
119
+ finally:
120
+ if tmp_out.exists():
121
+ tmp_out.unlink()