Telling Poems from Prose in a Word File, Then Setting Both with Typst
By Yuluo Han ·
A literary magazine is an awkward thing to typeset automatically. A story wants two justified columns. A poem wants one ragged column, with every line break exactly where the poet put it.
Nobody marks which is which. Zinewright gets a pile of Word files and has to work it out. This post covers the two halves of that problem: a heuristic that guesses, and the Typst that sets the result, including a bug that shipped past a passing test.
Half one: is this a poem?
Word has no concept of a poem. What we have after parsing a .docx is a list of paragraphs. So the detector looks at the shape of the text:
POEM_MIN_LINES, POEM_SHORT_LINE, POEM_LONGEST_LINE = 3, 80, 130
def _looks_like_poem(items):
"""Prose unless nearly every line is short, and the lines are not sentences of dialogue."""
if any(item["list"] for item in items):
return False
lines = [line for item in items if not item["level"] and not item["caption"]
for line in _inline_lines(item)]
widths = [len(line) + len(CJK.findall(line)) for line in lines] # a CJK character is about two Latin letters wide
if len(lines) < POEM_MIN_LINES or max(widths) > POEM_LONGEST_LINE:
return False
short = sum(width <= POEM_SHORT_LINE for width in widths)
stopped = sum(line[-1] in ".!?。!?…\"”’" for line in lines)
spoken = sum(any(mark in line for mark in '"“”「」') for line in lines)
# Many poems end every line with a full stop; short prose gives itself away by its dialogue.
return short >= 0.8 * len(lines) and (stopped <= 0.5 * len(lines) or spoken <= 0.2 * len(lines))
In words: at least three lines, none very long, at least 80% short. That alone also matches a page of quick-fire dialogue, which is why the last line matters:
- If most lines don't end in sentence punctuation, it's verse.
- If most lines do, it could be either. Plenty of poems end every line with a full stop. So look for quotation marks: short lines full of dialogue are prose.
The bias is deliberate. A poem sent through as prose is obvious in the proof and takes one dropdown to fix. Silently treating a short story as a poem, on the other hand, is a mess. So the function returns prose unless it's confident, and the author always gets the last word.
The CJK adjustment counts a Chinese or Japanese character as two Latin letters wide, so one set of thresholds works for both.
Two ways to type a poem
People type poems in Word in two ways. Some use Shift+Enter inside one paragraph per stanza. Others press Enter after every line, with an empty paragraph between stanzas. The importer normalises the second form into the first: consecutive paragraphs merge into a stanza, and an empty paragraph ends it. The manuscript format then has one shape, a stanza paragraph with hard breaks.
Remember that detail. It caused the bug.
Half two: mixed columns in one Typst document
Typst makes per-chapter columns pleasantly direct. set page(columns: ...) inside the loop body applies to that chapter alone, and each chapter starts on a fresh page, which is exactly what a magazine wants.
Chapter titles need one more trick. In a two-column chapter, a title placed normally sits in the first column and wraps badly. A floating placement scoped to the parent runs it across both columns:
#for (index, chapter) in chapters.enumerate() {
if index > 0 { pagebreak() }
let poem = chapter.kind == "poem"
set page(columns: if poem { 1 } else { 2 })
let opener = heading(level: 1, chapter.title)
if poem {
opener
verse(chapter.stanzas)
} else {
place(top + left, float: true, scope: "parent", clearance: 16pt, opener)
chapter.body
}
}
scope: "parent" does the work. Without it, a float stays inside its column. Wide images in Zinewright's magazine layout use the same mechanism.
The bug: a hanging indent that hung everything
Poems need hanging indents. When a long verse line wraps, the overflow has to be indented so that nobody mistakes it for a new line of the poem. Typst has par(hanging-indent:), so the first version was one line:
set par(justify: false, first-line-indent: 0pt, hanging-indent: 1.5em) if poem
A stanza was a paragraph with linebreak() between its verse lines. Here's what the rendered stanza looked like:
The kettle ticks.
The street forgets its name.
A long line that wraps inside the measure and hangs
under itself, the same as every other line.
I count the windows.
hanging-indent means "every line of the paragraph except the first", and a linebreak() doesn't start a paragraph. So all the verse lines but the first were indented, and the wrapped text, the only thing that should have been, was indistinguishable from the rest.
The test suite passed the whole time. It built a magazine with a poem, ran pdftotext -layout, and checked that line order was preserved and that a line with leading spaces was indented more than a line without. Each assertion was true. Nothing checked whether the first line of a stanza aligned with the second, because in my head that wasn't a question.
The fix: one paragraph per verse line
The verse line is the unit that needs the hanging indent, so each one becomes its own paragraph. Tight paragraph spacing stands in for line spacing, and a block around each stanza provides the gap:
#let verse(stanzas) = {
set par(justify: false, first-line-indent: 0pt, hanging-indent: 1.5em, spacing: 0.65em)
for lines in stanzas {
block(below: 1.4em, for line in lines { par(line) })
}
}
Now only true wraps hang:
The kettle ticks.
The street forgets its name.
A long line that wraps inside the measure and hangs
under itself, so nobody mistakes the overflow.
I count the windows.
One more verse detail
Poets use leading spaces on purpose, and Typst collapses whitespace in markup. Since manuscript text arrives as strings, the template measures the spaces and converts them to explicit space, half an em each:
let kept = n.text.trim(" ", at: start)
if kept.len() < n.text.len() { body = [#h((n.text.len() - kept.len()) * 0.5em)#smart(kept)] }
It keeps a stepped poem stepped. It won't reproduce a poem with lines placed freely across the page, and I'd rather say so than pretend. Concrete poetry needs a designer.
What I'd take from this
- For a guess that a person can correct, choose the failure that's cheap to see and undo.
- Normalising input into one shape is still the right call. It just means a bad assumption about that shape reaches everything.
- A text-extraction test can't see layout. It proved the words were in the right order. Only a look at the rendered page showed that the poem was wrong. Golden-image checks for a handful of pages would have caught this on the first run.
You can try the import on your own manuscript at zinewright.com. If you maintain a better detector, or have a poem that breaks this one, send it to support@zinewright.com. I'd enjoy the bug report.