各类官网

Windows本地编译和调试环境搭建

初始化

推荐用conda新建环境,并安装以下包

 pip3 install Sphinx sphinx-autobuild sphinx_rtd_theme recommonmark sphinx_markdown_tables

其中recommonmarksphinx_markdown_tables用于使sphinx支持markdown,作者推荐含有表格的文档还是用rst格式写

执行 sphinx-quickstart 构建项目框架,将会出现如下对话窗口。

 欢迎使用 Sphinx 3.2.1 快速配置工具。
 
 Please enter values for the following settings (just press Enter to
 accept a default value, if one is given in brackets).
 
 Selected root path: .
 
 You have two options for placing the build directory for Sphinx output.
 Either, you use a directory "_build" within the root path, or you separate
 "source" and "build" directories within the root path.
 > 独立的源文件和构建目录(y/n) [n]:

首先,询问你是否要创建独立的源文件和构建目录。实际上对应两种目录结构,一种是在根路径下创建“_build”目录,另一种是在根路径下创建“source”和“build”两个独立的目录,前者用于存放文档资源,后者用于保存构建生成的各种文件。强烈推荐独立目录,因此输入 y

接着,需要输入项目名称、作者等信息。

 The project name will occur in several places in the built documentation.
 > 项目名称: test1
 > 作者名称: author1
 > 项目发行版本 []: 

独立目录的项目结构:

  • Makefile:可以看作是一个包含指令的文件,在使用 make 命令时,可以使用这些指令来构建文档输出。
  • build:生成的文件的输出目录。
  • make.bat:Windows 用命令行。
  • _static:静态文件目录,比如图片等。
  • _templates:模板目录。
  • conf.py:存放 Sphinx 的配置,包括在 sphinx-quickstart 时选中的那些值,可以自行定义其他的值。
  • index.rst:文档项目起始文件。

手动编译

自动方法:在项目目录中执行 make html,就会在build/html 目录生成 html 相关文件。其中编译所需的source目录和build目录的具体路径在make.batMakefile中,修改和使用都不是很方便

手动方法:使用sphinx-build source build命令,其中sourcebuild都可以手动指定,具有更强的灵活性
在浏览器中打开 index.html可以预览

当然,直接访问 html 文件不是很方便,所以我们借助 sphinx-autobuild 工具启动 HTTP 服务。

 sphinx-autobuild source build/html

默认启动 8000 端口,在浏览器输入 http://127.0.0.1:8000

修改设置

打开 conf.py 文件,加入:

html_theme = 'sphinx_rtd_theme'
extensions = ['recommonmark','sphinx_markdown_tables']
source_suffix = {'.rst': 'restructuredtext','.md': 'markdown'}

Sphinx 提供的主题在 Sphinx Themes 都可以找到。

修改样式

默认样式的内容框占不满整个屏幕的横向距离,若想增加该宽度,则:

_templates里放layout.html

{% extends "!layout.html" %}
{% block extrahead %}
    <link href="{{ pathto("_static/style.css", True) }}" rel="stylesheet" type="text/css">
{% endblock %}

_static里放style.css,这里的具体值可以改:

.wy-nav-content {
    max-width: 100%;
}

内容编辑

细节可参考rst语言官网rst语言中文文档
后续修改内容时需要先删除build目录然后重新编译,不然旧页面的左侧目录会有bug

作为静态站点部署至linux服务器

使用docker部署nginx镜像,然后在容器/etc/nginx/conf.d/default.conf内设置root并挂载,将站点内容上传至该文件夹,最后执行docker-compose up -d