summaryrefslogtreecommitdiffhomepage
path: root/dlang/README.md
diff options
context:
space:
mode:
authorRalph Amissah <ralph.amissah@gmail.com>2026-09-27 15:39:02 -0400
committerRalph Amissah <ralph.amissah@gmail.com>2026-09-27 15:39:02 -0400
commit8d03e44a28c3d1de7d59b07f5e7f611cde0c38c6 (patch)
tree9db9f5c1e0ce10542c9bde49182ab53e0fd9187c /dlang/README.md
format 2.0: sample writers over the abstraction, in D and in Gleam
Writers over sisudoc-ocda-lib: functions from the document model to bytes, in five formats, one program, ocda-write, in each language. Begun 2026-09-25; this is the writer as it stood at format 2.0. ocda-write --text|--md|--html|--epub|--typst <file> [lang] They are samples. Spine is the output pipeline and nothing here replaces it. What they show is that the abstraction is sufficient: given a .ssp, a .ocda.db or a .sisupod and nothing else, a few hundred lines per format make a complete document. A writer is never told which form it was given, and every writer is run against every form of every document. Each format is held to an oracle that can say no: --text the words equal the html writer's objects, as a multiset; every object number exactly once --md CommonMark; every (#x) resolves to an <a id=x>, no id twice --html it parses; every internal link resolves; the objects' text equals spine's scroll html --epub epubcheck --typst typst itself: a link to a missing label does not compile and in every format the same bytes from every input form, and the D and Gleam writers byte for byte alike. Over the markup document samples every check holds for every document-language. The two languages' writers were not written from each other, and their agreement is the strongest check: twice it caught what nothing else did (an ampersand in an attribute mangled by Erlang's replacement string; typst label names built from bytes in one language and graphemes in the other). Byte identity with spine is not the aim, by decision. The text writer began byte identical to spine --text, and was rewritten: a note is written once, markup comments and dummy headings are not written, an image keeps its caption, a table is padded to display width. The html writer is valid html5 rather than a copy of spine's (h1..h6 where spine writes <h0>; one charset; no placeholder metadata; an id for every anchor tag; self-contained). Each difference is a better presentation, and the code says why at the point of choice. The typst writer puts the object number in the margin as the label every citation points at, a note as a real footnote with a link back, and a table of contents that locates by object number rather than by page, as the markdown writer's does: a page number is a fact about one typesetting, the object number is not. The markdown writer is CommonMark with yaml front matter; a note is at its reference, with the document's own number, and --image-prefix gives the image path, which the model rightly does not know. Holding the writers to these checks found defects in spine, several since fixed there, and spine's typst and markdown writers were ported from these. Use: nix build, in dlang/ or in gleam/. Each flake takes the library from git.sisudoc.org, and flake.lock pins the commit of it these were built and checked against. gleam/ builds with nix-gleam's builder, kept in gleam/nix-gleam/ (MIT); in a working tree, gleam.toml reads the library through the symlink ./ocda-lib. See README.md, dlang/README.md and gleam/README.md, and pipeline/ for building everything and running it over a directory of artefacts. (assisted by Claude-Code)
Diffstat (limited to 'dlang/README.md')
-rw-r--r--dlang/README.md85
1 files changed, 85 insertions, 0 deletions
diff --git a/dlang/README.md b/dlang/README.md
new file mode 100644
index 0000000..e3a41b6
--- /dev/null
+++ b/dlang/README.md
@@ -0,0 +1,85 @@
+# ocda-write, in D
+
+Sample writers over the abstraction:
+
+ ocda-write --text|--md|--html|--epub|--typst <file> [lang]
+ ocda-write --md <file> [lang] [--image-prefix <path>]
+
+writes a document out, from any serialisation of its abstraction.
+
+## Modules
+
+| | |
+|---|---|
+| `write.text` | the model -> spine's plain text, byte for byte |
+| `write.html` | the model -> one self-contained, valid html5 file |
+| `write.epub` | the model -> an epub3, one file per segment |
+| `write.markdown` | the model -> CommonMark, object numbers and all |
+| `write.typst` | the model -> typst markup, for a pdf |
+| `write.zip` | writing a zip, stored only, with no timestamps |
+| `write.rgx` | the internal markup, as regular expressions |
+| `main` | the program: which format, which file, which language |
+
+`write.text` takes an `OcdaDocument` and returns a string. It is given no
+file, no path and no option: a writer is a function from the model to bytes,
+and everything it needs is in the model.
+
+## Build and run
+
+```sh
+nix build --override-input ocda-lib path:../../sisudoc-ocda-lib
+./result/bin/ocda-write --text <doc>.ssp
+./result/bin/ocda-write --html <doc>.ocda.db de
+./result/bin/ocda-write --html <doc>.sisupod
+./result/bin/ocda-write --md <doc>.ssp
+./result/bin/ocda-write --epub <doc>.ocda.db en --out <doc>.epub
+./result/bin/ocda-write --typst <doc>.ocda.db en --out out/<doc>.typ
+typst compile --root out out/<doc>.typ out/<doc>.pdf
+```
+
+`--epub` writes a zip, so it takes `--out` rather than stdout. `--typst` goes
+to stdout too, but with `--out` it also writes the `image/` directory the
+`.typ` names.
+
+`nix build .#ocda-write-dmd` builds with dmd instead of ldc.
+
+The library's modules are compiled alongside these rather than linked as a
+separate product; for five modules a second build step buys nothing.
+
+## The checks
+
+`--text` and `--md` are checked against the html writer's objects, word for
+word, and against each other language's writer, byte for byte: **36 of 36 on
+every count.** Byte identity with `spine --text` was the first oracle and is
+no longer the target; see the repository README.
+
+`--html` is checked harder still: it parses, every `href="#x"`
+resolves, no id appears twice, no heading falls outside `h1`..`h6`, and the
+objects' visible text equals spine's scroll html exactly. **36 of 36 on each,
+and byte identical to the Gleam writer.** See the repository README for what
+it does differently from spine and why.
+
+`--epub` is judged by `epubcheck`, and `--typst` by typst: a link to a label
+that does not exist is a compile error, so a silent compile means every
+internal link in the document resolves. **36 of 36 on each, and byte
+identical to the Gleam writer.**
+
+## Where it is faithful rather than sensible
+
+The *text* writer reproduces spine's output exactly, which means reproducing
+what would not be chosen from scratch. The comments say so at each point.
+The three worth knowing before reading the code:
+
+- **the table layout strips trailing whitespace from the whole accumulated
+ table after every cell**, not once per row, so a row ending in an empty
+ cell loses the newline before it;
+- **a column's width is the widest cell in bytes and the padding is counted
+ in codepoints**, so a cell holding `©` is padded one place further than one
+ holding `c`;
+- **the section sequence is spine's `scroll` sequence**, not its `text` one:
+ head, toc, body, then each trailing section *that has more than one
+ object*, and no `tail` unless an html or epub output was also asked for.
+
+The *html* writer takes none of these as given. What it takes from the model
+and what it declines to take from spine is set out in its own comments, at
+the point where each choice is made.