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

制作一本小书

网站是一张由链接相连的页面网,读者可以按任意顺序浏览。书籍则需要精心安排的阅读顺序,并带有编号的章节、页眉和前置页。

在 Guidedog 中,目录指令(toctree)同时服务于两种格式。同一个 toctree 既把文档组织成网页的侧栏树,也排成 PDF 书籍的章节顺序。

根文档的 toctree 在网页中形成交互导航和面包屑,在打印版中形成编号章节。
图 4 同一个 toctree 同时决定网页导航和书籍章节顺序。

1. 添加新章节

在 notes 项目中,我们新建一个名为 measurement.rst 的文档:

代码清单 1 notes/measurement.rst
Measurement
===========

A scientific measurement consists of a numerical magnitude and an explicit unit.
Always report both. A bare number leaves the reader guessing.

.. _measurement-units:

Units of measurement
--------------------

Maintain a single, consistent unit system within every comparison.

When working with the International System of Units (SI):

* Length is expressed in meters (m).
* Mass is expressed in kilograms (kg).
* Time is expressed in seconds (s).

Prefixes scale units by powers of ten, simplifying very large or small values.

注意标题正上方的节标签 .. _measurement-units:。标签定义了交叉引用的目标,在网页和印刷版中的效果相同。

2. 组织阅读顺序

我们打开 notes/index.rst,声明目录:

代码清单 2 notes/index.rst
Field Notes
===========

These notes explain how to perform reliable physical measurements and document
them clearly.

.. toctree::
   :maxdepth: 2
   :numbered:

   measurement

Next steps:

* Begin with :doc:`measurement`.
* Review the standard unit conventions in :ref:`measurement-units`.

toctree 选项的含义如下:

  • :maxdepth: 2 设置目录中显示的子标题层级深度。
  • :numbered: 为章及其各节编号(例如“1. Measurement”和“1.1 Units of measurement”)。
  • measurement 引用文档名,不带 .rst 扩展名。

交叉引用的工作方式如下:

  • :doc:`measurement` 按文档路径链接到章节。
  • :ref:`measurement-units` 链接到带标签的节。PDF 中显示节标题。书籍模板还可以通过 page-ref 加上页码。可用的示例见 制作项目主题。

3. 添加 Graphviz 图示

图示能让关系一目了然。Guidedog 内置了 Graphviz 引擎,为网页绘制 SVG 矢量图,为打印版绘制 PDF 矢量图,无需外部渲染器。

我们在 notes/measurement.rst 中添加一张图示:

Comparison workflow
-------------------

Before comparing two experimental values, convert both to a shared base unit:

.. graphviz::
   :class: graphviz-adaptive
   :caption: Converting values to a common base unit before comparison.
   :alt: Raw experimental values are converted to a shared unit before comparison.

   digraph comparison {
     graph [rankdir=LR, bgcolor="transparent", pad="0.2"];
     node [shape=box, style="rounded,filled", fillcolor="#edf5f2",
           color="#216553", fontname="Helvetica", fontcolor="#16382f", fontsize=12];
     edge [color="#216553"];

     "Raw values" -> "Unit conversion" -> "Common unit" -> "Comparison";
   }

:caption: 选项提供图注。要为插图编号,我们在 notes/conf.toml 中加入 numfig = true。:alt: 选项为屏幕阅读器提供替代文本。

4. 构建并检查书籍

我们重新构建项目:

guidedog build html notes -W
guidedog build pdf notes -W

我们分别检查两种格式的结果:

  • HTML 网站:打开 notes/_build/html/index.html。侧栏现在显示带编号的章节。点击“1. Measurement”会打开新页面,其中有面包屑导航和 SVG 图示。
  • PDF 书籍:打开 notes/_build/pdf/field-notes-en.pdf。目录列出章节和页码,图示保持矢量形式,文档交叉引用链接到对应位置。

5. 下一步

现在我们已经了解一份源文件如何生成两种输出。主题部分会进一步展开: