| Marc Kupietz | 235b0b5 | 2026-08-21 15:31:02 +0200 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | """Re-apply IDS-local patches to the vendored reveal.js bundle. |
| 3 | |
| 4 | Run after every reveal.js update (see tools/update-reveal.R): |
| 5 | |
| 6 | python3 tools/patch-revealjs.py |
| 7 | |
| 8 | Every patch is an exact-string replacement keyed to the minified upstream |
| 9 | code. If a newer reveal.js refactors the surroundings, the script fails |
| 10 | loudly instead of skipping silently -- port the patch by hand, then update |
| 11 | the pattern here. Safe to run repeatedly (already-applied patches are |
| 12 | detected and skipped). |
| 13 | """ |
| 14 | import sys |
| 15 | from pathlib import Path |
| 16 | |
| 17 | REPO = Path(__file__).resolve().parent.parent |
| 18 | |
| 19 | # Patch name -> [(upstream snippet, patched snippet), ...] |
| 20 | PATCHES = { |
| 21 | # Upstream toggles the help overlay only on the US-layout keyCodes for '?' |
| 22 | # (63/191) + shift, and an earlier modifier guard swallows unknown keyCodes |
| 23 | # held with shift outright. On e.g. a German layout ('?' = Shift+ss) Chrome |
| 24 | # reports neither keyCode, so '?' never opened the help screen there. |
| 25 | # Match the produced character (event.key) instead; F1 keeps working. |
| 26 | "help overlay on non-US keyboard layouts ('?' via event.key)": [ |
| 27 | ( |
| 28 | 'l=!(-1!==[32,37,38,39,40,63,78,80,191].indexOf(e.keyCode)&&e.shiftKey||e.altKey)&&(e.shiftKey||e.altKey||e.ctrlKey||e.metaKey)', |
| 29 | 'l=!(-1!==[32,37,38,39,40,63,78,80,191].indexOf(e.keyCode)&&e.shiftKey||e.altKey)&&(e.shiftKey||e.altKey||e.ctrlKey||e.metaKey)&&"?"!==e.key', |
| 30 | ), |
| 31 | ( |
| 32 | ':63!==i&&191!==i||!e.shiftKey?112===i?this.Reveal.toggleHelp():u=!1:this.Reveal.toggleHelp())', |
| 33 | ':"?"!==e.key&&(!e.shiftKey||63!==i&&191!==i)?112===i?this.Reveal.toggleHelp():u=!1:this.Reveal.toggleHelp())', |
| 34 | ), |
| 35 | ], |
| 36 | } |
| 37 | |
| 38 | |
| 39 | def main(): |
| 40 | bundles = sorted((REPO / "inst").glob("reveal.js-*/dist/reveal.js")) |
| 41 | if len(bundles) != 1: |
| 42 | sys.exit(f"expected exactly one vendored reveal.js, found: {bundles}") |
| 43 | path = bundles[0] |
| 44 | original = src = path.read_text() |
| 45 | |
| 46 | for name, pairs in PATCHES.items(): |
| 47 | for old, new in pairs: |
| 48 | if new in src: |
| 49 | print(f"already applied: {name}") |
| 50 | elif old in src: |
| 51 | src = src.replace(old, new, 1) |
| 52 | print(f"applied: {name}") |
| 53 | else: |
| 54 | sys.exit( |
| 55 | f"UPSTREAM CHANGED -- cannot locate pattern for patch:\n" |
| 56 | f" {name}\n" |
| 57 | f" missing snippet starts with: {old[:70]}...\n" |
| 58 | f"Port the patch by hand, then update tools/patch-revealjs.py." |
| 59 | ) |
| 60 | |
| 61 | if src != original: |
| 62 | path.write_text(src) |
| 63 | print(f"written: {path}") |
| 64 | else: |
| 65 | print("nothing to do") |
| 66 | |
| 67 | |
| 68 | if __name__ == "__main__": |
| 69 | main() |