跳到主要内容
食谱编号15

排版食谱 · 第2章 · 字体与文字

逐行排诗

每首诗是一个照印刷样子书写的:::verse块:1918年版的缩进写成行首空格,转行比本行悬挂2 em,空行分隔诗节。

本页内容
类型
诗歌
输出
Canvas
难度
中级
Postext
已用Postext 1.65.0测试
需要≥ 1.34.0
许可证
更新于2026年10月16日
代码MIT · 文本CC BY 4.0

第2–3页,共8页

  • 英文样例:尚无中文版本
  • 成品尺寸140 × 216 mm
  • 1栏
  • Sorts Mill Goudy 11/15
  • Italiana
  • Marcellus SC
  • 8页
  • 难度
  • Postext 1.57.1
  • 排版用时59 ms
  • 159行代码

简单来说

一本收录霍普金斯四首自然诗的小册子。示例演示怎样让每行诗各自成行,保留诗人原来的缩进,诗节之间空一行。

成品一览

一本收录杰拉德·曼利·霍普金斯四首自然诗的小诗集,140 × 216 mm,文本采用1918年初版。简扉页之后,一张蕨草和花楸的物影照片作为卷首插图,与用Italiana排的书名页相对。目录按罗马数字和斜体诗题列出每首诗,用疏排的点通向页码。每首诗另起一页,上方是灰绿色的罗马数字和斜体诗题。每首诗是一个:::verse块,缩进与1918年版相同:Pied Beauty的诗行按韵脚缩进,The Windhover的长行转行后悬挂缩进4 em,是其缩进深度的两倍。诗节之间空一行,除了霍普金斯自己断开的地方,没有任何词被断开。日期排在每首诗之下,右对齐,最后一首之后是一枝花楸。诗页在页脚居中印页码。

这道食谱解答

  • 怎样排诗歌:每句一行,诗节间留空,折行悬挂缩进,不断词?
  • 空行不起作用时,怎样在两个块之间加额外的垂直间距?
  • 怎样添加自动更新的目录(前导符、页码、作者、篇名行)?
  • 怎样排题词、献词、署名,或带大号引号的引文框?
  • 怎样排不编号的插图:饰图、小花饰、标志?

简短回答

script.js · 第31–46行在完整代码中
// A poem is one :::verse block written as it is printed: a line of verse a line, a blank line
// between stanzas (one line of the grid), and spaces before an indented line, two to an em:
//   :::verse{style="verse" align=start}
//   The world is charged with the grandeur of God.
//       It will flame out, like shining from shook foil;
//
//   And for all this, nature is never spent;
//   :::
// align=start sets the poem flush with the margin (the default centres it on its longest line).
const verse = {
  id: 'verse',
  hangingIndent: em(2), // a turnover hangs 2 em past its line's start: 4 em under a 2 em indent
  // Verse keeps its words whole, even where bodyText.hyphenation.ragged is switched on.
  hyphenation: false,
};
// Hook-up: paragraphStyles: [verse, …]; the Markdown goes to buildDocument as it is written.

用料

类型
Sorts Mill Goudy, Italiana, Marcellus SC(SIL OFL 1.1)
素材
  • plate-1120.jpg
  • The frontispiece photogram of fern and rowan (Generated With Diffusion Models, 原创)
  • The rowan ornament, drawn in code in the page’s palette (Ignacio Ferro, CC BY 4.0)

做法

#1 · 一首诗是一个:::verse块

:::verse块把Markdown的每一行保留为一行诗,所以诗照印刷的样子书写(:::verse)。行首空格就是缩进,两个空格一个em,因此押韵的诗行与1918年版一样缩进相同的距离。诗节之间的空行留出基线网格的一行,落在页首时去掉。比版心长的诗行在词间空格处转行,按诗行样式的hangingIndent悬挂,从该行自身的起点算起:无缩进的行悬挂2 em,缩进2 em的行下面悬挂到4 em,转行因此不会被当成新的一行。align=start让每首诗与页边距对齐;没有它,诗会以最长的一行为准居中。诗行不加连字符断词,分页时一行诗也绝不与它的转行分开。

The Windhover, page 6: the lines that rhyme in -iding start 2 em in, and the four lines too long for the page turn over and hang 4 em in.

#2 · 诗周围的文字也有样式

script.js · 第50–54行在完整代码中
const lineStyles = [
  { id: 'dedication', fontSize: pt(9.5), marginBottom: pt(LEAD) }, // under The Windhover
  { id: 'date', fontFamily: 'Marcellus SC', fontSize: pt(8), color: col('muted'),
    textAlign: 'right', marginTop: pt(LEAD) }, // a line of space above, flush right
];

