0
点赞
收藏
分享

微信扫一扫

mac 同步安卓手机屏幕

一枚路过的程序猿 03-23 08:00 阅读 4
sphinx

总的来说, 任务分为两个部分:

  • 使用 Sphinx 生成文档
  • 文档托管到 Read the docs 平台

准备

整个过程基于 python 实现, 推荐使用 Anoconda 能够创建管理独立的虚拟环境【 conda使用方法】,这样之后在配置文件的时候会方便很多。
在这里插入图片描述


使用 Sphinx 生成文档

掌握几个基本语句:

  1. 安装需要的库: pip install sphinx
  2. 快速创建项目:sphinx-quickstart
  3. 独立的源文件和构建目录(y/n) [n]: 个人填 y
  4. 接下来所填项目示例:
  5. 项目语种 [en]: zh_CN, 英语则填写 EN

在这之后项目基本创建完成,项目框架如图所示:
在这里插入图片描述
需要关注的主要是 **source** **build** 两个文件夹。

  • source: 所有的需要的文档都要放在里面 (.rst| .md)
  • build:所有生成的文件都会保存在里面

⭐ 需要配置**conf.py** **index.rst** 两个文件

---* conf.py *---
# Configuration file for the Sphinx documentation builder.
#
# For the full list of built-in configuration values, see the documentation:
# https://www.sphinx-doc.org/en/master/usage/configuration.html

# -- Project information -----------------------------------------------------
# https://www.sphinx-doc.org/en/master/usage/configuration.html#project-information
import sys
import os
# 此处在 sphinx-quickstart 中都进行了填写,一般无需修改
project = 'XXXX'
copyright = '2024, 摸鱼仙人'
author = 'Jin'
release = 'v1'

# -- General configuration ---------------------------------------------------
# https://www.sphinx-doc.org/en/master/usage/configuration.html#general-configuration
# 此处为项目文件地址
project_path = "F:\XXX"
sys.path.insert(0, os.path.abspath("../.."))

# 此处为导入的 md 文件需要的配置
extensions = ['sphinx_markdown_tables', 'm2r']
source_suffix = [".rst", ".md"]


templates_path = ['_templates']
exclude_patterns = []
# 此处为项目使用语言
language = 'zh_CN'

# -- Options for HTML output -------------------------------------------------
# https://www.sphinx-doc.org/en/master/usage/configuration.html#options-for-html-output

html_theme = 'sphinx_rtd_theme'
html_static_path = ['_static']
---* index.rst *---

.. XXXXXX documentation master file, created by
   sphinx-quickstart on Fri Mar 22 07:52:42 2024.
   You can adapt this file completely to your liking, but it should at least
   contain the root `toctree` directive.

XXXXX
=========================================

.. toctree::
   :maxdepth: 3
   :caption: XXXX

    ch01_数学基础 <ch01_数学基础/ch01_数学基础.md>


Indices and tables
==================

* :ref:`genindex`
* :ref:`modindex`
* :ref:`search`

需要熟练掌握语法, 由于内容过多, 本处不再赘述.有机会再开一篇详细展开. 配置内容大致相似,修改部分需要的名称即可.

注意:
.. toctree::
   :maxdepth: 3
   :caption: XXXX
<!-- 此处必须空一行 -->
    ch01_数学基础 <ch01_数学基础/ch01_数学基础.md>

该处的格式

掌握两句代码,反复使用:

  • make clean: 清除build生成的文件
  • make html: 生成html文件

build 文件夹 中, 可以找到 index.html, 可以通过该文件查看生成的结果

在这里插入图片描述


没有问题之后, 在 Github 上创建仓库, 将目前文件夹中所有的内容推送上去.

  • 配置 .gitignore 文件
---* .gitignore *---
 build/

托管到 Read the docs 平台

  • Read the docs 平台: Read the docs

登录平台,使用 Github 账号进行登录

在这里插入图片描述
点击 导入一个项目 就可以将 Github 中的项目的文档托管过来, 以后文档的更新只需要在Github 平台上传,此处的托管能够自动的跟进更新

注意:
导入前需要创建 .readthedocs.yaml 文件进行配置, 更新上传在 Github 中

# .readthedocs.yaml
# Read the Docs configuration file
# See https://docs.readthedocs.io/en/stable/config-file/v2.html for details

# Required
version: 2

# Set the OS, Python version and other tools you might need
build:
  os: ubuntu-22.04
  tools:
    python: "3.9"
    # You can also specify other tool versions:
    # nodejs: "19"
    # rust: "1.64"
    # golang: "1.19"

# Build documentation in the "docs/" directory with Sphinx
sphinx:
  configuration: source/conf.py

# Optionally build your docs in additional formats such as PDF and ePub
# formats:
#    - pdf
#    - epub

# Optional but recommended, declare the Python requirements required
# to build your documentation
# See https://docs.readthedocs.io/en/stable/guides/reproducible-builds.html
python:
   install:
   - requirements: docs/requirements.txt

点击导入后有几处地方容易导致报错, 需要注意:

  • 各个模块的内容需要顶格写,否则会出现报错

requirements.txt 文件生成:

在虚拟环境中使用 pip freeze > requirements.txt 能够自动生成配置的库的相关参数, 有时候 txt 文本中会出现一些奇怪的内容需要删除:
在这里插入图片描述
在这里插入图片描述
因此,开始时创建一个干净的虚拟环境十分有利于任务的快速推进.

导入构建后,如果显示如图则生成成功. 如果为红色, 显示 failed 则生成失败.
在这里插入图片描述
单击 build 则会在 构建 部分重新运行
在这里插入图片描述
在这里插入图片描述

通过此处的网址可以直接查看文档的地址:

在这里插入图片描述


参考资料:

  1. Sphinx + Read the Docs 从懵逼到入门
  2. 使用Read the Docs托管文档
  3. Sphinx 学习优质教程
举报

相关推荐

0 条评论