A CLI that splits a Markdown document into cells (Markdown vs.
fenced lean code), tracks each cell's evaluated outputs, and
renders the result as any of:
- a Jupyter
.ipynb(with thexeus-leankernel pre-set and cell outputs preserved), - a
.lean:percentsource file you can hand tolake buildfor type-checking, - a single static HTML page or a multi-chapter HTML site (sidebar nav, syntax-highlighted Lean, no JS evaluation), or
- the same Markdown back (round-trip, with outputs baked in).
It can also run a chapter through lean to capture each cell's
stdout / Display.* output and bake the results into a fresh
Markdown file as ```output:* fences.
Authoring directly as .ipynb doesn't review well — the JSON is
opaque in git diff, and code cells drift from .lean files that
lake build actually checks.
Authoring as .lean:percent (jupytext) works for lake build, but
jupytext doesn't ship native support for the .lean extension and
has no path that produces a presentable static site.
xlean-convert keeps the source in Markdown — readable on
GitHub, pretty diffs, no special tooling — and generates
whichever artefact you need:
chapter.md ─┬─→ chapter.ipynb (--to ipynb) JupyterLite / Lab
├─→ chapter.lean (--to lean) lake build
├─→ chapter.html (--to html) single page
├─→ chapter-out.md (--eval) outputs baked in
└─→ _site/Ch*.html (--site DIR) tintin-style site
Round-trip:
chapter.md ── --to ipynb ──→ chapter.ipynb
│
run in Jupyter (cell outputs saved)
↓
chapter.ipynb
chapter.md ←── --to md ──── (with outputs as ```output:* fences)
lake build xlean-convertThe binary lands at .lake/build/bin/xlean-convert.
xlean-convert --to {ipynb|lean|html|md} [-o OUTPUT] INPUT
xlean-convert --site DIR [-o OUTDIR] [--title TITLE]
xlean-convert --eval INPUT.md [-o OUTPUT.md]
--to TARGET— one-off conversion. INPUT may be.md,.markdown, or.ipynb; the source format is auto-detected by extension. Use-for stdin (treated as Markdown).-o OUTPUT— output file (or directory for--site). Defaults to the input filename with the extension swapped.-= stdout.--site DIR— build a multi-chapter static HTML site (see Site mode below).--title TXT— site index title (site mode only).--eval— run the chapter throughleanand bake every cell's stdout andDisplay.*output into the resulting.mdasoutput:*fences (see Eval mode below).
# Markdown → notebook
xlean-convert --to ipynb chapter.md
# Markdown → executable Lean source for `lake build`
xlean-convert --to lean chapter.md
# Notebook → Markdown (outputs land as ```output:* fences)
xlean-convert --to md chapter.ipynb
# Single static HTML page (no kernel)
xlean-convert --to html chapter.md
# Multi-chapter static site
xlean-convert --site docs/tutorial/md \
-o _site \
--title "My HDL Tutorial"
# Bake evaluation results into the Markdown
LEAN_PATH=.lake/build/lib/lean \
xlean-convert --eval chapter.md -o chapter-out.mdThe Markdown is scanned line-by-line for fenced code blocks
(```).
| Fence tag | Goes to |
|---|---|
lean (case-insensitive) |
a Jupyter code cell, body verbatim |
output / output:<mime/lang> |
the last code cell's outputs (see below) |
anything else (bash, verilog, text, …) |
stays in the surrounding Markdown cell |
| no fence at all | accumulates into a Markdown cell |
A code cell's evaluated output is attached as one or more
```output[:TAG] fences immediately after the lean fence:
| Tag | Maps to (Jupyter MIME) | HTML render |
|---|---|---|
output |
stream (stdout) |
<pre class="output"> |
output:html |
text/html |
raw HTML inside <div class="output html"> |
output:svg |
image/svg+xml |
raw SVG inside <div class="output svg"> |
output:latex |
text/latex |
MathJax span |
output:markdown / output:md |
text/markdown |
recursively rendered |
output:json |
application/json |
<pre><code class="language-json"> |
output:<lang> (verilog, c++, …) |
text/plain + hljs language-<lang> |
colourised plain text |
Multiple outputs on one cell are listed in order. Authors can
write them by hand (handy for documenting expected results) or
have --eval insert them automatically (see below).
Example:
```lean
#eval 1 + 1
```
```output
2
```
```lean
#eval Display.html "<b>x</b>"
```
```output:html
<b>x</b>
```The output is a jupytext .lean:percent-style source: a YAML
front-matter block (in -- Lean comments) declaring the kernel,
then alternating -- %% [markdown] and -- %% blocks. Markdown
bodies are line-prefixed with -- so the file is also valid
Lean syntactically. Cell outputs are not preserved in this
format (use --to md if you need them).
Walks DIR for Ch*.md and Ch*.ipynb (preferring .ipynb
when both exist for the same stem so authors can ship pre-
executed notebooks), parses each, and emits:
index.html— site title + chapter listCh00_Foo.html,Ch01_Bar.html, … — one page per chapterstyle.css— two-column layout, dark sidebar, table styling
Each chapter page has:
- Sidebar (left) — full chapter list, current page highlighted,
numbers like
0,1b,10derived by stripping theChapter N —prefix from each title. - Main column (right) — the chapter body, with Lean code in
<pre><code class="language-lean">(real Lean keywords coloured via thehighlightjs-leanCDN), tables / blockquotes / lists, and anyoutput:*fences rendered as described above. - Footer — prev / Contents / next links.
The pages are fully static (no JS execution); rich outputs come
from whatever the source .ipynb or .md contains. Pages still
render cleanly if the highlight.js CDN is unreachable.
Runs the chapter's code cells through lean to capture their
stdout and Display.* output, then writes a fresh .md with the
results baked in as output:* fences.
The pipeline:
- The cells are rendered as a single
.leanfile with each code cell followed by a delimiter#evalthat flushesDisplay.drainand prints===XLEAN-CELL-END N===. lean FILEis invoked (nomainneeded;#evalruns top-to- bottom). The child inherits the currentLEAN_PATH.- The captured stdout is split on the delimiters, then each
chunk is parsed for Display's MIME markers
(
\x1bMIME:<type>\x1e<body>\x1b/MIME\x1e). Everything outside markers becomes astreamoutput; each marker becomes the corresponding MIME output. - The augmented cells are serialised back to Markdown.
LEAN_PATH=.lake/build/lib/lean \
xlean-convert --eval chapter.md -o chapter-out.mdRequirements:
leanon$PATH(any toolchain that can buildDisplay).LEAN_PATHincludes the directory containingDisplay.olean(.lake/build/lib/leanafterlake build Display).- If the chapter uses Sparkle / Hesper / other libs, add their
oleans to
LEAN_PATHand includeimport Sparkle(etc.) at the top of the firstleancell.
Not yet supported:
- Per-cell error reporting (a failing
#evalaborts the rest of the chapter; the partial outputs are still written so you can see where it broke). - Interactive comms / waveform
WaveformSession.*(those rely on the live Jupyter kernel; use--to ipynband run in Jupyter for those).
A typical setup:
- Author chapters as plain
.mdunderdocs/tutorial/md/. - Build artefacts in CI:
xlean-convert --to ipynb Ch00.md -o build/Ch00.ipynb xlean-convert --to lean Ch00.md -o build/Ch00.lean lake build BuildLean xlean-convert --site docs/tutorial/md -o _site --title "..." - Optional: bake live results into Markdown for review:
LEAN_PATH=.lake/build/lib/lean \ xlean-convert --eval Ch00.md -o docs/tutorial/md/Ch00.md
- Deploy
_site/to GitHub Pages.
lake build convert-test
.lake/build/bin/convert-testsrc/ConvertTest.lean runs in-memory checks covering empty input,
fence handling, the .ipynb JSON structure, and the
.lean:percent round-trip.