|
| 1 | +Rich Clipboard — HTML (CF_HTML) |
| 2 | +=============================== |
| 3 | + |
| 4 | +The base ``clipboard`` module handles plain text (``CF_UNICODETEXT``) and image |
| 5 | +(``CF_DIB``) only. Pasting *formatted* content into Word / Outlook / a rich editor |
| 6 | +needs the ``CF_HTML`` format, whose ``Version / StartHTML / EndHTML / StartFragment / |
| 7 | +EndFragment`` **byte-offset** header is notoriously error-prone to build by hand. |
| 8 | +``build_cf_html`` / ``parse_cf_html`` compute and recover that header in pure Python |
| 9 | +(a fully unit-tested round-trip, correct across multi-byte UTF-8), and |
| 10 | +``set_clipboard_html`` / ``get_clipboard_html`` wrap them over the Win32 clipboard. |
| 11 | + |
| 12 | +The byte-offset math is platform-independent and headless-testable; only the actual |
| 13 | +clipboard I/O is Windows (raising ``RuntimeError`` elsewhere, like the base module). |
| 14 | +Imports no ``PySide6``. |
| 15 | + |
| 16 | +Headless API |
| 17 | +------------ |
| 18 | + |
| 19 | +.. code-block:: python |
| 20 | +
|
| 21 | + from je_auto_control import (build_cf_html, parse_cf_html, |
| 22 | + set_clipboard_html, get_clipboard_html) |
| 23 | +
|
| 24 | + set_clipboard_html("<b>Bold</b> and <i>italic</i>", |
| 25 | + fragment_plaintext="Bold and italic") # Windows |
| 26 | + html = get_clipboard_html() # Windows |
| 27 | +
|
| 28 | + # The pure pieces work anywhere (e.g. to pre-build a payload): |
| 29 | + payload = build_cf_html("<p>hello</p>") # bytes, valid CF_HTML |
| 30 | + assert parse_cf_html(payload) == "<p>hello</p>" |
| 31 | +
|
| 32 | +``build_cf_html`` returns valid ``CF_HTML`` UTF-8 bytes whose offsets point exactly at |
| 33 | +the fragment; ``parse_cf_html`` recovers the fragment from bytes or text (preferring |
| 34 | +the comment markers, falling back to the byte offsets). ``set_clipboard_html`` also |
| 35 | +seeds plain text via ``fragment_plaintext`` so apps that ignore HTML still paste |
| 36 | +something. |
| 37 | + |
| 38 | +Executor commands |
| 39 | +----------------- |
| 40 | + |
| 41 | +``AC_set_clipboard_html`` (``html`` / ``fragment_plaintext`` → ``{set, length}``) and |
| 42 | +``AC_get_clipboard_html`` (→ ``{found, html}``). They are exposed as the MCP tools |
| 43 | +``ac_set_clipboard_html`` / ``ac_get_clipboard_html`` and as Script Builder commands |
| 44 | +under **Data**. |
0 commit comments