md2xarticle 0.1.1__tar.gz

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 (99) hide show
  1. md2xarticle-0.1.1/.gitignore +8 -0
  2. md2xarticle-0.1.1/LICENSE +21 -0
  3. md2xarticle-0.1.1/PKG-INFO +592 -0
  4. md2xarticle-0.1.1/README.md +567 -0
  5. md2xarticle-0.1.1/pyproject.toml +53 -0
  6. md2xarticle-0.1.1/src/md2xarticle/__init__.py +3 -0
  7. md2xarticle-0.1.1/src/md2xarticle/__main__.py +3 -0
  8. md2xarticle-0.1.1/src/md2xarticle/check.py +82 -0
  9. md2xarticle-0.1.1/src/md2xarticle/cli.py +459 -0
  10. md2xarticle-0.1.1/src/md2xarticle/compose/__init__.py +1 -0
  11. md2xarticle-0.1.1/src/md2xarticle/compose/base.py +42 -0
  12. md2xarticle-0.1.1/src/md2xarticle/compose/body.py +27 -0
  13. md2xarticle-0.1.1/src/md2xarticle/compose/cover.py +116 -0
  14. md2xarticle-0.1.1/src/md2xarticle/compose/fit.py +54 -0
  15. md2xarticle-0.1.1/src/md2xarticle/compose/measure.py +60 -0
  16. md2xarticle-0.1.1/src/md2xarticle/customize.py +284 -0
  17. md2xarticle-0.1.1/src/md2xarticle/errors.py +139 -0
  18. md2xarticle-0.1.1/src/md2xarticle/factory.py +112 -0
  19. md2xarticle-0.1.1/src/md2xarticle/fonts.py +33 -0
  20. md2xarticle-0.1.1/src/md2xarticle/model.py +101 -0
  21. md2xarticle-0.1.1/src/md2xarticle/outline.py +55 -0
  22. md2xarticle-0.1.1/src/md2xarticle/parse/__init__.py +1 -0
  23. md2xarticle-0.1.1/src/md2xarticle/parse/base.py +15 -0
  24. md2xarticle-0.1.1/src/md2xarticle/parse/markdown_it.py +213 -0
  25. md2xarticle-0.1.1/src/md2xarticle/pipeline.py +289 -0
  26. md2xarticle-0.1.1/src/md2xarticle/platform/__init__.py +1 -0
  27. md2xarticle-0.1.1/src/md2xarticle/platform/base.py +25 -0
  28. md2xarticle-0.1.1/src/md2xarticle/platform/posix.py +23 -0
  29. md2xarticle-0.1.1/src/md2xarticle/platform/windows.py +25 -0
  30. md2xarticle-0.1.1/src/md2xarticle/platform/wsl.py +31 -0
  31. md2xarticle-0.1.1/src/md2xarticle/privacy.py +262 -0
  32. md2xarticle-0.1.1/src/md2xarticle/render/__init__.py +1 -0
  33. md2xarticle-0.1.1/src/md2xarticle/render/base.py +25 -0
  34. md2xarticle-0.1.1/src/md2xarticle/render/mermaidx.py +179 -0
  35. md2xarticle-0.1.1/src/md2xarticle/render/svg_flowchart.py +175 -0
  36. md2xarticle-0.1.1/src/md2xarticle/render/svg_gitgraph.py +458 -0
  37. md2xarticle-0.1.1/src/md2xarticle/shape/__init__.py +1 -0
  38. md2xarticle-0.1.1/src/md2xarticle/shape/captions.py +96 -0
  39. md2xarticle-0.1.1/src/md2xarticle/shape/comments.py +12 -0
  40. md2xarticle-0.1.1/src/md2xarticle/shape/headings.py +155 -0
  41. md2xarticle-0.1.1/src/md2xarticle/shape/markers.py +63 -0
  42. md2xarticle-0.1.1/src/md2xarticle/theme.py +88 -0
  43. md2xarticle-0.1.1/src/md2xarticle/themes/__init__.py +0 -0
  44. md2xarticle-0.1.1/src/md2xarticle/themes/mono.json +75 -0
  45. md2xarticle-0.1.1/src/md2xarticle/themes/soft.json +75 -0
  46. md2xarticle-0.1.1/src/md2xarticle/themes/warm.json +75 -0
  47. md2xarticle-0.1.1/src/md2xarticle/themes/x-dark.json +86 -0
  48. md2xarticle-0.1.1/src/md2xarticle/themes/x-light.json +86 -0
  49. md2xarticle-0.1.1/src/md2xarticle/transform.py +42 -0
  50. md2xarticle-0.1.1/src/md2xarticle/write/__init__.py +1 -0
  51. md2xarticle-0.1.1/src/md2xarticle/write/base.py +19 -0
  52. md2xarticle-0.1.1/src/md2xarticle/write/html.py +486 -0
  53. md2xarticle-0.1.1/src/md2xarticle/write/manifest.py +65 -0
  54. md2xarticle-0.1.1/tests/__init__.py +0 -0
  55. md2xarticle-0.1.1/tests/conftest.py +20 -0
  56. md2xarticle-0.1.1/tests/contracts/__init__.py +0 -0
  57. md2xarticle-0.1.1/tests/contracts/test_article_writer.py +27 -0
  58. md2xarticle-0.1.1/tests/contracts/test_diagram_renderer.py +59 -0
  59. md2xarticle-0.1.1/tests/contracts/test_image_composer.py +78 -0
  60. md2xarticle-0.1.1/tests/contracts/test_markdown_parser.py +84 -0
  61. md2xarticle-0.1.1/tests/contracts/test_platform.py +44 -0
  62. md2xarticle-0.1.1/tests/contracts/test_registry.py +40 -0
  63. md2xarticle-0.1.1/tests/dist_check.py +191 -0
  64. md2xarticle-0.1.1/tests/helpers.py +45 -0
  65. md2xarticle-0.1.1/tests/offline_run.py +95 -0
  66. md2xarticle-0.1.1/tests/skip_budget.py +28 -0
  67. md2xarticle-0.1.1/tests/snapshots/paste_test.html +91 -0
  68. md2xarticle-0.1.1/tests/snapshots/paste_test.md +127 -0
  69. md2xarticle-0.1.1/tests/snapshots/paste_test.nested.html +100 -0
  70. md2xarticle-0.1.1/tests/snapshots/showcase.md +40 -0
  71. md2xarticle-0.1.1/tests/snapshots/synthetic_article.expected.json +12 -0
  72. md2xarticle-0.1.1/tests/snapshots/synthetic_article.manifest.json +39 -0
  73. md2xarticle-0.1.1/tests/snapshots/synthetic_article.md +76 -0
  74. md2xarticle-0.1.1/tests/snapshots/tall.md +105 -0
  75. md2xarticle-0.1.1/tests/test_architecture.py +120 -0
  76. md2xarticle-0.1.1/tests/test_author_theme_and_wide.py +71 -0
  77. md2xarticle-0.1.1/tests/test_captions.py +154 -0
  78. md2xarticle-0.1.1/tests/test_check.py +129 -0
  79. md2xarticle-0.1.1/tests/test_cli.py +187 -0
  80. md2xarticle-0.1.1/tests/test_convert.py +394 -0
  81. md2xarticle-0.1.1/tests/test_customize.py +123 -0
  82. md2xarticle-0.1.1/tests/test_default_theme.py +52 -0
  83. md2xarticle-0.1.1/tests/test_dist_check.py +99 -0
  84. md2xarticle-0.1.1/tests/test_errors.py +201 -0
  85. md2xarticle-0.1.1/tests/test_headings.py +168 -0
  86. md2xarticle-0.1.1/tests/test_html_writer.py +299 -0
  87. md2xarticle-0.1.1/tests/test_hygiene.py +166 -0
  88. md2xarticle-0.1.1/tests/test_manifest.py +70 -0
  89. md2xarticle-0.1.1/tests/test_markers.py +91 -0
  90. md2xarticle-0.1.1/tests/test_no_network.py +135 -0
  91. md2xarticle-0.1.1/tests/test_outline.py +151 -0
  92. md2xarticle-0.1.1/tests/test_polish.py +309 -0
  93. md2xarticle-0.1.1/tests/test_privacy.py +125 -0
  94. md2xarticle-0.1.1/tests/test_readme_sync.py +515 -0
  95. md2xarticle-0.1.1/tests/test_release.py +63 -0
  96. md2xarticle-0.1.1/tests/test_skip_budget.py +38 -0
  97. md2xarticle-0.1.1/tests/test_tall_gitgraph.py +192 -0
  98. md2xarticle-0.1.1/tests/test_transform.py +23 -0
  99. md2xarticle-0.1.1/tests/test_x_themes.py +111 -0
