Guidedog 手册 1.0.0-beta.16
语言
本页内容
Guidedog / 文档 1.0.0-beta.16

制作项目主题

主题让同一份内容既成为网站,也成为书籍。本教程制作 Orbit 主题,统一视觉风格,使用 HTML 局部模板和宏,并拆分 PDF 模板。不需要插件或编译器。

1. 从真实项目开始

请使用 Guidedog 1.0.0-beta.9 或更高版本。在准备存放 orbit 的父目录中运行:

guidedog quickstart orbit -q -p "Orbit Field Notes" -a "Orbit contributors" -v 1.0

在同一目录下将生成的 orbit/_templates/book.typ 重命名为 base-book.typ,并保留同目录的 theme.typ;重命名后的图书模板继续提供经过验证的页面版式、封面、页眉、目录、代码样式及段落断行优化。

下载 conf.toml、index.rst 和 reading.rst。前两份保存到 orbit,第三份保存到 orbit/chapters。完成后的文件结构如下:

orbit/
  conf.toml
  index.rst
  chapters/reading.rst
  _templates/
    layout.html
    macros/ui.html
    partials/edition.html
    theme.typ              # Shared PDF theme generated by quickstart.
    discussion.typ         # Discussion layout generated by quickstart.
    article.typ            # Standalone article layout generated by quickstart.
    base-book.typ          # Generated by quickstart; renamed, not replaced.
    book.typ
    palette.typ
  _static/
    guidedog.css           # Generated by quickstart.
    guidedog.js            # Generated by quickstart.
    typography.js          # Generated by quickstart.
    theme.css

2. 设定契约

下载的配置指定本地模板和静态文件目录。html_css_files 在默认样式表之后添加主题 CSS;html_context 向 Jinja 传入项目值。显式指定 PDF 文件名,便于查找输出。

代码清单 3 orbit/conf.toml
project = "Orbit Field Notes"
author = "Orbit contributors"
version = "1.0"
release = "1.0"
language = "en"
root_doc = "index"
source_suffix = [".rst"]
exclude_patterns = ["_build", "README.md"]
templates_path = ["_templates"]
html_static_path = ["_static"]
html_css_files = ["theme.css"]
numfig = true
pdf_paper_size = "a4"

[html_theme_options]
accent = "#2658a0"

[html_context]
edition_label = "Field edition"

[[pdf_documents]]
root = "index"
file = "orbit-notes.pdf"
title = "Orbit Field Notes"
author = "Orbit contributors"

本教程中的路径都是项目文件。HTML 模板通过 templates_path 查找。PDF 入口始终是源目录下的 _templates/book.typ。修改 templates_path 不会改变它的位置。

3. 扩展 HTML 布局

将 layout.html 保存为 orbit/_templates/layout.html,完整替换生成的文件:

代码清单 4 orbit/_templates/layout.html
{% extends "!layout.html" %}
{% import "macros/ui.html" as ui %}

{% block extrahead %}
{{ super() }}
<meta name="theme-name" content="Orbit field notes">
{% endblock %}

{% block body %}
<p class="orbit-kicker">{{ ui.tag(edition_label) }}</p>
{{ super() }}
{% endblock %}

{% block footer %}
{{ super() }}
{% include "partials/edition.html" %}
{% endblock %}

!layout.html 选择 Guidedog 内置布局。没有 ! 时,名为 layout.html 的模板会试图继承自己。super() 保留父块内容。导航、搜索、主题控件和无障碍地标都留在基础布局中;我们的块只添加标签、元数据和版本页脚。

主题改变呈现;构建插件改变源文本、完整 HTML 或生成资源。模板无法表达所需操作时,参见 编写插件。一般布局修改不需要原生代码。

4. 复用重复的标记

创建 orbit/_templates/macros 和 orbit/_templates/partials,将 ui.html、edition.html 保存到对应路径:

代码清单 5 orbit/_templates/macros/ui.html
{% macro tag(label) %}<span class="orbit-tag">{{ label|e }}</span>{% endmacro %}
代码清单 6 orbit/_templates/partials/edition.html
<p class="orbit-edition">
  <span>{{ project|e }} / {{ release|e }}</span>
  <a href="{{ pathto('chapters/reading') }}">Reading a measurement</a>
</p>

宏接收明确的参数,包含的局部模板使用当前页面上下文。普通项目值用 |e 转义,文档链接用 pathto。硬写 chapters/reading.html 会在嵌套页面或其他 HTML 构建器中失效。

5. 适应读者的主题选择

将 theme.css 保存为 orbit/_static/theme.css:

代码清单 7 orbit/_static/theme.css
/* Typography and measure are shared. Color follows the reader's chosen scheme. */
:root {
  --gd-measure: 43rem;
  --gd-radius: 4px;
  --gd-bg: #fdfbf6;
  --gd-bg-soft: #f3f0e7;
  --gd-text: #202c28;
  --gd-muted: #53605a;
}
:root[data-theme="dark"] {
  --gd-bg: #141c22; --gd-bg-soft: #1b2832;
  --gd-text: #eaf1f6; --gd-muted: #b0c0cb;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --gd-bg: #141c22; --gd-bg-soft: #1b2832;
    --gd-text: #eaf1f6; --gd-muted: #b0c0cb;
  }
}
.gd-article { font-family: Georgia, "Noto Serif", serif; font-size: 1.12rem; }
.gd-article p { line-height: 1.7; hyphens: auto; text-wrap: pretty; }
.gd-article :is(h1, h2, h3) { font-weight: 700; hyphens: none; text-wrap: balance; }
.gd-article h1 { font-size: clamp(2.4rem, 5vw, 3.6rem); letter-spacing: -.025em; }
.gd-header { border-top: 3px solid var(--gd-link); }
.orbit-kicker { margin-bottom: 1rem; }
.orbit-tag {
  display: inline-block; padding: .2rem .65rem;
  font: 700 .75rem/1.5 var(--gd-sans); letter-spacing: .08em;
  border: 1px solid var(--gd-link); border-radius: 3px; color: var(--gd-link);
}
.orbit-edition {
  display: flex; flex-wrap: wrap; gap: .5rem 1rem; justify-content: space-between;
  font: .85rem/1.6 var(--gd-sans); color: var(--gd-muted);
}
img.graphviz-adaptive { display: block; margin: 1.5rem auto; padding: 1rem; }
@media print {
  :root { color-scheme: light; --gd-bg: #fff; --gd-text: #202c28; --gd-muted: #53605a; }
  .orbit-tag { color: #202c28; border-color: currentColor; }
  .gd-article p { orphans: 3; widows: 3; }
}

内置主题默认跟随系统配色方案,页面自带的切换控件可选择跟随系统、浅色或深色并记住选择。显式深色规则与系统媒体查询共用同一组样式变量,字体排印和间距设置也保持共享,无需额外编写 JavaScript。如果背景色会随主题切换,切勿将文字设为固定颜色。

_static/typography.js 使用 Knuth-Plass 断行算法和 Liang 断字算法优化桌面端英文段落,而在禁用 JavaScript 或窄屏设备上,guidedog.css 会平滑回退到 text-wrap: pretty;参见 适合阅读的排版。

当前 Guidedog 构建中,带 :class: graphviz-adaptive 的图形会跟随所选配色方案。普通图形保留自身颜色,旧版宿主仍会按浅色配色渲染同一份 DOT 源码。语义节点类与印刷表现详见 使用 Graphviz 和 Typst 绘制图表。

6. 包装 PDF 书籍模板

将 palette.typ 和 book.typ 保存到 orbit/_templates。这是两个新文件,旁边的 base-book.typ 要保留。

代码清单 8 orbit/_templates/palette.typ
#let palette = (
  accent: rgb("#2658a0"),
  size: 11pt,
  serif: ("Libertinus Serif", "New Computer Modern"),
  sans: ("Libertinus Sans", "DejaVu Sans", "Libertinus Serif"),
)
代码清单 9 orbit/_templates/book.typ
#import "base-book.typ": book as base-book
#import "palette.typ": palette
// guidedog: margin-x = 31mm

// Keep the host's parameters explicit. The final positional argument is content.
#let book(
  title: "", author: "", version: "", date: "", lang: "en", paper: "a4",
  numbering: false, logo: none, copyright: "", version-label: "Version", body,
) = base-book(
  title: title, author: author, version: version, date: date, lang: lang,
  paper: paper, numbering: numbering, logo: logo, copyright: copyright,
  version-label: version-label, accent: palette.accent, size: palette.size,
  serif: palette.serif, sans: palette.sans, page-ref: n => [p. #n], body,
)

宿主调用 book 函数并传入元数据与章节正文。包装函数将这些参数转发给生成的基础模板,并注入自定义配色。将 body 传入样式函数可确保样式规则作用于全部正文。网页样式表不影响 PDF,Typst 模板也不影响 HTML。

基础模板负责按语言断词并优化断行。包装函数保留这些规则,使用与网页相配的蓝色,并为文档交叉引用添加页码。PDF 始终采用适合打印的浅色配色。

7. 构建并检查两种输出

回到第一步的同一个父目录运行:

guidedog build html orbit -W
guidedog build pdf orbit -W
guidedog build dirhtml orbit -W
guidedog build singlehtml orbit -W
guidedog serve orbit

打开 serve 打印的网址。不只看首页,还要检查章节和生成的搜索页面。尝试浅色、深色及系统模式,缩放窗口,用键盘操作导航和搜索。修改页脚局部模板,保存并刷新,预览服务会重建依赖它的页面。

在 PDF 阅读器中打开 orbit/_build/pdf/orbit-notes-en.pdf。Guidedog 会在配置的文件名后添加语言后缀。检查目录、章节首页、公式、表格、代码和图示,留意裁切、松散行距和缺字。其他输出位于 _build/html、_build/dirhtml 和 _build/singlehtml。用 Ctrl+C 停止服务器。

8. 复用和维护主题

用 guidedog quickstart 创建另一个项目,将完成的 _templates 目录和 _static/theme.css 复制过去。在配置中加入 html_css_files = ["theme.css"]、强调色选项和 edition_label 上下文值。将页脚文档链接改为新项目中存在的页面,保留新项目自己的元数据和源文件。

复制的 PDF 基础模板让主题自成一体。升级 Guidedog 时,我们新建一个临时的 quickstart 项目,把其中的模板与保存的基础模板进行比较,合入需要的改动,然后重新构建所有目标。内置的 HTML 布局也可能变化。少量的块覆盖比另一整套布局更容易维护。所有变量和块的说明见 模板。

若构建报告缺少 base-book.typ,请先完成第 1 步的重命名。若标签为空,请在 html_context 中设置 edition_label。若样式缺失,请一并检查 html_static_path 和 html_css_files。切勿盲目改动无关配置,应先阅读诊断信息中的文件名、行号与修正建议。