搭建在线电子书:Sphinx + Github + ReadTheDocs

2024-01-29 19:50

本文主要是介绍搭建在线电子书:Sphinx + Github + ReadTheDocs,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

我写博客的初衷是为了系统的构建自己的知识体系,目前使用的平台有微信公众号,CSDN,博客园,GitHub Pages和Gitee Pages,他们都各有优缺点,整理的笔记多了之后发现这些平台不是很方便,比如公众号,CSDN和博客园,每次写完文章后,还需要再平台上进行编辑再发布,比较麻烦;GitHub Pages和Gitee Pages虽然可以快速发布,但是在文章系统管理上不是很方便。我希望将笔记整理成类似电子书一样,方便搜索和管理,经过查询资料,发现了ReadTheDocs这个文档管理工具,比较符合我的需求。可以使用 Sphinx 生成文档,GitHub 托管文档,然后导入到 ReadtheDocs进行展示,本文记录一下搭建过程。

目录

  • 准备条件
  • Sphinx创建文档
    • 1. 安装Sphinx
    • 2. 创建文档
    • 3. 编译
    • 4. 配置主题
    • 5. 配置markdown
  • 关联Read the Docs

准备条件

1、github账号
使用github对文档进行版本管理

2、注册Read the Docs账号
官网地址:https://readthedocs.org/

3、安装Python
Sphinx是一个python工具,用于生成文档,所以需要安装Python环境。

Sphinx创建文档

Sphinx是一个基于Python的文档生成项目,开始是用来生成 Python 官方文档的工具,更多介绍可参考官网:https://www.sphinx.org.cn/ 。

1. 安装Sphinx

Sphinx的GitHub地址:https://github.com/sphinx-doc/sphinx

pip安装Sphinx

$ pip install -U sphinx

2. 创建文档

先将远程github仓库clone到本地,这个仓库是你要托管文档的仓库,如果没有就新建一个。

clone到本地后,在项目根目录创建一个docs目录,cd进入docs目录,执行如下命令:

$ sphinx-quickstart
Welcome to the Sphinx 4.2.0 quickstart utility.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.
> Separate source and build directories (y/n) [n]: yThe project name will occur in several places in the built documentation.
> Project name: 测试开发小记
> Author name(s): hiyongz
> Project release []: 0.1.0If the documents are to be written in a language other than English,
you can select a language here by its language code. Sphinx will then
translate text that it generates into that language.For a list of supported codes, see
https://www.sphinx-doc.org/en/master/usage/configuration.html#confval-language.
> Project language [en]: zh_CNCreating file D:\pythonproj\devtest\source\conf.py.
Creating file D:\pythonproj\devtest\source\index.rst.
Creating file D:\pythonproj\devtest\Makefile.
Creating file D:\pythonproj\devtest\make.bat.Finished: An initial directory structure has been created.You should now populate your master file D:\pythonproj\devtest\source\index.rst and create other documentation
source files. Use the Makefile to build the docs, like so:make builder
where "builder" is one of the supported builders, e.g. html, latex or linkcheck.

上面的配置可以选择默认,稍后修改生成的conf.py配置文件即可。

设置完成后,目录结构如下:

│   make.bat
│   Makefile
│
├───build
└───source│   conf.py│   index.rst│├───_static└───_templates
  • build 存放编译后的文件
  • source/_static 存放静态文件
  • source/_templates 存放模板文件
  • source/conf.py 项目配置文件,上面的配置可以在这里面修改
  • source/index.rst 首页

3. 编译

对rst文件进行编译生成HTML及相关静态文件:

$ make html
Running Sphinx v4.2.0
loading translations [zh_CN]... done
making output directory... done
building [mo]: targets for 0 po files that are out of date
building [html]: targets for 1 source files that are out of date
updating environment: [new config] 1 added, 0 changed, 0 removed
reading sources... [100%] index
looking for now-outdated files... none found
pickling environment... done
checking consistency... done
preparing documents... done
writing output... [100%] index
generating indices... genindex done
writing additional pages... search done
copying static files... done
copying extra files... done
dumping search index in Chinese (code: zh)... done
dumping object inventory... done
build succeeded.The HTML pages are in build\html.

index.rst文件内容会编译到_build/html目录下。

打开_build\html\index.html文件,下面是渲染出来的HTML页面:

默认主题不好看,可以配置其它主题。

4. 配置主题

安装sphinx Read the Docs主题

pip install sphinx_rtd_theme

更多主题可到官网 https://sphinx-themes.org/ 查看。

配置source/conf.py 文件:

import sphinx_rtd_theme
html_theme = "sphinx_rtd_theme"
html_theme_path = [sphinx_rtd_theme.get_html_theme_path()]

重新编译:

$ make html

打开_build\html\index.html文件,可以发现主题配置成功。

5. 配置markdown

Sphinx默认使用 reStructuredText 标记语言,由于已经习惯使用markdown进行文档编辑,下面来配置markdown。

1) 安装recommonmark插件

pip install recommonmark

2)安装支持markdown表格的插件