@@ -0,0 +1,8 @@
1
+ __pycache__/
2
+ *.py[cod]
3
+ *.egg-info/
4
+ build/
5
+ dist/
6
+ .venv/
7
+ .pytest_cache/
8
+ out/
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 si4ai
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,592 @@
1
+ Metadata-Version: 2.5
2
+ Name: md2xarticle
3
+ Version: 0.1.1
4
+ Summary: Markdown in, X Article out: polished, centered diagram images and paste-ready articles.
5
+ Project-URL: Homepage, https://github.com/si4ai/md2xarticle
6
+ Project-URL: Source, https://github.com/si4ai/md2xarticle
7
+ Project-URL: Issues, https://github.com/si4ai/md2xarticle/issues
8
+ License-Expression: MIT
9
+ License-File: LICENSE
10
+ Classifier: Environment :: Console
11
+ Classifier: Operating System :: OS Independent
12
+ Classifier: Programming Language :: Python :: 3
13
+ Classifier: Programming Language :: Python :: 3.11
14
+ Classifier: Programming Language :: Python :: 3.12
15
+ Classifier: Programming Language :: Python :: 3.13
16
+ Requires-Python: >=3.11
17
+ Requires-Dist: markdown-it-py<5,>=3
18
+ Requires-Dist: mermaidx==0.11.3
19
+ Requires-Dist: novasvg==0.11.5
20
+ Requires-Dist: pillow==13.0.0
21
+ Requires-Dist: quickjs-ng==0.18.0.1
22
+ Provides-Extra: test
23
+ Requires-Dist: pytest>=8; extra == 'test'
24
+ Description-Content-Type: text/markdown
25
+
26
+ # md2xarticle
27
+
28
+ Markdown in, X Article out. One command turns a Markdown file, Mermaid diagrams included, into a paste-ready article body plus polished images for the X (Twitter) Article editor.
29
+
30
+ **Status:** The full conversion (`md2xarticle ARTICLE.md`) is implemented: one run writes `article.html`, `outline.txt`, the images, and `manifest.json`. So are `diagrams`, `outline`, and `check`. This README is the v0 design for whoever builds it, human or bot.
31
+
32
+ ### If you are an agent
33
+
34
+ Run `md2xarticle article.md -o out/ --json`.
35
+ Read `out/manifest.json`.
36
+ Paste `out/article.html` into the X Article editor, then upload each image in manifest order at its marker.
37
+
38
+ The short bot skill (install, commands, `--json` and `manifest.json`, upload order, exit codes, privacy check) is in [docs/BOT-SKILL.md](docs/BOT-SKILL.md).
39
+
40
+ ## Why
41
+
42
+ X Articles accept rich text, but only in a narrow shape: three text levels, native tables and code blocks, and images that must be uploaded in the editor because paste drops them. Long-form Markdown does not fit that shape out of the box.
43
+
44
+ Converting one real long-form article by hand showed where the time goes:
45
+
46
+ - Deep heading levels (`####` and below) had no place to go and had to be restructured by hand.
47
+ - Mermaid diagrams came out cropped tight, off-center, and in stock Mermaid styling, at whatever size the renderer picked.
48
+ - Text placeholders had to be swapped for images one by one.
49
+ - Much of the work was copy, paste, and tweak inside the X rich editor.
50
+ - The 5:2 cover image was a separate job.
51
+ - Captions taken automatically from the nearest heading read badly.
52
+ - The privacy check before publishing was a manual read-through.
53
+
54
+ md2xarticle's rule: **if the tool can do it, the human should not have to.** Whatever is left by hand (today, image uploads) is made as short and as obvious as possible.
55
+
56
+ ## Just works
57
+
58
+ The default:
59
+
60
+ ```
61
+ pip install md2xarticle
62
+ md2xarticle article.md -o out/
63
+ ```
64
+
65
+ Output:
66
+
67
+ ```
68
+ out/
69
+ article.html # paste this into the X Article editor
70
+ outline.txt # the restructured heading outline, for a quick check
71
+ manifest.json # image order, file, caption, anchor text (for bots)
72
+ images/
73
+ 01.png # 16:9 body images, numbered in reading order
74
+ 02.png
75
+ cover.png # 5:2, only with --cover
76
+ ```
77
+
78
+ Zero config is the goal. No browser, no Node, no web service, and no network at run time. The diagram font is DejaVu Sans, which ships inside the pinned mermaidx package, so images look the same on every machine. Cover titles use the same font. Every flag has a default.
79
+
80
+ Python 3.11 to 3.13. Platforms: Windows, WSL, Linux, and macOS, on Intel and ARM where the dependencies ship wheels.
81
+
82
+ **WSL note:** run inside WSL, output lands in the Linux filesystem. To paste from a browser on Windows, either write to the Windows side (`-o /mnt/c/<folder>/out`) or open the folder through `\\wsl$`. md2xarticle prints this hint when it detects WSL.
83
+
84
+ ### Acceptance tests (zero-config goal)
85
+
86
+ | Input | Expected behavior |
87
+ |---|---|
88
+ | Plain Markdown, `#` and `##` only | Headings map 1:1 to Heading and Subheading; no images; `manifest.json` has an empty image list; exit 0. |
89
+ | Deep headings (`###` to `######`) | Restructured per [Headings](#headings); `outline.txt` shows the result; no heading level beyond Subheading reaches the HTML. |
90
+ | Tables and fenced code | Stay as real `<table>` and `<pre><code>` markup, never images. |
91
+ | Mermaid blocks | Each becomes a themed PNG centered on a 16:9 canvas, a numbered marker in the HTML, and a manifest entry. |
92
+ | Invalid Mermaid block | That diagram fails with its line number and the exact command to retry it alone; the rest of the article still renders; exit 1. |
93
+ | `--cover` | Adds `images/cover.png` at 5:2. |
94
+ | Front matter (YAML) | Read for title, subtitle, and cover text; not pasted into the body. |
95
+ | Empty file | Empty article, exit 0. |
96
+ | Same input twice | Byte-identical HTML and manifest; PNGs identical on the same platform. |
97
+
98
+ ## Concepts
99
+
100
+ ```mermaid
101
+ flowchart LR
102
+ MD[article.md] --> P[parse<br/>markdown-it-py]
103
+ P --> S[shape for X<br/>headings, markers]
104
+ S --> W[article.html<br/>outline.txt]
105
+ P --> D[Mermaid blocks]
106
+ D --> R[render<br/>mermaidx]
107
+ R --> C[compose<br/>Pillow, 16:9 and 5:2]
108
+ C --> I[images/NN.png]
109
+ S --> M[manifest.json]
110
+ I --> M
111
+ ```
112
+
113
+ ### X Article constraints
114
+
115
+ These are the facts the design is built around. Anything not listed here is unknown until the Phase 0 paste test measures it.
116
+
117
+ - **Three text levels:** Heading, Subheading, Body.
118
+ - **Tables and code blocks** are supported natively. They must reach the editor as real markup.
119
+ - **Images cannot come through paste.** They are uploaded in the editor, one at a time.
120
+ - **Cover image** is 5:2.
121
+ - **Body images** are 16:9 for now. Other ratios may come later.
122
+
123
+ ### Headings
124
+
125
+ X has two heading levels, so the tool restructures instead of renaming.
126
+
127
+ The first `#`, or a front matter `title`, is the article title. It does not appear in the body.
128
+
129
+ When the document uses a single `#` as that title, the body mapping is:
130
+
131
+ | Markdown | X Article |
132
+ |---|---|
133
+ | `##` | Heading |
134
+ | `###` | Subheading |
135
+ | `####` and deeper | Bold lead-in paragraph, or a list item when siblings form a list |
136
+
137
+ Otherwise the mapping is:
138
+
139
+ | Markdown | X Article |
140
+ |---|---|
141
+ | `#` or `##` | Heading |
142
+ | `###` | Subheading |
143
+ | `####` and deeper | Bold lead-in paragraph, or a list item when siblings form a list |
144
+
145
+ The tool picks the mapping, writes it to `outline.txt`, and prints it, so a human or bot can check the structure before pasting. `--heading-map` overrides the choice.
146
+
147
+ This restructuring is implemented; `md2xarticle outline ARTICLE.md` prints the result.
148
+
149
+ ### Diagrams
150
+
151
+ Each Mermaid block is rendered to a PNG, trimmed, and placed on a fixed canvas.
152
+
153
+ - **Renderer:** [mermaidx](https://pypi.org/project/mermaidx/) by default, pinned to mermaidx==0.11.3, novasvg==0.11.5, and quickjs-ng==0.18.0.1, with Pillow==13.0.0 for composition. It runs real Mermaid inside an embedded QuickJS engine and rasterizes with novasvg, so it needs no Node and no Chrome. Known costs: about 0.5 to 2 seconds per diagram, and bundled-font metrics can differ slightly from a browser render. Fidelity notes are in [Phase 0 findings](docs/PHASE0-FINDINGS.md).
154
+ - **Theme:** the default is `mono`: DejaVu Sans (from mermaidx), neutral gray on white, rounded boxes, and generous spacing. The other built-ins are `soft` (pale teal on white), `warm` (amber on cream), and two matched to X's own palette: `x-light` (white, text #0F1419, borders #CFD9DE) and `x-dark` (black, text #E7E9EA, borders #2F3336). Both X themes use X blue (#1D9BF0) only as an accent, for the gitGraph main branch, tags, and the first pie slice, and draw a cover subtitle in X's secondary text gray. Every built-in starts from Mermaid's `base` theme with its own palette; Mermaid's own `default` theme is never used. Palette, corner radius, spacing, and later the font are set in a customization file. See [Customization](#customization).
155
+ - **Composition:** Pillow trims the render, scales it to fit with even padding, and centers it on a 16:9 canvas (default 1600x900, set by `--size`). The cover composer does the same at 5:2 (default 1500x600) with an optional title and subtitle above the diagram, from front matter `title` and `subtitle` (or `cover_title` and `cover_subtitle` in the customization file). Upscaling is capped at 3x the native render, so a tiny diagram is not blown up. Every image must be the exact canvas size, with content centered within 4 px (body) or 8 px (cover with a title or subtitle), and a minimum margin of 4% of each side.
156
+ - **Never cropped tight, never off-center.** A check measures the margins of every output image against those tolerances.
157
+ - **Readable text.** A flowchart that is limited by the canvas height is also rendered with wider label wrapping (320 px instead of Mermaid's 200 px), and whichever draws larger is kept. The cover makes this choice again against its own diagram box, below the title and subtitle. The source is never rewritten. When a diagram still has to shrink below 0.65x on a body image or the cover, so its text drops under about 10 px, the run warns and names the next step: a `layout = "LR"` override for a flowchart, or splitting the diagram.
158
+ - **Tall diagrams.** A top-down flowchart or a sequence diagram that is taller than 16:9 and would shrink on the default canvas is also rendered compact: a flowchart with Mermaid's dagre layout at 30 px between ranks (the default ELK layout ignores rank spacing), a sequence diagram with tighter message spacing. Whichever fits 16:9 larger is kept; `[style] compact_tall = false` turns this off.
159
+ - **Wide flowcharts.** A flowchart wider than 16:9 is limited by the canvas width, so it cannot be scaled up further without breaking the margins. It is also rendered compact (dagre, 30 px between ranks and between nodes), and the compact render is kept only when it draws at least 10% larger. The direction is never changed. `[style] compact_tall = false` turns this off too.
160
+ - **The diagram's own theme.** A `%%{init: ...}%%` directive that sets `theme` or `themeVariables` keeps its colors for that diagram; the run theme's colors are not applied to it. Spacing, corner radius, and the canvas color still come from the run theme.
161
+ - **Subgraph spacing.** The default ELK layout can leave a large empty band at the bottom of a subgraph that has both an outgoing edge and an incoming back edge. That band is removed after layout, keeping the flowchart padding under the last node.
162
+ - **gitGraph labels.** Slanted commit labels are measured against the lanes, branch lines, branch names, tags, commits, and each other. Only the lane or commit gaps that need it are widened, so no label crosses anything. Level labels are left as Mermaid draws them.
163
+ - **Same input, same bytes.** With the pinned stack, a run in a fresh process writes byte-identical PNGs on the same platform.
164
+
165
+ ### Image markers and the manifest
166
+
167
+ Images cannot be pasted, so the tool makes placement obvious and ordered.
168
+
169
+ - Each diagram's spot in `article.html` holds a short visible marker, for example `[Image 02: Request flow]`, on its own line. The human or bot uploads `images/02.png` there and deletes the marker.
170
+ - `manifest.json` lists the same things in machine form:
171
+
172
+ ```json
173
+ {
174
+ "version": 1,
175
+ "source": "article.md",
176
+ "cover": "images/cover.png",
177
+ "images": [
178
+ {
179
+ "order": 1,
180
+ "file": "images/01.png",
181
+ "marker": "[Image 01: System overview]",
182
+ "caption": "System overview",
183
+ "anchor_text": "The pieces fit together like this:",
184
+ "source_line": 42
185
+ }
186
+ ]
187
+ }
188
+ ```
189
+
190
+ `anchor_text` is the paragraph just before the image, so a bot can find the spot in the editor even after the marker text is gone.
191
+
192
+ This is implemented. `md2xarticle diagrams` writes `manifest.json` next to the images. `cover` is `null` when there is no cover image. `source` is the article file name, not a path. The manifest lists only images that were written.
193
+
194
+ ### Captions
195
+
196
+ Captions come from, in order: a Mermaid `title`, a `%% caption:` comment in the block, the alt text of a following `*Figure: ...*` line, a following line that is italic as a whole (`*...*` or `_..._`), then the nearest heading. If none of those are present, the caption is `Diagram NN`, so every marker has a label. See [Decided](#open-questions).
197
+
198
+ ### Privacy scan
199
+
200
+ `--check-privacy` (or the `check` command) flags, without changing anything:
201
+
202
+ - Email addresses and phone-number shapes
203
+ - Local paths: Windows user folders, Unix and macOS home folders, `\\wsl$` shares, and Windows user folders seen from WSL under `/mnt/<drive>/Users`
204
+ - Secret-shaped strings: API keys, bearer tokens, PEM blocks, credentials in URLs
205
+ - Names from an optional local deny-list file that never leaves the machine. The path comes from `--deny-list FILE` or the `MD2XARTICLE_PRIVACY_DENYLIST` environment variable; the file is not shipped with the package.
206
+
207
+ Findings print with file and line, and never include the matched text. It is a check, not a block: exit 1 on findings, and the output files are still written. The `check` command writes nothing.
208
+
209
+ ## Architecture
210
+
211
+ The design uses abstract base classes (`abc.ABC`) as interfaces, with one concrete subclass per implementation. The interfaces sit where things really vary: the parser library, the diagram renderer, the image shape, the output format, and the operating system.
212
+
213
+ ```mermaid
214
+ classDiagram
215
+ class MarkdownParser {
216
+ <<abstract>>
217
+ +parse(text) Document
218
+ }
219
+ class MarkdownItParser
220
+ MarkdownParser <|-- MarkdownItParser
221
+
222
+ class DiagramRenderer {
223
+ <<abstract>>
224
+ +render(source, theme) RenderedImage
225
+ +available() bool
226
+ }
227
+ class MermaidxRenderer
228
+ class MmdrRenderer
229
+ class MmdcRenderer
230
+ DiagramRenderer <|-- MermaidxRenderer
231
+ DiagramRenderer <|-- MmdrRenderer
232
+ DiagramRenderer <|-- MmdcRenderer
233
+
234
+ class ImageComposer {
235
+ <<abstract>>
236
+ +compose(image, size) Image
237
+ }
238
+ class BodyImageComposer
239
+ class CoverComposer
240
+ ImageComposer <|-- BodyImageComposer
241
+ ImageComposer <|-- CoverComposer
242
+
243
+ class ArticleWriter {
244
+ <<abstract>>
245
+ +write(article, out_dir) list
246
+ }
247
+ class XArticleHtmlWriter
248
+ class ManifestWriter
249
+ ArticleWriter <|-- XArticleHtmlWriter
250
+ ArticleWriter <|-- ManifestWriter
251
+
252
+ class Platform {
253
+ <<abstract>>
254
+ +font_dirs() list
255
+ +default_output_dir() Path
256
+ +open_result(path)
257
+ +notes() list
258
+ }
259
+ class WindowsPlatform
260
+ class PosixPlatform
261
+ class WslPlatform
262
+ Platform <|-- WindowsPlatform
263
+ Platform <|-- PosixPlatform
264
+ PosixPlatform <|-- WslPlatform
265
+
266
+ class Pipeline {
267
+ +run(source, out_dir) Result
268
+ }
269
+ Pipeline --> MarkdownParser
270
+ Pipeline --> DiagramRenderer
271
+ Pipeline --> ImageComposer
272
+ Pipeline --> ArticleWriter
273
+ Pipeline --> Platform
274
+ ```
275
+
276
+ | Interface | Subclasses | What varies |
277
+ |---|---|---|
278
+ | `MarkdownParser` | `MarkdownItParser` | The parsing library, so it can be swapped later. |
279
+ | `DiagramRenderer` | `MermaidxRenderer` (default); later `MmdrRenderer` (Rust reimplementation), `MmdcRenderer` (needs Node and Chromium, opt-in only) | How Mermaid becomes pixels. |
280
+ | `ImageComposer` | `BodyImageComposer` (16:9), `CoverComposer` (5:2) | Canvas shape, padding, and cover text. |
281
+ | `ArticleWriter` | `XArticleHtmlWriter`, `ManifestWriter` | Output format. |
282
+ | `Platform` | `WindowsPlatform`, `PosixPlatform`, `WslPlatform` | Font lookup, default output folder, opening the result, the WSL paste note. |
283
+
284
+ Rules:
285
+
286
+ - **`Pipeline` depends only on the base classes.** It never imports a concrete subclass.
287
+ - **A factory picks the subclasses** from CLI flags and the detected platform. It is the only place that knows the concrete names.
288
+ - **No network renderer is ever the default.** mermaid.ink, Kroki, and similar services are out unless a future flag opts in by name.
289
+ - **Paths use `pathlib`; files are read and written as UTF-8** explicitly. The OS split lives in `Platform`, not in every class.
290
+ - **Renderers report `available()`**, so a missing optional backend fails with the install command, not a stack trace.
291
+
292
+ Pipeline:
293
+
294
+ ```mermaid
295
+ flowchart TD
296
+ A[CLI args] --> F[factory<br/>flags plus detected platform]
297
+ F --> PL[Pipeline<br/>base classes only]
298
+ PL --> P1[MarkdownParser.parse]
299
+ P1 --> P2[shape for X<br/>heading map, markers, captions]
300
+ P2 --> P3{Mermaid blocks?}
301
+ P3 -->|yes| P4[DiagramRenderer.render<br/>per block]
302
+ P4 --> P5[ImageComposer.compose<br/>16:9, and 5:2 cover]
303
+ P3 -->|no| P6
304
+ P5 --> P6[ArticleWriter.write<br/>HTML, outline, manifest]
305
+ P6 --> P7{--check-privacy?}
306
+ P7 -->|yes| P8[privacy scan<br/>report findings]
307
+ P7 -->|no| P9[summary, Platform notes,<br/>exit code]
308
+ P8 --> P9
309
+ ```
310
+
311
+ Suggested layout:
312
+
313
+ ```
314
+ src/md2xarticle/
315
+ cli.py
316
+ pipeline.py
317
+ factory.py
318
+ parse/ base.py markdown_it.py
319
+ shape/ headings.py markers.py captions.py
320
+ render/ base.py mermaidx.py
321
+ compose/ base.py body.py cover.py
322
+ write/ base.py html.py manifest.py
323
+ platform/ base.py windows.py posix.py wsl.py
324
+ privacy.py
325
+ fonts.py finds DejaVu Sans inside mermaidx (no font files ship here)
326
+ tests/
327
+ contracts/ one contract suite per base class
328
+ snapshots/ synthetic .md fixtures and expected outputs
329
+ ```
330
+
331
+ ## Planned commands
332
+
333
+ All four are implemented: the full conversion, `diagrams`, `outline`, and `check`. Defaults match the [quick start](#just-works).
334
+
335
+ ### `md2xarticle ARTICLE.md [-o OUT] [options]`
336
+
337
+ The full conversion: HTML, outline, images, manifest.
338
+
339
+ | Flag | Default | Meaning |
340
+ |---|---|---|
341
+ | `-o, --out DIR` | `./out` (from `Platform`) | Output folder. Created if missing. |
342
+ | `--cover` | off | Also build `images/cover.png` at 5:2. Uses front matter `title` and `subtitle` if present. |
343
+ | `--theme NAME` | `mono` | Diagram theme: `mono`, `soft`, `warm`, `x-light`, or `x-dark`. Overridden by `--custom-path` when that file sets a theme. |
344
+ | `--custom-path FILE` | off | One TOML customization file for this run. See [Customization](#customization). |
345
+ | `--size WxH` | `1600x900` | Body image size. Must be 16:9 for now. |
346
+ | `--only N[,N]` | all diagrams | Render only these diagrams, by 1-based index. Used in the error retry hint. |
347
+ | `--renderer NAME` | `mermaidx` | Diagram renderer. |
348
+ | `--heading-map MAP` | auto | Override the heading restructuring. |
349
+ | `--check-privacy` | off | Run the privacy scan after writing. |
350
+ | `--json` | off | Print a machine-readable summary to stdout instead of text. |
351
+ | `-v, --verbose` | off | Full renderer message on a failed diagram, and a traceback on an internal error (exit 3). Without it, exit 3 is a short message and a `try:` line, never a traceback. |
352
+ | `--open` | off | Open the output folder when done. |
353
+
354
+ Sample output:
355
+
356
+ ```
357
+ md2xarticle 0.1.1 article.md -> out/
358
+
359
+ Outline (X levels)
360
+ H Introduction
361
+ H How it works
362
+ SH The parser
363
+ SH The renderer
364
+ [bold lead-in] Theme details
365
+ H Results
366
+
367
+ Images
368
+ 01 images/01.png System overview (after "The pieces fit together like this:")
369
+ 02 images/02.png Request flow (after "Each request goes through three steps:")
370
+ cover images/cover.png 5:2
371
+
372
+ Wrote out/article.html, out/outline.txt, out/manifest.json, 3 images in 2.4s
373
+
374
+ Next: open out/article.html, copy all, paste into the X Article editor,
375
+ then upload images in order at each [Image NN] marker.
376
+ ```
377
+
378
+ `--json` output:
379
+
380
+ ```json
381
+ {
382
+ "ok": true,
383
+ "out_dir": "out",
384
+ "html": "out/article.html",
385
+ "outline": "out/outline.txt",
386
+ "manifest": "out/manifest.json",
387
+ "images": 2,
388
+ "cover": true,
389
+ "failed": [],
390
+ "warnings": [],
391
+ "privacy_findings": [],
392
+ "notes": []
393
+ }
394
+ ```
395
+
396
+ `images` counts the body images written; `cover` says whether `images/cover.png` was written. `failed` lists each diagram that did not render (`order`, `source_line`, `error`). `privacy_findings` is `[]` when `--check-privacy` is off. `notes` holds platform hints, such as the WSL paste note.
397
+
398
+ ### `md2xarticle diagrams ARTICLE.md [-o OUT] [--cover]`
399
+
400
+ Images only: render every Mermaid block to a polished, centered 16:9 PNG, plus an optional 5:2 cover. This is the Phase 1 milestone and is useful on its own for an article whose text is already in the X editor and only needs better images. `--only`, `--theme`, `--custom-path`, `--size`, `--renderer`, and `--check-privacy` apply here too. `--check-privacy` scans the source article after writing; images are still written.
401
+
402
+ ### `md2xarticle check ARTICLE.md`
403
+
404
+ Privacy scan only. Prints findings with line numbers. Writes nothing. `--deny-list FILE` or `MD2XARTICLE_PRIVACY_DENYLIST` supplies the optional local deny-list. `--json` prints the same findings as a list. Exit 1 on findings, 0 if clean, 2 on a usage error (missing file or deny-list).
405
+
406
+ ### `md2xarticle outline ARTICLE.md`
407
+
408
+ Implemented. Prints the restructured heading outline only, so the structure can be checked before converting.
409
+
410
+ ### Customization
411
+
412
+ `--custom-path FILE` points to one TOML file with the theme, layout, cover, and sizes for that run. The usual pattern is one file per Markdown article:
413
+
414
+ ```
415
+ md2xarticle article.md --custom-path article.toml
416
+ ```
417
+
418
+ With no flag, the built-in defaults apply: theme `mono`, and Mermaid source is rendered as written.
419
+
420
+ The file may set the theme (palette, corner radius, spacing, label wrapping, gitGraph label rotation; font later), per-diagram layout overrides, cover choice and text, and image sizes. A value in the file wins over the matching flag (`--theme`, `--size`), and the run prints a warning when that happens.
421
+
422
+ The file is read with the standard library `tomllib`, so no extra TOML dependency is needed.
423
+
424
+ Example:
425
+
426
+ ```toml
427
+ theme = "mono"
428
+ size = "1600x900"
429
+ cover_size = "1500x600"
430
+ cover = "diagram:2"
431
+ cover_subtitle = "" # "" turns the front matter subtitle off
432
+
433
+ [style]
434
+ corner_radius = 8
435
+ spacing = 16
436
+ wrap_width = 280 # flowchart label wrapping, px (default: automatic)
437
+ rotate_commit_labels = false # gitGraph: level commit labels (default: rotated)
438
+ compact_tall = true # tall or wide flowcharts and tall sequences may render compact
439
+
440
+ [[diagrams]]
441
+ id = 1
442
+ layout = "LR"
443
+ ```
444
+
445
+ ## Output contract
446
+
447
+ Text summary to stdout by default; `--json` for machines. Warnings and errors go to stderr.
448
+
449
+ Exit codes:
450
+
451
+ | Code | Meaning |
452
+ |---|---|
453
+ | 0 | Ok. |
454
+ | 1 | Findings: a diagram failed, privacy findings, or a heading needed a forced fallback. Output that could be written was written. |
455
+ | 2 | Usage error (bad flag, missing file, wrong size ratio). |
456
+ | 3 | Internal error. |
457
+
458
+ Every error prints the exact next command to try. Examples:
459
+
460
+ ```
461
+ error: diagram 03 (article.md line 88) failed to render: Parse error on line 4
462
+ try: md2xarticle diagrams article.md --only 3 --verbose
463
+ ```
464
+
465
+ ```
466
+ error: renderer 'mermaidx' is not installed
467
+ try: pip install md2xarticle
468
+ ```
469
+
470
+ ```
471
+ error: renderer 'mmdc' is planned and not available yet
472
+ try: md2xarticle diagrams article.md --renderer mermaidx
473
+ ```
474
+
475
+ ## Testing
476
+
477
+ - **Contract tests.** One suite per base class. Every subclass runs its base class's suite, so a new renderer, composer, writer, or platform must pass the same checks before it plugs in.
478
+ - **Snapshot tests.** Synthetic Markdown fixtures in, expected output out: today the `diagrams --json` result and `manifest.json`; HTML snapshots join with the HTML writer. PNGs are checked by the image checks and determinism below, not against stored reference images.
479
+ - **Image checks.** Every output PNG is checked for exact canvas size, aspect ratio, and margins: content centered within 4 px (body) or 8 px (cover with a title or subtitle), with a minimum margin of 4% of each side. Never cropped tight.
480
+ - **Cross-platform.** CI runs the full suite on Windows, macOS, and Ubuntu across the supported Python versions, so the image checks and determinism hold on each OS. There is no cross-OS perceptual diff against reference images yet; it is planned, not in CI.
481
+ - **Determinism.** Same input twice gives byte-identical HTML and manifest. With the pinned renderer stack (mermaidx==0.11.3, novasvg==0.11.5, quickjs-ng==0.18.0.1, Pillow==13.0.0), PNG output is byte-identical across fresh processes on one platform. Byte equality across OSes is not expected.
482
+ - **No network.** A conversion runs in a fresh process with a socket guard that fails the run on any connect or DNS lookup, even one the caller swallows. On Ubuntu, CI also converts inside `unshare -rn` (no network interfaces), after proving that connects and DNS fail there and succeed outside it.
483
+ - **Packaging.** CI builds the wheel and sdist on all three OSes, checks every file against an allowlist (package code is `.py`, themes are `.json`), runs the hygiene scan over their contents, installs each into a fresh venv, and runs `md2xarticle --help` and a `diagrams --cover` conversion.
484
+ - **Spec sync.** A test fails when the Python range or the pins stated here drift from `pyproject.toml`, or when any workflow names a Python outside that range or none covers it all. Workflows are linted with a pinned, checksummed actionlint.
485
+ - **Skip budget.** The run fails when more tests skip than `tests/skip_budget.py` expects (0 today).
486
+
487
+ ## Hygiene
488
+
489
+ This is an open-source repository. Nothing personal goes in it.
490
+
491
+ - No personal information, real names, usernames, local paths, email addresses, or secrets anywhere: code, package, tests, fixtures, docs, examples, commit messages.
492
+ - Fixtures are synthetic. Real articles are tested locally and never committed.
493
+ - Example paths are generic: `article.md`, `out/`, `images/01.png`.
494
+ - A hygiene check (`tools/hygiene.py`, the one scanner) runs in CI on every PR and push, over the tree, the commit messages, and the built wheel and sdist. It scans for email shapes, home-directory paths, secret shapes, typographic marks, byte-order marks, and a deny-list kept outside the repo (`MD2XARTICLE_HYGIENE_DENYLIST`). Typographic marks under `tests/snapshots/` are allowed, because those fixtures capture the HTML writer's typography. Its planted fixture (`--selftest`) and the pytest suite prove it fails visibly.
495
+ - md2xarticle ships no font files. DejaVu Sans comes with mermaidx, under its own license. Any font bundled here later must have a license that allows redistribution, shipped beside it.
496
+
497
+ ## Release
498
+
499
+ Publishing is trusted publishing from GitHub Actions, with no stored PyPI token. The workflow file is `.github/workflows/release.yml`. `workflow_dispatch` from `main` is a dry run: it builds the package, runs the tests, `twine check --strict`, and a wheel smoke test, and it uploads nothing. A `v*` tag on `main` runs the same checks and publishes to PyPI through the `pypi` environment. A human admin approves the `pypi` environment when a required reviewer is set. The `testpypi` environment is unused. The runbook, including the one-time admin settings, is [docs/RELEASE.md](docs/RELEASE.md).
500
+
501
+ ## Roadmap
502
+
503
+ ```mermaid
504
+ flowchart LR
505
+ P0[Phase 0<br/>renderer fidelity,<br/>X paste test, theme] --> P1[Phase 1<br/>diagrams milestone<br/>16:9 and 5:2]
506
+ P1 --> P2[Phase 2<br/>HTML, outline,<br/>manifest]
507
+ P2 --> P3[Phase 3<br/>privacy scan, polish,<br/>PyPI release, bot skill doc]
508
+ ```
509
+
510
+ | Phase | Work | Done when |
511
+ |---|---|---|
512
+ | 0. Measure | Render a synthetic diagram set with mermaidx on all three OSes and note fidelity gaps. Paste test: which elements survive paste into the X editor (spacing, nested lists, links, blockquotes, inline code, tables, code blocks). Show theme samples to the owner. | Renderer notes are in [docs/PHASE0-FINDINGS.md](docs/PHASE0-FINDINGS.md). Still open: the paste-survival table (the owner runs the X paste test) and an owner pick of theme samples, which only confirms the default. |
513
+ | 1. Diagrams | `md2xarticle diagrams article.md` produces polished, centered 16:9 PNGs and an optional 5:2 cover. ABC skeleton, contract tests, CI matrix, and hygiene check land here. | The owner drops the images into an existing X draft and approves them. CI is green on all three OSes. Every image is the exact canvas size, with content centered within 4 px (body) or 8 px (cover with a title or subtitle), and a minimum margin of 4% of each side. |
514
+ | 2. Article | HTML writer shaped by the Phase 0 paste table, heading restructuring, `outline.txt`, markers, captions, `manifest.json`, `--json`. | A synthetic long-form fixture pastes into X with no text cleanup. The owner's manual-edit list passes as acceptance tests. |
515
+ | 3. Release | Privacy scan, error messages with next commands, README and docs, a short skill doc for bots, PyPI release from CI. | `pip install md2xarticle` works on Windows, WSL, Linux, and macOS from a clean environment. |
516
+
517
+ Phase 1 comes before the full pipeline on purpose: a real article is already drafted in the X editor and is waiting only on better images.
518
+
519
+ **Project done when:** a real long-form article goes from Markdown to a ready-to-publish X draft with only the image uploads done by hand.
520
+
521
+ **Not in scope:** posting to X, driving the X editor, or any browser automation. md2xarticle writes files; a human or a bot does the upload.
522
+
523
+ ## Open questions
524
+
525
+ Ask the owner. Do not guess.
526
+
527
+ - **What X paste keeps.** Spacing, nested lists, links, blockquotes, inline code, tables, and code blocks are unmeasured until the Phase 0 paste test. The owner runs that test by pasting `tests/snapshots/paste_test.html` and `tests/snapshots/paste_test.nested.html` (both written from `tests/snapshots/paste_test.md`). The HTML writer's current markup, listed under Decided, follows public reports until then.
528
+ - **The owner's manual edits.** The list of hand edits made in the X editor on the real article. Each one becomes an acceptance test.
529
+ - **Image placement.** How close to one-step placement X allows. Markers plus manifest is the baseline; anything better depends on what the editor permits without browser automation.
530
+ - **`--watch` mode.** Wanted or not.
531
+ - **Renderer fallback.** If mermaidx output falls short of the theme bar, which alternate wins: mmdr (fast, not official Mermaid) or opt-in mmdc (needs Node and Chromium).
532
+ - **Cover content.** What the 5:2 cover is built from: a chosen diagram, the title on a themed background, or a supplied image.
533
+
534
+ Decided:
535
+
536
+ - **`--custom-path FILE`.** One TOML customization file per run, typically one file per article. No flag means built-in defaults (theme `mono`, Mermaid rendered as written). See [Customization](#customization).
537
+ - **Default theme.** `mono` (neutral gray on white), picked by the owner from the theme samples. `soft`, `warm`, `x-light`, and `x-dark` are options, and Mermaid's own `default` theme is not used. Further look-and-feel is customization.
538
+ - **HTML writer markup** (`write/html.py`, one function per element; revisit after the paste test). No classes, inline styles, or scripts, and the same input gives the same bytes.
539
+ - Headings: X Heading is `<h2>` and Subheading is `<h3>`, never `<h1>`, because X keeps the title in its own field. The title goes in `<title>` only. Heading text is plain, matching `outline.txt`.
540
+ - Deep headings: a bold lead-in is `<p><strong>...</strong></p>` on its own line; a run of sibling list-item headings is one `<ul>`.
541
+ - Paragraphs: one `<p>` per paragraph, with no empty paragraphs for spacing. A soft line break becomes a space and a hard break becomes `<br>`.
542
+ - Inline: `<strong>`, `<em>`, `<s>`, `<a href>` (titles dropped), and `<code>`.
543
+ - Lists: `<ul>` and `<ol>` (with `start` when it is not 1), always tight, so there is no `<p>` inside `<li>`. Nested lists are flattened by default into the top-level list, with an en dash per level and the number kept for ordered sub-items, because public reports say X drops nested items. `HtmlOptions(nested_lists="nest")` keeps real nesting.
544
+ - Quotes: `<blockquote>` with `<p>` inside.
545
+ - Code blocks: `<pre><code>`, with the language tag dropped because X has no highlighting.
546
+ - Tables: `<table>` with `<thead>`/`<tbody>`, `<th>` and `<td>`, and alignment dropped.
547
+ - Divider: `<hr>`.
548
+ - Images: each Mermaid block becomes its marker as its own `<p>`, taken from the document's image reference when one exists and `[Image NN]` otherwise. A Markdown image becomes the text `[Image: alt]`.
549
+ - Raw HTML in the source is escaped and shown as text. HTML comments (`<!-- ... -->`) are notes for the author and are dropped, block or inline.
550
+ - **Caption source rules.** Captions are taken in this order: a Mermaid `title` (front matter in the diagram source, a `title` line, or a `pie title`), then a `%% caption:` comment in the block, then the text of an immediately following `*Figure: ...*` line (also `**Figure: ...**`, `_Figure: ..._`, `Figure: ...`, and `![Figure: ...](...)`), then an immediately following paragraph that is one italic run (`*...*` or `_..._`, one line; a paragraph with only some italic words, or bold, does not count), then the nearest preceding heading. If none of those are present, the caption is `Diagram NN` (padded like the image number) so every marker has a label. A diagram with no explicit caption still gets one.
551
+ - **Markers and manifest.** Each written body image gets a marker `[Image NN: caption]` and a `manifest.json` entry with `order`, `file`, `marker`, `caption`, `anchor_text`, and `source_line`. `anchor_text` is the paragraph immediately before the diagram, or `null` when the previous block is not a paragraph. `source` is the article file name, not a path. `cover` is `images/cover.png` or `null`. Image numbers are zero-padded to at least two digits, matching `images/NN.png`. The manifest lists only images that were written (a failed render is omitted). `diagrams` writes `manifest.json`, and `--json` includes a `manifest` path.
552
+ - **Nested diagrams.** A Mermaid fence inside a list item or blockquote is a diagram like any other and keeps its place in reading order. It takes no `anchor_text` and no `Figure:` caption from the text around it; its caption comes from its own `title` or `%% caption:`, then the nearest heading.
553
+ - **HTML comments** (dogfood finding). Comments are dropped from `article.html`: a comment-only block, a comment next to other raw HTML, and an inline comment. Other raw HTML is still escaped and shown as text. The writer now tokenizes raw HTML (markdown-it `html` on) so comments can be found, but never passes HTML through. A dropped comment also no longer separates a diagram from the line after it, so a caption line below a comment still counts.
554
+ - **Italic caption line** (dogfood finding). A paragraph right after a top-level diagram that is one italic run is its caption, after the `Figure:` forms and before the nearest heading. A `Figure:` line still wins; a Mermaid `title` and `%% caption:` still win over both. The italic line stays in the article body, like a `Figure:` line does, so the words remain under the image after the marker is deleted. Nested diagrams still take no caption from the text around them.
555
+ - **The diagram's own `%%{init}%%` theme** (dogfood finding). Honored rather than warned about: it is cheap (the renderer's own `themeVariables` were overriding the directive) and "Mermaid source is rendered as written" reads that way. Only colors and the Mermaid theme name come from the directive; layout, spacing, corner radius, and the canvas color stay the run theme's, so every image keeps the same canvas.
556
+ - **Wide flowcharts** (dogfood finding). Not scaled past the canvas width: the 16:9 size and the 4% minimum margin stay. Instead, a flowchart wider than 16:9 gets a compact dagre render and keeps it when it draws at least 10% larger (a sample four-stage loop chart goes from about 1.03x to 1.22x). The 10% floor keeps diagrams that gain little on the default ELK layout, so they render as before. It shares `compact_tall` rather than adding a new key.
557
+ - **PyPI release.** Trusted publishing from `.github/workflows/release.yml`. `workflow_dispatch` from `main` is a dry run (build, tests, twine, wheel smoke; uploads nothing). A `v*` tag on `main` publishes to PyPI (environment `pypi`). A human admin approves the `pypi` environment when a reviewer is required. The `testpypi` environment is unused. See [Release](#release) and [docs/RELEASE.md](docs/RELEASE.md).
558
+ - **Full conversion** (`md2xarticle ARTICLE.md`).
559
+ - Any first argument that is not a command name (`diagrams`, `outline`, `check`) or `-h`, `--help`, `--version` starts the full conversion, so flags may come before the file. An article named like a command is passed as `./check`.
560
+ - `Pipeline.run` parses once, renders and composes the images, then hands one `Article` (images written, cover, Markdown text, its parse, heading map) to every `ArticleWriter` the factory gives it: `XArticleHtmlWriter`, then `ManifestWriter`. The privacy scan runs last, only with `--check-privacy`.
561
+ - `article.html` and `outline.txt` are always written in full. A diagram that failed, or that `--only` left out, keeps the numbered `[Image NN]` marker in the HTML and is not in the manifest, which lists only images written in this run.
562
+ - Exit 1 for a failed diagram, privacy findings, or a heading fallback. A heading fallback prints a warning and `try: md2xarticle ARTICLE.md --heading-map flat`. A failed diagram keeps the retry line from the [Output contract](#output-contract): `md2xarticle diagrams ARTICLE.md --only N --verbose`. Usage and internal errors name the full conversion in their `try:` line.
563
+ - `--json` has the README keys plus `outline`, `failed`, and `notes`; see the sample under [Planned commands](#planned-commands). The text summary counts the cover in its image total and cuts a long `anchor_text` to 48 characters.
564
+ - `--open` opens the output folder through `Platform.open_result` after writing.
565
+ - **Contract reports (rco).** `.github/workflows/rco.yml`, written by `rco init` from repo-context-optimizer 0.1.6 (the workflow pins that version), adds one report-only `rco` job (Ubuntu, Python 3.12). On a pull request its job summary shows `rco diff`, `rco affected`, `rco drift pins` and the cross-PR notice; on a push to `main` it runs `rco update`. It is not a required check and nothing runs at pre-commit. `pins/core.json` (`model`, `theme`, `errors`, the package root) and `pins/interfaces.json` (the five base classes, `pipeline`, `factory`) pin the most-imported public contracts; a drift is a report, and a change made on purpose re-pins with `rco pin --for FILES --format json --out pins/NAME.json`. `rco.toml` keeps every default.
566
+
567
+ Proposed in this draft but not yet approved by the owner. Confirm or cut each one:
568
+
569
+ - **Subcommands `check` and `outline`,** alongside `diagrams`.
570
+ - **Flags `--heading-map`, `--only`, `--open`, and `--size`.**
571
+ - **Default image sizes:** 1600x900 for body images and 1500x600 for the cover.
572
+ - **Exit code 1** for a failed diagram or for privacy findings.
573
+ - **Privacy deny-list:** an optional local list of names for the privacy scan, kept out of the repo.
574
+
575
+ ## Working on this repo (for agents)
576
+
577
+ - **Open questions go to the owner before you guess.** If a decision is listed above, or you find a new one, ask and wait. Record the answer in this README.
578
+ - **Prefer checks over hard gates.** Report facts visibly (CI checks, findings, warnings) instead of blocking work.
579
+ - **Small, verifiable PRs.** One concern per PR, each with a done-when that a test or check confirms.
580
+ - **Do not re-run flaky tests until they pass. Fix them.** A flaky image diff usually means a nondeterministic renderer, font, or Pillow setting.
581
+ - **Keep the interfaces honest.** A new subclass passes its base class's contract suite. `Pipeline` never imports a concrete class.
582
+ - **Hygiene first.** Run the hygiene check before every push. Synthetic fixtures only. Once per checkout, turn on the repo hooks with `git config core.hooksPath .githooks`. `pre-commit` runs `tools/hygiene.py --staged` on staged blobs.
583
+ - **Contracts before code.** Install the repo-context-optimizer version that `.github/workflows/rco.yml` pins, in a separate environment on the Python version that workflow uses (rco needs a newer Python than this package's floor). Then run `rco pack --for FILES` before editing those files and `rco diff $(git merge-base origin/main HEAD) HEAD` before pushing. Both are advisory.
584
+ - **No network at run time.** If a feature needs the network, it is out of scope or opt-in by name, and the owner decides.
585
+
586
+ ## Prior art
587
+
588
+ Browser tools and editor extensions already convert Markdown for X Articles, and some agent skills drive the X editor through a browser. md2xarticle differs in being a local, pip-installable CLI with no browser, Node, or network, real Mermaid rendering, and a manifest so bots can finish the job.
589
+
590
+ ## License
591
+
592
+ MIT, under the si4ai organization. See [LICENSE](LICENSE).