My manuscript, a fantasy novel I am writing, reads beautifully in a browser. The same words, piped into an EPUB (the standard e-book file format), looked broken on the first e-ink screen they touched. E-ink is the low-power paper-like display on most e-readers. Nothing was missing and nothing was reworded, so I assumed the book needed a better export. It took me a chunk of the week to see that the book was never the thing being tested.
The EPUB was, and EPUB is barely a document format at all. It is a shape-fit problem. The publisher doesn't get to specify the shape, the reader does, and my job is to make the content flex to whatever they choose.
Where the fit breaks
The manuscript is LitRPG, fiction where characters follow game-like rules, so it is full of "System boxes", the little ASCII game windows the genre uses for status screens and level-ups. In my working copy of the text, each one is a fenced block of box-drawing characters: corner glyphs, side rails and a header row, all drawn at the right column widths. On a monitor it looks like a terminal window floating in the prose, and I'm fond of it.
Then the same block meets an e-ink screen. The boxes run 53 to 78 characters wide, and e-ink doesn't scroll sideways, so a line either wraps or clips. A reader who picks a large font turns a 78-character line into two lines, and the second one lands under the left border instead of inside the box. The characters that made the box a box become noise in the margin.
The tell was that none of this happened on my machine. The <pre> tag I was using is honest about its nature: it never reflows, it only overflows. On a wide window, overflow is invisible. On a 6-inch screen, it is the whole experience. The format looked correct because I had never looked at it under the conditions it would be read in.
The reader's settings are the spec
Once I treated EPUB as a constraint problem, three things the reader controls became the real specification for the build:
- The reader chooses the font size, so any layout that assumes a fixed line width is a bet I will lose.
- The reader chooses the theme, whether day, sepia or dark, so any colour I hard-code will fight a theme I can't see.
- The reader chooses a device I don't own, so anything that depends on scrolling sideways is broken before they open it.
That list rewrote the plan. The task stopped being "convert markdown to EPUB" and became "make every block reflow under any width, font and theme, and then prove it did".
Rebuilding the boxes so they flex
The boxes are where the work went. The old approach drew a box with characters, which is a picture of a container. The new approach is a real one: a bordered div holding short paragraphs, with the header as a centred bold line and the rows as indented text underneath. The conversion step peels the drawing characters off and throws them away, and the structure they were faking becomes real structure.
def sysbox(body: str) -> str:
lines = body.rstrip("\n").split("\n")
drawn = any(c in body for c in "╔║")
head, rows, in_head = [], [], drawn
for raw in lines:
if drawn and raw.lstrip().startswith(("╔", "╚")):
continue
if drawn and raw.lstrip().startswith("╠"):
in_head = False
continue
text = raw.strip().strip("║").strip() if drawn else raw.strip()
if in_head:
head.append(text)
else:
rows.append(text)
The same content now renders as a reflowable block at whatever font size the reader picks.
Constraints I got for free
Once the boxes could flex, the publishing guidelines of the e-book platform I'm building for turned out to be a decent design brief. Four of its rules are worth stealing for any e-book work:
- Sibling selectors and pseudo-elements are off the table, so "first paragraph of a chapter" and "scene break" can't be derived with clever CSS. They get stamped in as explicit classes in a cleanup step after conversion.
font-variant: small-capsrenders inconsistently across devices, so chapter labels are uppercased in the text itself, withletter-spacingdoing the fancy part.- Transparency flattens to white on some devices, so the four paintings, one for each Part of the book, are opaque JPEGs at 1600px with real alt text.
- Fixed black borders look wrong in sepia and dark themes, so the border colour inherits from the text and follows the reader's theme.
None of these were aesthetic calls I got to make. They were the reader's settings, discovered the hard way and promoted to build constraints.
Proving the words didn't change
The last step was an audit. I wanted hard evidence that the rebuild touched formatting and nothing else, because a "cleaner export" that quietly rewrote a sentence would be worse than a broken one. The check is small and boring. The build script re-derives the plain text of every block after the transform, my working copy already has that plain text on file, and the two must match exactly. If they don't, the build fails and nothing ships.
It passes, and the prose word count is unchanged at 162,174. That number is the real deliverable, more than any of the CSS: a pipeline that changes the shape of a book and provably leaves the words alone.
Build for the shape
"A clean export" is the wrong goal when the reader controls the presentation. Their font size, theme, screen width and device quirks are the spec, and all of it was knowable in advance. I could have guessed most of it from the guidelines before writing a line of code. I didn't, because the browser copy looked good and the browser copy was the thing in front of me.
That answers the puzzle I started with. Identical words broke on one screen because I had been checking a snapshot, and the reader on the e-ink screen, the one the book was built for, never got a vote until the file shipped. Build for the shape they choose, and the snapshot takes care of itself.