diff options
| author | Ralph Amissah <ralph.amissah@gmail.com> | 2026-09-27 15:39:02 -0400 |
|---|---|---|
| committer | Ralph Amissah <ralph.amissah@gmail.com> | 2026-09-27 15:39:02 -0400 |
| commit | 8d03e44a28c3d1de7d59b07f5e7f611cde0c38c6 (patch) | |
| tree | 9db9f5c1e0ce10542c9bde49182ab53e0fd9187c /gleam/src/write/zip.gleam | |
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 'gleam/src/write/zip.gleam')
| -rw-r--r-- | gleam/src/write/zip.gleam | 132 |
1 files changed, 132 insertions, 0 deletions
diff --git a/gleam/src/write/zip.gleam b/gleam/src/write/zip.gleam new file mode 100644 index 0000000..05cb56a --- /dev/null +++ b/gleam/src/write/zip.gleam @@ -0,0 +1,132 @@ +//// Writing a zip, stored only. +//// +//// An epub is a zip with rules: the first entry must be "mimetype", stored, +//// with no extra field, so that a reading system can identify the file from +//// its first bytes without inflating anything. Everything else may be +//// deflated and does not have to be. +//// +//// So: stored only. Deflate would buy compression on a sample writer's +//// output, which is not what is being demonstrated, and it would put a second +//// thing in the way of the two writers agreeing byte for byte. +//// +//// No timestamps. Every entry is dated 1980-01-01, the epoch of the format, +//// which is what makes the same document produce the same bytes twice. + +import gleam/bit_array +import gleam/list + +pub type Entry { + Entry(name: String, data: BitArray) +} + +const method_stored = 0 + +/// 2.0, which stored requires +const version_needed = 20 + +const dos_time = 0 + +/// 1980-01-01 +const dos_date = 0x0021 + +/// The archive, entries in the order given. +pub fn write(entries: List(Entry)) -> BitArray { + let #(local, offsets, crcs, _) = + list.fold(entries, #(<<>>, [], [], 0), fn(state, e) { + let #(acc, offsets, crcs, at) = state + let crc = crc32(e.data) + let header = local_header(e, crc) + let size = bit_array.byte_size(header) + bit_array.byte_size(e.data) + #( + bit_array.concat([acc, header, e.data]), + [at, ..offsets], + [crc, ..crcs], + at + size, + ) + }) + let offsets = list.reverse(offsets) + let crcs = list.reverse(crcs) + let cd_offset = bit_array.byte_size(local) + let central = + list.zip(entries, list.zip(crcs, offsets)) + |> list.map(fn(t) { + let #(e, #(crc, offset)) = t + central_entry(e, crc, offset) + }) + |> bit_array.concat + let cd_size = bit_array.byte_size(central) + bit_array.concat([ + local, + central, + end_record(list.length(entries), cd_size, cd_offset), + ]) +} + +fn local_header(e: Entry, crc: Int) -> BitArray { + let name = <<e.name:utf8>> + let size = bit_array.byte_size(e.data) + bit_array.concat([ + <<0x04034b50:little-size(32)>>, + <<version_needed:little-size(16)>>, + // flags: none; the name is ascii + <<0:little-size(16)>>, + <<method_stored:little-size(16)>>, + <<dos_time:little-size(16)>>, + <<dos_date:little-size(16)>>, + <<crc:little-size(32)>>, + <<size:little-size(32)>>, + <<size:little-size(32)>>, + <<bit_array.byte_size(name):little-size(16)>>, + // no extra field, which "mimetype" requires + <<0:little-size(16)>>, + name, + ]) +} + +fn central_entry(e: Entry, crc: Int, offset: Int) -> BitArray { + let name = <<e.name:utf8>> + let size = bit_array.byte_size(e.data) + bit_array.concat([ + <<0x02014b50:little-size(32)>>, + // made by + <<version_needed:little-size(16)>>, + <<version_needed:little-size(16)>>, + <<0:little-size(16)>>, + <<method_stored:little-size(16)>>, + <<dos_time:little-size(16)>>, + <<dos_date:little-size(16)>>, + <<crc:little-size(32)>>, + <<size:little-size(32)>>, + <<size:little-size(32)>>, + <<bit_array.byte_size(name):little-size(16)>>, + // extra, comment, disk, internal attributes + <<0:little-size(16)>>, + <<0:little-size(16)>>, + <<0:little-size(16)>>, + <<0:little-size(16)>>, + // external attributes + <<0:little-size(32)>>, + <<offset:little-size(32)>>, + name, + ]) +} + +fn end_record(count: Int, cd_size: Int, cd_offset: Int) -> BitArray { + bit_array.concat([ + <<0x06054b50:little-size(32)>>, + // this disk, and the disk the central directory starts on + <<0:little-size(16)>>, + <<0:little-size(16)>>, + <<count:little-size(16)>>, + <<count:little-size(16)>>, + <<cd_size:little-size(32)>>, + <<cd_offset:little-size(32)>>, + // no archive comment + <<0:little-size(16)>>, + ]) +} + +/// Erlang has crc32 in the zlib module, which is built in: the NIF is for +/// compression, and the checksum is not. +@external(erlang, "erlang", "crc32") +fn crc32(data: BitArray) -> Int |
