Skip to content

docs: literate-programming blocks, applied to /blocks - #116

Merged
vito merged 7 commits into
mainfrom
literate-docs-plugin
Jun 12, 2026
Merged

vito merged 7 commits into
mainfrom
literate-docs-plugin

Conversation

@vito

@vito vito commented Jun 12, 2026 •

Copy link
Copy Markdown
Owner

Docs feedback

This should be runnable. And also the string should use backticks. In fact, let's do something fun: literate programming. Add a plugin for that - alternating paragraphs and code sharing a Dang environment that runs and prints the result of each code block.

— feedback on /blocks, on the list.each { item, index => print("${index}: ${item}") } example.

The reader was more right than they knew (edit by "reader": oh, I knew): that example was broken, not just unidiomatic — ${...} only interpolates inside backtick templates, so it printed the literal text ${index}: ${item}.

The literate plugin

\dang-literate{{{ ... }}} is a code fence that actually runs:

  • Build time (docs/go/literate.go): all literate blocks in one source file share a single Dang environment, evaluated in document order — later snippets use earlier definitions, like notebook cells. Each block's output (stdout + the value of its last form, suppressed when the last form is a declaration) is baked into the static HTML, so the page is complete without JS and a broken example fails the docs build. This bug couldn't have shipped through a literate block.
  • Client side (playground.js): the blocks upgrade into the playground's overlay editor. Run (or Ctrl/Cmd+Enter) resets a shared wasm REPL session and replays the page top to bottom via a new dangLiterateEval export, updating every output; editing a block dims its output and everything downstream until the next replay.

/blocks rewritten in full literate style

The whole page is now alternating prose and evaluations: every claim is followed by a small literate block demonstrating exactly that claim — 23 blocks sharing one environment (fruits defined under "Block arguments" is the list firstMatch searches four sections later). Highlights:

  • the flagged .each example uses backticks and shows its real printed output
  • { _ + _ } ⇒ [2, 4, 6] demonstrates "every _ is the same one argument" better than prose ever did; nested _ shadowing maps over a list of lists
  • break x from .each and continue 0 in .map show their yielded values
  • the Dagger-flavored dot-block chain became a runnable string-method chain (.toUpper → .{ emphasize(_) } → .replace)
  • the null sections evaluate a null receiver through .{ } (⇒ fallback) and .{{ }} (⇒ null) side by side

