Skillfarm / skills · skillfarm.ai / converted / docx / scripts / office / helpers / pptx_theme.py
| 1 | """Find masters sharing a theme part in the way PowerPoint refuses to open. |
| 2 | |
| 3 | Reports only; the fix is to move <p:notesMasterIdLst> back to directly after |
| 4 | <p:sldIdLst> in ppt/presentation.xml. |
| 5 | """ |
| 6 | |
| 7 | |
| 8 | from __future__ import annotations |
| 9 | |
| 10 | import posixpath |
| 11 | import re |
| 12 | from typing import Mapping |
| 13 | |
| 14 | from . import part_text |
| 15 | |
| 16 | THEME_REL_TYPE = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme" |
| 17 | |
| 18 | _MASTER_RE = re.compile( |
| 19 | r"^ppt/(?P<group>slideMasters|notesMasters|handoutMasters)/" |
| 20 | r"(?:slide|notes|handout)Master(?P<num>\d+)\.xml$" |
| 21 | ) |
| 22 | _GROUP_ORDER = {"slideMasters": 0, "notesMasters": 1, "handoutMasters": 2} |
| 23 | |
| 24 | _RELATIONSHIP_RE = re.compile( |
| 25 | r"<Relationship\b[^>]*?(?:/>|>.*?</Relationship\s*>)", re.DOTALL |
| 26 | ) |
| 27 | |
| 28 | |
| 29 | def _sort_key(name: str) -> tuple[int, int]: |
| 30 | m = _MASTER_RE.match(name) |
| 31 | assert m is not None |
| 32 | return (_GROUP_ORDER[m.group("group")], int(m.group("num"))) |
| 33 | |
| 34 | |
| 35 | def _rels_path(part: str) -> str: |
| 36 | directory, base = posixpath.split(part) |
| 37 | return f"{directory}/_rels/{base}.rels" |
| 38 | |
| 39 | |
| 40 | def _resolve(rels_path: str, target: str) -> str: |
| 41 | if target.startswith("/"): |
| 42 | return target.lstrip("/") |
| 43 | part_dir = posixpath.dirname(posixpath.dirname(rels_path)) |
| 44 | return posixpath.normpath(posixpath.join(part_dir, target)) |
| 45 | |
| 46 | |
| 47 | def _theme_rel(files: Mapping[str, bytes], master: str): |
| 48 | rels_path = _rels_path(master) |
| 49 | rels = files.get(rels_path) |
| 50 | if rels is None: |
| 51 | return None |
| 52 | for element in _RELATIONSHIP_RE.findall(part_text(rels)): |
| 53 | if f'Type="{THEME_REL_TYPE}"' not in element: |
| 54 | continue |
| 55 | target = re.search(r'\bTarget="([^"]+)"', element) |
| 56 | if target is None: |
| 57 | continue |
| 58 | return rels_path, element, _resolve(rels_path, target.group(1)) |
| 59 | return None |
| 60 | |
| 61 | |
| 62 | def _masters(files: Mapping[str, bytes]) -> list[str]: |
| 63 | return sorted((n for n in files if _MASTER_RE.match(n)), key=_sort_key) |
| 64 | |
| 65 | |
| 66 | _PRESENTATION = "ppt/presentation.xml" |
| 67 | _NOTES_MASTERS = "ppt/notesMasters/" |
| 68 | _IGNORABLE_RE = re.compile(r"<!--.*?-->|<\?.*?\?>", re.DOTALL) |
| 69 | _AFTER_SLDIDLST_RE = re.compile( |
| 70 | r"<p:sldIdLst\b(?:[^>]*/>|[^>]*>.*?</p:sldIdLst\s*>)\s*(<[^>\s/]+)", re.DOTALL |
| 71 | ) |
| 72 | |
| 73 | |
| 74 | def _notes_master_share_is_inert(files: Mapping[str, bytes]) -> bool: |
| 75 | data = files.get(_PRESENTATION) |
| 76 | if data is None: |
| 77 | return False |
| 78 | match = _AFTER_SLDIDLST_RE.search(_IGNORABLE_RE.sub("", part_text(data))) |
| 79 | return match is not None and match.group(1) == "<p:notesMasterIdLst" |
| 80 | |
| 81 | |
| 82 | def _shares(files: Mapping[str, bytes]): |
| 83 | owner: dict[str, str] = {} |
| 84 | for master in _masters(files): |
| 85 | found = _theme_rel(files, master) |
| 86 | if found is None: |
| 87 | continue |
| 88 | rels_path, element, theme = found |
| 89 | if theme not in files: |
| 90 | continue |
| 91 | if theme in owner: |
| 92 | yield master, rels_path, element, theme, owner[theme] |
| 93 | else: |
| 94 | owner[theme] = master |
| 95 | |
| 96 | |
| 97 | def _is_inert(master: str, inert_notes: bool) -> bool: |
| 98 | return inert_notes and master.startswith(_NOTES_MASTERS) |
| 99 | |
| 100 | |
| 101 | def find_shared_master_themes(files: Mapping[str, bytes]) -> list[str]: |
| 102 | return [ |
| 103 | f"{master} shares {theme} with {first}" |
| 104 | for master, _, _, theme, first in _shares(files) |
| 105 | ] |
| 106 | |
| 107 | |
| 108 | def live_shared_master_themes(files: Mapping[str, bytes]) -> list[str]: |
| 109 | inert_notes = _notes_master_share_is_inert(files) |
| 110 | return [ |
| 111 | f"{master} shares {theme} with {first}" |
| 112 | for master, _, _, theme, first in _shares(files) |
| 113 | if not _is_inert(master, inert_notes) |
| 114 | ] |