编写文档¶
Guidedog 同时支持 reStructuredText(.rst)和 MyST Markdown(.md)。两者都解析为同一种文档树,HTML 网站和 PDF 书籍都由这棵树生成。
选择源文件语言¶
需要完整使用域、复杂表格和可扩展的指令时,我们使用 reStructuredText。更喜欢 Markdown 语法、同时又想使用 Sphinx 风格的指令和角色时,我们使用 MyST Markdown。
在文档项目中,Markdown 文档按 MyST 解析。使用 guidedog convert 转换单个文档时,Markdown 按纯 CommonMark 0.31.2 读取:MyST 的指令、角色和设置只属于项目。
标题与结构¶
文档标题标识页面,章节和小节划分正文。在 reStructuredText 中,标题使用下划线装饰(也可以加上相同的上划线)。下划线的长度不能短于标题文字。
建议的标题符号:
Document Title
==============
Section Heading
---------------
Subsection
~~~~~~~~~~
Sub-subsection
^^^^^^^^^^^^^^
Paragraph Heading
"""""""""""""""""
我们在整个项目中保持标题层级一致。在 MyST Markdown 中,使用标准的井号(#、##、###)。
文字格式与行内标记¶
行内标记表达的是含义,而不是装饰:
| 样式 | reStructuredText | MyST Markdown |
|---|---|---|
| 加粗 | **strong text** |
**strong text** |
| 强调/斜体 | *emphasized text* |
*emphasized text* |
行内代码 |
``inline code`` |
`inline code` |
| 下标 | :sub:`text` |
{sub}`text` |
| 上标 | :sup:`text` |
{sup}`text` |
要在正文中写出字面的星号或反引号,用反斜杠转义:\*not italic\*。
列表¶
Guidedog 支持四种列表:
无序列表¶
* First item
* Second item with multiple lines
of continuing explanation.
* Third item
有序列表¶
1. Numbered item
2. Second item
#. Automatically numbered item
#. Next auto-numbered item
定义列表¶
定义列表把术语和说明配成对。术语单独占一行,缩进的定义紧随其后:
Workspace
The reusable conversion context. It keeps its buffers between documents.
Project
A loaded configuration with its source folder, templates, and caches.
字段列表¶
字段列表用于结构化元数据和参数说明:
:Authors: Jane Doe, John Smith
:Version: 1.2
:Status: Active
当字段列表位于文档标题之前时,Guidedog 会将受识别的发布元数据字段读入页面的元数据记录,并从渲染后的正文中移除这些字段。这些字段包括 :date:、:updated:、:publish-at:、:expires-at:、:draft:、:authors:、:category:、:tags:、:series:、:summary:、:template: 和 :pdf:。MyST Markdown 文档可在开头的 YAML front matter 中提供相同的字段。位于标题之后的字段列表仍作为普通正文内容。完整的发布生命周期参见 静态站点与文章。
代码块¶
code-block 指令(或 sourcecode)显示带语法高亮的代码。Guidedog 能高亮 35 种编程语言,其中包括 Rust 和 Odin,使用 Pygments 的名称和样式,且无需 Python 运行时:
.. code-block:: python
:linenos:
:caption: Server entry point
:emphasize-lines: 2, 4-5
def main():
app = create_app()
app.run(host="0.0.0.0", port=8080)
代码块选项:
:linenos::在代码旁显示行号。:caption: Title:在代码块上方或下方添加说明。:emphasize-lines: 1, 3-5:突出显示指定行。:name: label:为代码块指定引用标签。
要从文件中引入源代码,我们使用 literalinclude:
.. literalinclude:: ../src/server.py
:language: python
:lines: 1-25
:linenos:
提示框¶
提示框用于突出显示备注、警告和补充建议:
.. note::
Helpful background context or implementation detail.
.. tip::
Suggested best practices or workflow shortcuts.
.. important::
Essential requirements that must not be overlooked.
.. warning::
Conditions that could lead to unexpected behavior or lost work.
.. caution::
Potential pitfalls or sensitive operational steps.
.. seealso::
References to related chapters, specifications, or external guides.
Guidedog 也支持 danger、error、hint 和 attention。自定义标题的通用提示框使用 admonition 指令:
.. admonition:: Design Rationale
Explains why a particular architecture was chosen.
表格¶
Guidedog 支持简单表格、网格表格和指令生成的表格。
简单表格¶
简单表格用横线划分列的范围:
===== ===== =======
A B A and B
===== ===== =======
False False False
True False False
True True True
===== ===== =======
网格表格¶
网格表格支持多行单元格和跨行、跨列合并:
+------------------------+------------+----------+
| Header row, column 1 | Column 2 | Column 3 |
+========================+============+==========+
| Cell with multiple | Second | Third |
| paragraphs of text. | column | column |
+------------------------+------------+----------+
列表表格¶
list-table 指令用嵌套的无序列表创建表格。即使是较宽的表格,在源码中也便于阅读和维护:
.. list-table:: Project configurations
:widths: 25 25 50
:header-rows: 1
* - Target
- Builder
- Description
* - Website
- ``html``
- Static HTML documentation site
* - Book
- ``pdf``
- Typeset PDF book powered by Typst
CSV 表格¶
csv-table 用逗号分隔的数据创建表格:
.. csv-table:: Comparative metrics
:header: "Name", "Time (ms)", "Memory (MB)"
:widths: 40, 30, 30
"Cold build", 42, 12
"Incremental", 3, 4
图片与插图¶
可以用 image 和 figure 插入插图、屏幕截图和现成图表,也可以在标记文本中直接编写 Graphviz、Typst 绘图源码与 LilyPond 乐谱,自动生成适用于 HTML 和 PDF 的图像资源;参见 使用 Graphviz 和 Typst 绘制图表 和 乐谱记法。
对于已有图像:
.. image:: /assets/architecture.png
:width: 600px
:align: center
:alt: System architecture diagram
.. figure:: /assets/flow.png
:scale: 80%
:align: center
:alt: Execution flowchart
Data flows sequentially through reader, resolver, and renderer.
figure 为图片加上图注,还可以指定引用目标。
用目录树组织文档¶
toctree 定义文档层级、导航菜单,以及 HTML 和 PDF 的阅读顺序:
.. toctree::
:maxdepth: 2
:caption: User Guide
:numbered:
installation
quickstart
configuration
toctree 的常用选项:
:maxdepth: N:目录中包含的标题层级深度。:caption: Title:导航条目上方显示的分类标题。:numbered::为章节和标题添加编号。:titlesonly::只列出文档标题,不显示内部章节。:hidden::记录文档阅读顺序,但不在正文中显示列表。:glob::允许用通配符匹配文档,例如tutorials/*。
用 postlist 生成带日期的文章列表¶
对于带有 :date: 元数据的文章、版本说明和研究日志,postlist 指令会按时间倒序渲染匹配的已发布页面列表:
.. postlist:: 5
:path: posts/*
:category: systems
:tags: rust, incremental
:excerpts:
postlist 的选项:
- 参数
N:最多显示的匹配文章数;为 0 或省略参数时包含全部匹配项。 :path: GLOB:将条目限制为匹配该 glob 模式的文档名。:category: NAME、:tags: TAG, ...、:author: NAME、:series: NAME、:year: YYYY:按发布元数据筛选条目。:sort: date或:sort: -date:按发布时间从早到晚或从新到旧排序,时间相同时按文档名排序。:style: list或:style: cards:选择列表的 CSS 类。:excerpts::渲染每个条目在.. more::之前的富文本引言;没有分界标记时,使用其纯文本:summary:。
关于集合、订阅源和定时发布,参见 静态站点与文章。
交叉引用与链接¶
显式目标让链接保持稳定,即使文档文件改名也不受影响。
目标标签与 :ref:¶
我们把目标标签直接放在标题、表格或插图之前:
.. _storage-model:
Storage model
-------------
Every conversion runs within explicit limits.
项目中的任何文档都可以引用这个目标:
See :ref:`storage-model` for details.
See :ref:`custom link text <storage-model>`.
用 :doc: 引用文档¶
我们按路径链接到其他文档,省略文件扩展名:
Read the :doc:`configuration guide <../reference/configuration>` for details.
用 :term: 引用术语¶
我们用 glossary 指令定义术语:
.. glossary::
Workspace
A reusable context whose buffers keep their capacity between conversions.
Pass
An in-memory transformation step operating on the document AST.
我们用 :term: 角色链接到术语表中的术语:
Conversion runs within a reusable :term:`workspace`.
用 :download: 提供文件下载¶
下面的写法会把文件复制到输出的下载目录,并生成指向它的链接:
Download the :download:`starter template <files/starter.toml>`.
外部链接与跨挂载链接¶
链接到外部网址的写法如下:
Visit `Typst <https://typst.app>`_ for typography details.
在通过 [[site.mounts]] 组装的多项目网站中,我们还可以使用 mount://<name>/<path> 按挂载名链接到已挂载的子项目:
Read the `User Manual <mount://manual/index.html>`_ or `GDS 0013 <mount://discussions/records/0013-static-site-generation.html>`_.
挂载名必须与 [[site.mounts]] 一致,且路径必须指向当前的输出文件。
替换与包含¶
我们可以定义可复用的短语、符号或图片:
.. |version| replace:: 1.0.0
.. |brand| replace:: **Field Notes**
Welcome to |brand| version |version|.
要在多个文件中复用 reStructuredText 内容:
.. include:: ../shared/warnings.rst
编写 MyST Markdown¶
MyST Markdown 通过围栏块支持相同的指令和角色:
# Project overview
Here is a paragraph with **bold text**, *italic emphasis*, and `inline code`.
```{note}
This note is rendered identically to a reStructuredText note directive.
```
```{code-block} python
:caption: Example function
:linenos:
def add(a, b):
return a + b
```
```{toctree}
:maxdepth: 2
:caption: Navigation
first-chapter
second-chapter
```
See {ref}`storage-model` or consult the {doc}`../reference/configuration`.