The Windhover上方的献词和每首诗下方的地点与日期都不是诗行,所以各自是只含一个段落的:::paragraphs容器,使用自己的样式。这两个样式都不设行距,外边距都是一行(15 pt),所以诗行仍落在正文的网格上。日期排在诗下一行处,右对齐,用8 pt的Marcellus SC,也就是页码的字体。

#3 · 每首诗在自己的编号下另起一页

script.js · 第58–73行在完整代码中
// '{1:I}' numbers the poems I to IV: {number} prints the numeral here, :::toc in the contents.
// The head is HEAD_LINES grid lines (minHeight, no bottom margin), so each poem starts on the same
// line; numeral and title fill 4, or 5 if the title wraps.
const HEAD_LINES = 6;
const poemHead = { enabled: true, minHeight: pt(LEAD * HEAD_LINES), slot: { elements: [
  { kind: 'text', id: 'numeral', content: '{number}', fontFamily: 'Italiana', fontSize: pt(22),
    color: col('sage'), align: 'left',
    placement: { anchor: { to: 'container', edge: 'top-left' } } },
  { kind: 'text', id: 'title', content: '{titleText}', fontFamily: 'Sorts Mill Goudy',
    italic: true, fontSize: pt(17), color: col('ink'), align: 'left',
    placement: { anchor: { to: '#numeral', edge: 'below' }, offset: { y: mm(2) },
      size: { width: 'fill' } } },
] } };
// Parity 'any': the next page, recto or verso, with no blank page between two poems.
const poems = { level: 1, numberingTemplate: '{1:I}', advancedDesign: poemHead,
  marginBottom: pt(0), breakBefore: { enabled: true, parity: 'any' } };

这个标题不印它的文字,而是在栏里印一个版面设计。在这个设计里,{number}是按numberingTemplate: '{1:I}'格式化的计数,从I到IV,{titleText}是诗题(通栏与高级版面设计)。这一级标题没有下外边距,minHeight让每个标题都占六行网格(31.75 mm)。罗马数字和一行诗题占其中四行,诗题折行时占五行,所以每首诗都从页面上的同一行开始。在第6页上,这一行放的是献词。奇偶设为'any'时分页到下一页,不论左右页,所以诗与诗之间没有空白页。

#4 · 前置部分由标题组成

script.js · 第77–106行在完整代码中
// Headings too, each on a page of its own, with no number, contents entry or folio.
// A style restates the break, or inherits the poems' (gotcha: style-inherits-break).
const leaf = { numbered: false, toc: false, span: 'page', footer: { elements: [] },
  breakBefore: { enabled: true, parity: 'any' } };
const onPage = (y, width) => ({ anchor: { to: 'page', edge: 'top' }, offset: { y: mm(y) },
  ...(width && { size: { width: mm(width) } }) }); // centred, y mm below the trim
const face = (id, content, font, size, y, extra = {}) => ({ kind: 'text', id, content,
  fontFamily: font, fontSize: pt(size), color: col('ink'), align: 'center',
  placement: onPage(y), ...extra });
const image = (id, placement) => ({ kind: 'image', id, resourceId: id, placement });
const design = (...elements) => ({ enabled: true, slot: { elements } });
const front = [
  { id: 'half-title', ...leaf,
    advancedDesign: design(face('title', '{titleText}', 'Italiana', 20, 60)) },
  { id: 'plate', ...leaf, advancedDesign: design( // span 'page': a column's foot would cut it
    image('plate', { anchor: { to: 'bleed', edge: 'top-left' }, size: { width: 'fill' } }),
    face('caption', '{attr.caption}', 'Sorts Mill Goudy', 8.5, 203,
      { italic: true, color: col('muted') })) },
  { id: 'title-page', ...leaf, advancedDesign: design(
    face('author', '{author}', 'Marcellus SC', 10, 46, // {author}, {title}: the frontmatter
      { letterSpacing: pt(2.4), textTransform: 'uppercase' }),
    face('title', '{title}', 'Italiana', 54, 56, { lineHeight: 1 }),
    face('subtitle', '{subtitle}', 'Sorts Mill Goudy', 13, 80,
      { italic: true, color: col('sage') }),
    image('sprig', onPage(96, 30)),
    face('press', '{attr.press}', 'Marcellus SC', 8.5, 182,
      { letterSpacing: pt(1.8), color: col('muted') })) },
  { id: 'contents', ...leaf, span: 'column', advancedDesign: { enabled: false },
    fontFamily: 'Italiana', fontSize: pt(24), lineHeight: pt(LEAD * 2), marginBottom: pt(LEAD) },
];

标题样式可以分页到新的一页并在那里画出版面设计,所以简扉页、卷首插图和书名页各是一个标题,各有自己的样式。numbered: false让它们不参与计数,第一首诗仍是I。toc: false让它们不进目录,空的页脚让它们的页面不带页码(标题样式)。这些样式设为通栏,因为留在栏内的版面设计会在版心底部被裁掉,那样满版出血的插图下面一截会被裁去,题注也印不出来。题注来自标题的caption属性;书名页上的作者、书名和副书名来自前置元数据。

#5 · 目录自动填写

script.js · 第110–116行在完整代码中
// The numerals stand on the titles' baseline, in Italiana and sage, at the titles' size.
const contents = {
  levels: [{ level: 1, italic: true, marginBottom: pt(LEAD), numberFontFamily: 'Italiana',
    numberFontWeight: 400, numberColor: col('sage'), numberWidth: mm(6), numberGap: mm(3) }],
  pageNumber: { fontFamily: 'Marcellus SC', fontSize: pt(9), color: col('muted'), width: mm(6) },
  leader: { char: '. ', gap: mm(2) }, // spaced dots, right-aligned so they line up
};

:::toc列出样式允许进入目录的每个标题,罗马数字出自同一个模板,页码取标题所在页的页码标签。构建会反复排版,直到这些页码标签不再变化(目录)。目录页本身的标题Contents使用带numbered: false的样式;没有它,Contents就会成为第I首诗。前导符'. '在每个点后加一个空格。点是右对齐的,所以各条目之间的点上下对齐;它们用页码的字体和颜色印出,因为toc.leader没有颜色设置。

#6 · 装饰图案是一种不带标签的资源类型

script.js · 第120–124行在完整代码中
// No caption prefix and no caption: the sprig prints bare, where ::resource{id="sprig"} puts
// it (double quotes: gotcha resource-double-quotes), a fifth of the measure wide, centred.
const ornament = { id: 'ornament', name: 'Ornament', shortLabel: '', captionPrefix: '',
  numberingTemplate: '{n}', resetOn: 'never', counterFormat: 'decimal',
  defaultPlacement: { position: 'here', width: 0.2, align: 'center' } };

正文中的图片是资源,每个资源都有一个类型。装饰图案类型的题注前缀为空,花楸枝也没有题注,所以下面不会印出“Figure 1”之类的标签(资源类型)。这个类型的defaultPlacement把花楸枝放在::resource{id="sprig"}所在的位置,居中,宽度为行长的五分之一,所以Inversnaid下面的尾花只占一行Markdown。resourceTypes会替换默认列表,所以这本书没有Figure或Table类型。要保留它们,就把defaultResourceTypes()展开放在装饰图案之前。

完整食谱

沙盒
// ═══ Postext Cookbook · Nº 015 · Poems set line by line ═════════════════════════════
// https://postext.dev/en/cookbook/poetry-collection
// Code: MIT · Text: G. M. Hopkins, Poems, 1918 (PD) · Plate: diffusion models · Sprig: code
// Fonts: Sorts Mill Goudy, Italiana, Marcellus SC (SIL OFL 1.1) · Needs postext ≥ 1.34.0
import {
  buildDocumentWithFonts, renderPageToCanvas, registerResourceImage,
} from 'https://esm.sh/postext';

const LANG = 'en'; // @lang: the language of the sample document ('en' | 'es')
const RECIPE = 'poetry-collection';

// ─── 1 · Design ─────────────────────────────────────────────────────────────
const palette = { // every colour in the config links to one of these
  ink: '#26221f', // the text: a warm near-black
  sage: '#56673f', // the one accent: numerals, the subtitle, the ornament's leaves
  sepia: '#8c5f3a', // used for the rowan berries only
  muted: '#746a60', // dates, folios, leaders and the colophon
  paper: '#fbf8f2', // the page
};
// A colour is its hex and the palette entry it follows.
const col = (id) => ({ hex: palette[id], model: 'hex', paletteId: id });
const colorPalette = [
  ...Object.entries(palette).map(([id, hex]) => ({ id, name: id, value: { hex, model: 'hex' } })),
  // The engine's defaults link to 'main-color': point it at the accent, so nothing prints blue.
  { id: 'main-color', name: 'accent (defaults)', value: { hex: palette.sage, model: 'hex' } },
];
const TRIM_W = 140, TRIM_H = 216; // mm: a poetry trim, tall enough for a sonnet's turnovers
const LEAD = 15; // pt: the body's leading, the grid every line of verse sits on