Two error cases stay prose since a failing literate block fails the build by design: the nested-function break target, and expired escaped blocks (which also can't be written runnably — there's no surface syntax for a function-type annotation, so a function can't return a stored block ref).

Section anchors ({#blocks}, {#implicit-param}, {#dot-block}) and Meta notes are unchanged.

🤖 Generated with Claude Code

vito added 3 commits June 12, 2026 01:55
dangLiterateEval(sessionID, source) is dangReplEval with one display-rule
difference: an entry whose last form is a declaration yields no value, only
its printed output. It backs the docs' \dang-literate blocks, which bake
their results into the page at build time (docs/go/literate.go) and replay
client-side in one shared session — the two evaluators must agree on what a
setup block shows, or the first Run would visibly rewrite outputs the reader
already saw.

evalForms now also reports whether the last form is a declaration, detected
via DeclaredSymbols on the final node.

Signed-off-by: Alex Suraci <[email protected]>
A \dang-literate block is a code fence that actually runs. All literate
blocks in one source file share a single Dang environment, evaluated in
document order at build time — later snippets can use earlier definitions,
like cells of a notebook — and each block's output (stdout plus the value of
its last form) is baked into the static page. A block that fails to parse,
type-check, or evaluate fails the docs build, so literate examples can't rot
the way the /blocks interpolation example did.

Client-side, playground.js upgrades the blocks into the playground's overlay
editor: Run (or Ctrl/Cmd+Enter) resets a shared wasm REPL session and
replays the whole page top to bottom via dangLiterateEval, updating every
output; editing a block dims its output and everything downstream until the
next replay. Without JavaScript the baked outputs still show.

Signed-off-by: Alex Suraci <[email protected]>
Reader feedback on /blocks: the `list.each { item, index => ... }` example
printed the literal text "${index}: ${item}" — interpolation only happens in
backtick templates, not double-quoted strings — and should be runnable.

Switch the string to a backtick template and convert the block-arguments and
optional-parameters fences to \dang-literate blocks, so they evaluate at
build time with their real output baked in (the broken example would have
failed the build). The snippets now define what they use: `list` and
`numbers` get bindings, and a `withArg` declaration joins the first fence so
the args-then-block call resolves. A short note up top tells readers the
examples share one environment and can be edited and replayed in the
browser.

Signed-off-by: Alex Suraci <[email protected]>
@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Jun 12, 2026 •

Copy link
Copy Markdown

Deploying dang with  Cloudflare Pages  Cloudflare Pages

Latest commit: db9fede
Status: ✅  Deploy successful!
Preview URL: https://beece72f.dang-3kk.pages.dev
Branch Preview URL: https://literate-docs-plugin.dang-3kk.pages.dev

View logs

vito added 3 commits June 12, 2026 02:59
Every claim on the page is now followed by a small \dang-literate block
demonstrating exactly that claim, all 23 sharing one environment evaluated
top to bottom by the docs build — fruits defined under "Block arguments" is
the list firstMatch searches four sections later. The bullet-reference prose
is reworked into sentences that each set up the evaluation after them.

The examples got better in the process: `{ _ + _ }` evaluating to doubles
shows "same one argument, not positional" more convincingly than prose;
nested `_` shadowing maps over a list of lists; `break x` from .each and
`continue 0` in .map show their yielded values; the Dagger-flavored
dot-block chain is replaced by a runnable string-method chain; and the null
sections actually evaluate a null receiver through `.{ }` (=> fallback) and
`.{{ }}` (=> null) side by side.

Two error cases stay prose, since a literate block that fails would fail the
build by design: the nested-function break target and expired escaped
blocks. The latter also can't be written runnably today — there is no
surface syntax for a function-type annotation, so a function can't return a
stored block reference.

All section headings and anchors ({#blocks}, {#implicit-param},
{#dot-block}) are unchanged, as are the Meta notes.

Signed-off-by: Alex Suraci <[email protected]>
The template left a newline between the result element and the closing tag
of .dang-literate-out. The container is white-space:pre-wrap, so that
newline rendered as a blank line after every baked output. Trim it with the
{{end -}} marker.

Signed-off-by: Alex Suraci <[email protected]>
A textarea defaults to two rows, and the autosize routine sets height from
scrollHeight, which never drops below the rows-derived minimum — so every
single-line literate block (and playground) rendered two lines tall once
JS enhanced it. Set rows=1 like the REPL input already does, letting
autosize grow from a one-line baseline.

Signed-off-by: Alex Suraci <[email protected]>
@gmile

gmile commented Jun 12, 2026

Copy link
Copy Markdown

@vito do you happen to have a public URL for docs yet? 🙏 I keep updating my bookmark with recent builds like https://edab6a36.dang-3kk.pages.dev/json-yaml, but it's a moving target, would be nice to have a non-changing link

Editor textareas autosize by measuring scrollHeight, but the measurement is
only valid for the width and font it was taken at: the content re-wraps when
the viewport narrows (overflow:hidden then clips the extra lines) and again
when the JetBrains Mono webfont swaps in. Register every editor's autosize
in a shared list and re-run them all, frame-throttled, on window resize and
document.fonts.ready.

Signed-off-by: Alex Suraci <[email protected]>
@vito

vito commented Jun 12, 2026

Copy link
Copy Markdown
Owner Author

@gmile Yes! The docs are pushed to https://danglang.org whenever I merge into main. I'll update the repo with that URL.

@vito
vito merged commit 6372038 into main Jun 12, 2026
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants