Guidedog 手册 0.6.3
语言
本页内容
Guidedog / 文档 0.6.3

制作项目主题

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

1. 从真实项目开始

使用 Guidedog 0.6.2 或更新版本。在准备创建 orbit 的父目录运行:

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

将生成的 orbit/_templates/book.typ 在同一目录内重命名为 base-book.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
    base-book.typ          # Generated by quickstart; renamed, not replaced.
    book.typ
    palette.typ
  _static/
    guidedog.css           # Generated by quickstart.
    guidedog.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。背景会变时,不要将文字固定为不适应主题的颜色。

浏览器按页面语言断词,不等同于 Typst 的段落优化。本手册可选的英文网页优化器见 适合阅读的排版;本例使用浏览器原生排版。

当前 Guidedog 构建中,带 :class: graphviz-adaptive 的图会跟随所选主题。普通图保留自己的颜色,旧版主机仍能按浅色配色渲染相同 DOT。语义节点类和打印行为见 用图表解释概念。

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

// 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 交给应用样式的函数,规则才会在正文中生效。网页 CSS 不影响 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,完成第一步的重命名。标签为空时,在 html_context 中设置 edition_label。CSS 缺失时,同时检查 html_static_path 和 html_css_files。先阅读诊断中的文件、行号和修正建议,再调整相关设置。