// #region answer: verse: a :::verse block keeps each line, its indent, the stanza breaks
// A poem is one :::verse block written as it is printed: a line of verse a line, a blank line
// between stanzas (one line of the grid), and spaces before an indented line, two to an em:
//   :::verse{style="verse" align=start}
//   The world is charged with the grandeur of God.
//       It will flame out, like shining from shook foil;
//
//   And for all this, nature is never spent;
//   :::
// align=start sets the poem flush with the margin (the default centres it on its longest line).
const verse = {
  id: 'verse',
  hangingIndent: em(2), // a turnover hangs 2 em past its line's start: 4 em under a 2 em indent
  // Verse keeps its words whole, even where bodyText.hyphenation.ragged is switched on.
  hyphenation: false,
};
// Hook-up: paragraphStyles: [verse, …]; the Markdown goes to buildDocument as it is written.
// #endregion

组合好的script.js可以直接运行:把它粘贴到任何页面的模块脚本中,或在CodePen上打开这道食谱。 GitHub上的食谱文件夹 ↗ (在新标签页中打开)

变化

#转行悬挂得更深

悬挂缩进越深,转行的部分离页边距越远,适合经常转行的诗。

-  hangingIndent: em(2), // 转行悬挂在1 em和2 em缩进之外
+  hangingIndent: em(4), // 转行悬挂在1 em和2 em缩进之外

#转行靠右排

在bodyText.verse中设置turnover: 'right'后,比版心长的诗行,其余部分排在下一行并靠右,前面加开方括号,许多诗集都这样排。

     indentAfterHeading: false, minWordSpacing: 0.8, maxWordSpacing: 1.6,
+    verse: { turnover: 'right' }, // a turnover flush right, behind [
   },

#每首诗都从右页开始

奇偶设为'odd'时,每首诗都从下一个右页开始。全书从八页增加到十一页,三个空白的左页不印页码,因为页码按页面角色筛选。

 const poems = { level: 1, numberingTemplate: '{1:I}', advancedDesign: poemHead,
-  marginBottom: pt(0), breakBefore: { enabled: true, parity: 'any' } };
+  marginBottom: pt(0), breakBefore: { enabled: true, parity: 'odd' } };

#用阿拉伯数字给诗编号

模板的计数样式会同时改变诗页上和目录里的编号。

-const poems = { level: 1, numberingTemplate: '{1:I}', advancedDesign: poemHead,
+const poems = { level: 1, numberingTemplate: '{1}', advancedDesign: poemHead,

常见问题

易错点

段首的'1998. '或'- '会开始一个列表

以数字、句点和空格开头,或以连字符和空格开头的段落,会变成列表项。在数字前放一个连接符(U+2060),对话用长破折号来写。 转义与字面字符 →

易错点

标题样式会继承其级别的分页设置

headingStyles中的条目没有写出的字段都取自它所属的标题级别,breakBefore也不例外。在:::pagebreak之后以H1设置样式的目录页或版权页会继承parity 'odd',结果落在一张空白页之后。给这类样式设置breakBefore: { enabled: false }。 标题样式 →

易错点

::resource{id="…"}只接受双引号

块级嵌入只有写成带双引号的::resource{id="…"}才能被识别;其他写法会作为一行可见文字留在正文中。 图就放在这里 →

  • 一个诗节的诗行之间不要留空行::::verse块中的空行(不论几行)都会开始新的诗节。
  • 以加号加空格开头的行是错行,从上一行结束处开始;要印出加号请写\+。
  • 没有align=start时,每首诗以最长的一行为准居中,各首诗与页边距的距离就各不相同。
  • 要让两个词一起转行,用不换行空格(U+00A0)把它们连起来,如第5页的the soil和bright wings.。
  • pages保留为'all'的页脚元素在空白页上也会印出。这里的页码是两个元素,一个用于章首页,一个用于正文页,所以空白的左页保持空白。

致谢

文本
  • “God’s Grandeur”, “The Windhover”, “Pied Beauty” and “Inversnaid”, with the places and dates from the editor’s notes, in the first edition of Poems of Gerard Manley Hopkins (1918); accents and indents checked against the proofread Wikisource transcription of that edition · Gerard Manley Hopkins; Robert Bridges (editor) · 公有领域
  • The note on the text, the plate’s caption and the colophon · Ignacio Ferro · CC BY 4.0
图片
  • The frontispiece photogram of fern and rowan · Generated With Diffusion Models · 原创
  • The rowan ornament, drawn in code in the page’s palette · Ignacio Ferro · CC BY 4.0
字体
Sorts Mill Goudy (SIL OFL 1.1) · Italiana (SIL OFL 1.1) · Marcellus SC (SIL OFL 1.1)
沙盒