搭建在线电子书: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

相关文章

水位雨量在线监测系统概述及应用介绍

在当今社会,随着科技的飞速发展,各种智能监测系统已成为保障公共安全、促进资源管理和环境保护的重要工具。其中,水位雨量在线监测系统作为自然灾害预警、水资源管理及水利工程运行的关键技术,其重要性不言而喻。 一、水位雨量在线监测系统的基本原理 水位雨量在线监测系统主要由数据采集单元、数据传输网络、数据处理中心及用户终端四大部分构成,形成了一个完整的闭环系统。 数据采集单元:这是系统的“眼睛”,

电力系统中的A类在线监测装置—APView400

随着电力系统的日益复杂和人们对电能质量要求的提高,电能质量在线监测装置在电力系统中得到广泛应用。目前,市场上的在线监测装置主要分为A类和B类两种类型,A类和B类在线监测装置主要区别在于应用场景、技术参数、通讯协议和扩展性。选择时应根据实际需求和应用场景综合考虑,并定期维护和校准。电能质量在线监测装置是用于实时监测电力系统中的电能质量参数的设备。 APView400电能质量A类在线监测装置以其多核

搭建Kafka+zookeeper集群调度

前言 硬件环境 172.18.0.5        kafkazk1        Kafka+zookeeper                Kafka Broker集群 172.18.0.6        kafkazk2        Kafka+zookeeper                Kafka Broker集群 172.18.0.7        kafkazk3

【IPV6从入门到起飞】5-1 IPV6+Home Assistant(搭建基本环境)

【IPV6从入门到起飞】5-1 IPV6+Home Assistant #搭建基本环境 1 背景2 docker下载 hass3 创建容器4 浏览器访问 hass5 手机APP远程访问hass6 更多玩法 1 背景 既然电脑可以IPV6入站,手机流量可以访问IPV6网络的服务,为什么不在电脑搭建Home Assistant(hass),来控制你的设备呢?@智能家居 @万物互联

pico2 开发环境搭建-基于ubuntu

pico2 开发环境搭建-基于ubuntu 安装编译工具链下载sdk 和example编译example 安装编译工具链 sudo apt install cmake gcc-arm-none-eabi libnewlib-arm-none-eabi libstdc++-arm-none-eabi-newlib 注意cmake的版本,需要在3.17 以上 下载sdk 和ex

JavaFX应用更新检测功能(在线自动更新方案)

JavaFX开发的桌面应用属于C端,一般来说需要版本检测和自动更新功能,这里记录一下一种版本检测和自动更新的方法。 1. 整体方案 JavaFX.应用版本检测、自动更新主要涉及一下步骤: 读取本地应用版本拉取远程版本并比较两个版本如果需要升级,那么拉取更新历史弹出升级控制窗口用户选择升级时,拉取升级包解压,重启应用用户选择忽略时,本地版本标志为忽略版本用户选择取消时,隐藏升级控制窗口 2.

Go Playground 在线编程环境

For all examples in this and the next chapter, we will use Go Playground. Go Playground represents a web service that can run programs written in Go. It can be opened in a web browser using the follow

如何提高 GitHub 的下载速度

如何提高 GitHub 的下载速度 文章目录 如何提高 GitHub 的下载速度1. 注册账号2. 准备好链接3. 创建仓库4. 在码云上下载代码5. 仓库更新了怎么办 一般来说,国内的朋友从 GitHub 上面下载代码,速度最大是 20KB/s,这种龟速,谁能忍受呢? 本文介绍一种方法——利用“码云”,可以大大提高下载速度,亲测有效。 1. 注册账号 去“码云”注册一

Github连接方式

打开Linux中git的配置文件: /home/username/git/MyRepository/.git/config [core]repositoryformatversion = 0filemode = truebare = falselogallrefupdates = true[remote "origin"]fetch = +refs/heads/*:refs/remot

GitHub每周最火火火项目(9.2-9.8)

项目名称:polarsource / polar 项目介绍:polar 是一个开源项目,它是 Lemon Squeezy 的替代方案,并且具有更具优势的价格。该项目的目标是为开发者提供一种更好的选择,让他们能够在追求自己的热情和兴趣的同时,通过编码获得相应的报酬。通过使用 polar,开发者可以享受到更实惠的价格,同时也能够更自由地发挥自己的创造力和技能。 项目地址:https://github.