
The actual source of kmalloc.org/people.html, built with motorino build -donut. View-source on any page of the site right now and you will see the same thing.
motorino is the static site generator I wrote for kmalloc.org and nothing else. I introduced it here a week ago, at version 1.3.0. Its “What it still lacks” list did not mention minifying the output, because at the time I had not thought of it. Then, in chat, I had the kind of idea that should be stopped at the door: minify the HTML and CSS, sure, that is normal. And also, optionally, lay the minified source out as a donut. A real one, with a hole in the middle, the way a1k0n’s donut.c spins an ASCII torus on your terminal. It shipped in 1.4.0 three days later as issue #14, and it is live on the site today.
Minify first
A browser does not care about whitespace between an opening <meta> tag and the next one. It cares a great deal about the whitespace between two words in a sentence, and not at all about a newline inside <head>. motorino’s minifier makes that distinction explicit instead of guessing: every boundary between two pieces of the page is one of three kinds.
- Space: whitespace that shows. Between two words, between an inline tag and a word. Any amount of it, a newline included, means the same thing, so it collapses to one space.
- Free: no whitespace needed, and adding or removing it changes nothing. Next to a block-level tag, inside
<head>, between CSS declarations. It is written as nothing. - None: no whitespace allowed at all. Inside a word, inside a quoted attribute, inside a CSS string or a
url().
pre, textarea and script are copied untouched, the text of style is minified as CSS along the same three rules, comments go, and the doctype stays the first line. motorino build -minify writes every .html file this way. On kmalloc.org it brings the HTML down from 121Â KB to 79Â KB across the whole site, with nothing a browser would notice.
That part is normal practice and would make for a short, boring changelog entry. It is also the only reason the next part works at all: by the time the donut runs, the page is already a flat sequence of pieces, each one glued to its neighbour by a Space, a Free, or a None boundary. A Space or a Free boundary is a place that can absorb more whitespace without changing a thing the browser sees. The donut’s entire trick is finding those places and putting a very specific amount of whitespace in them.
Then draw a ring with it
motorino build -donut does everything -minify does, and then lays the result out so that it traces a torus, the way donut.c draws one, except standing still and made of your own HTML.
The shape is sized to the page, not the other way around:
- A character is about twice as tall as it is wide, so the ring is built as an ellipse with
Rx = 2·Ry, wide enough to look round in a monospace font instead of squashed. - The hole is 45% of the ring’s radius.
- The ring is grown, one row of radius at a time, until 85% of its area would be enough to hold the minified source. The other 15% is the room justification needs.
Each row of the ring is written as indent + segment 1 + hole + segment 2, except at the very top and bottom, where there is no hole yet and a row is one segment spanning the full width. The minified pieces are dropped into the segments in order, filling each row from where the last one stopped, and then the row is justified to its exact width by widening its Space and Free boundaries with extra spaces: the same boundaries minify just finished shrinking to the bare minimum. Nothing is added where the grammar said None: a word, a quoted value, a CSS string never gets a space pushed into the middle of it. If one piece is longer than the segment it is supposed to sit in (a long meta value, usually), it gets its own full line and the row after it is skipped, so the ring keeps its shape instead of getting a bite taken out of it. The source ends when the pieces end; the last row is simply not justified.
Every space the donut adds back sits at a boundary the minifier proved was safe to touch. The DOM a browser builds from the donut-shaped file is identical, token for token, to the DOM it builds from the minified one. I did not take this on faith: the test suite builds both, strips every byte of whitespace outside the raw elements from each, and asserts the two strings are equal.
The part I did not expect
I diffed the donut build against the completely unminified build of the same site, file by file, out of curiosity about how much the shape would cost. Every single page, HTML byte for byte, came out exactly the same size as the page with no minification at all:
| page | plain | minified | donut |
|---|---|---|---|
manifesto.html | 29032 | 19303 | 29032 |
privacy.html | 18177 | 12095 | 18177 |
index.html | 10812 | 6864 | 10812 |
people.html | 10022 | 6273 | 10022 |
Nine for nine, across the whole site. That is not a coincidence I planned for: the fill constant (85%) was picked by eye, for a ring that reads as a ring and not as a crowded blob or a sparse one. But it means the donut has a clean way to describe its own cost: it gives back, as justification padding, exactly the bytes minifying took away. You can have the smaller page, or you can have the donut; the second one is the first one wearing the saved bytes as a costume. Nobody asked for this property and I am not sure it holds for every possible page, but it held for mine, which is the only page motorino has ever had to build.
How to get one
motorino build -minify # smaller HTML, same DOM
motorino build -donut # same, but shaped like a ring
Or in site.json:
{ "minify": "donut" }
("minify" for the plain version, anything else or missing is off, so a development build stays readable.) Only .html files are touched; feed.xml, robots.txt and the assets go through untouched either way. kmalloc.org has run with "minify": "donut" since the merge. View-source on any page and you are looking at a torus.
Why
Nobody needs this. The site was already small, the saving is a few kilobytes nobody downloading it would notice, and a page’s worth of HTML arranged as a torus is not more readable, not more secure, not more anything measurable. I built it because donut.c is one of the things that made me want to understand computers in the first place, and because a static site generator that only does what the site strictly needs still has room, once in a while, for the thing that does not need to exist and gets built anyway. motorino’s own README says it is not meant for your site. It was never trying to be reasonable about this one either.
The code is at git.kmalloc.org/squid/motorino, the internal/minify package specifically, and the site it builds is at git.kmalloc.org/squid/kmalloc.org.