pip install sphinx_markdown_tables

ReadTheDocs的python环境貌似没有sphinx_markdown_tables,在构建时可能报如下错误:

ModuleNotFoundError: No module named 'sphinx_markdown_tables'

解决方案是在docs目录下新建一个requirements.txt文件,写入如下内容:

sphinx-markdown-tables==0.0.15

3)配置source/conf.py 文件

增加:

extensions = ['recommonmark','sphinx_markdown_tables'] 

在source目录下创建一个markdown文件,将makdown文件名添加到source/index.rst 中

.. toctree:::maxdepth: 2:caption: Contents:windows-shortcuts.md

重新编译

4)提交上传

.gitignore文件添加docs/build/目录,不需要上传这个目录。上传:

git add .
git commit -m "提交说明"
git push -u origin master

关联Read the Docs

关联Read the Docs,使其可以在线访问文档。

浏览器访问 https://readthedocs.org/, 点击【我的项目】-> 【Import a Project】:

选择仓库

点击下一步

构建版本

构建完成后,点击阅读文档

构建成功

在线文档地址为https://devtest-notes.readthedocs.io/。

参考资料:

  1. https://www.sphinx.org.cn/
  2. https://readthedocs.org/
  3. https://github.com/readthedocs/readthedocs.org
  4. https://docs.readthedocs.io/en/stable/tutorial/
  5. https://www.osgeo.cn/sphinx/usage/markdown.html
  6. https://www.sphinx-doc.org/zh_CN/master/usage/configuration.html
  7. https://iridescent.ink/HowToMakeDocs/Basic/reST.html
--THE END--

这篇关于搭建在线电子书:Sphinx + Github + ReadTheDocs的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/658003

相关文章

一文详解如何在idea中快速搭建一个Spring Boot项目

《一文详解如何在idea中快速搭建一个SpringBoot项目》IntelliJIDEA作为Java开发者的‌首选IDE‌,深度集成SpringBoot支持,可一键生成项目骨架、智能配置依赖,这篇文... 目录前言1、创建项目名称2、勾选需要的依赖3、在setting中检查maven4、编写数据源5、开启热

基于Python实现一个简单的题库与在线考试系统

《基于Python实现一个简单的题库与在线考试系统》在当今信息化教育时代,在线学习与考试系统已成为教育技术领域的重要组成部分,本文就来介绍一下如何使用Python和PyQt5框架开发一个名为白泽题库系... 目录概述功能特点界面展示系统架构设计类结构图Excel题库填写格式模板题库题目填写格式表核心数据结构

github打不开的问题分析及解决

《github打不开的问题分析及解决》:本文主要介绍github打不开的问题分析及解决,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、找到github.com域名解析的ip地址二、找到github.global.ssl.fastly.net网址解析的ip地址三

如何搭建并配置HTTPD文件服务及访问权限控制

《如何搭建并配置HTTPD文件服务及访问权限控制》:本文主要介绍如何搭建并配置HTTPD文件服务及访问权限控制的问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、安装HTTPD服务二、HTTPD服务目录结构三、配置修改四、服务启动五、基于用户访问权限控制六、

pytest+allure环境搭建+自动化实践过程

《pytest+allure环境搭建+自动化实践过程》:本文主要介绍pytest+allure环境搭建+自动化实践过程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐... 目录一、pytest下载安装1.1、安装pytest1.2、检测是否安装成功二、allure下载安装2.

使用vscode搭建pywebview集成vue项目实践

《使用vscode搭建pywebview集成vue项目实践》:本文主要介绍使用vscode搭建pywebview集成vue项目实践,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地... 目录环境准备项目源码下载项目说明调试与生成可执行文件核心代码说明总结本节我们使用pythonpywebv

Windows Server 2025 搭建NPS-Radius服务器的步骤

《WindowsServer2025搭建NPS-Radius服务器的步骤》本文主要介绍了通过微软的NPS角色实现一个Radius服务器,身份验证和证书使用微软ADCS、ADDS,具有一定的参考价... 目录简介示意图什么是 802.1X?核心作用802.1X的组成角色工作流程简述802.1X常见应用802.

Spring Cloud GateWay搭建全过程

《SpringCloudGateWay搭建全过程》:本文主要介绍SpringCloudGateWay搭建全过程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐... 目录Spring Cloud GateWay搭建1.搭建注册中心1.1添加依赖1.2 配置文件及启动类1.3 测

SpringBoot快速搭建TCP服务端和客户端全过程

《SpringBoot快速搭建TCP服务端和客户端全过程》:本文主要介绍SpringBoot快速搭建TCP服务端和客户端全过程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,... 目录TCPServerTCPClient总结由于工作需要,研究了SpringBoot搭建TCP通信的过程

Gradle下如何搭建SpringCloud分布式环境

《Gradle下如何搭建SpringCloud分布式环境》:本文主要介绍Gradle下如何搭建SpringCloud分布式环境问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地... 目录Gradle下搭建SpringCloud分布式环境1.idea配置好gradle2.创建一个空的gr