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

相关文章

Android实现在线预览office文档的示例详解

《Android实现在线预览office文档的示例详解》在移动端展示在线Office文档(如Word、Excel、PPT)是一项常见需求,这篇文章为大家重点介绍了两种方案的实现方法,希望对大家有一定的... 目录一、项目概述二、相关技术知识三、实现思路3.1 方案一:WebView + Office Onl

JS+HTML实现在线图片水印添加工具

《JS+HTML实现在线图片水印添加工具》在社交媒体和内容创作日益频繁的今天,如何保护原创内容、展示品牌身份成了一个不得不面对的问题,本文将实现一个完全基于HTML+CSS构建的现代化图片水印在线工具... 目录概述功能亮点使用方法技术解析延伸思考运行效果项目源码下载总结概述在社交媒体和内容创作日益频繁的

利用Python快速搭建Markdown笔记发布系统

《利用Python快速搭建Markdown笔记发布系统》这篇文章主要为大家详细介绍了使用Python生态的成熟工具,在30分钟内搭建一个支持Markdown渲染、分类标签、全文搜索的私有化知识发布系统... 目录引言:为什么要自建知识博客一、技术选型:极简主义开发栈二、系统架构设计三、核心代码实现(分步解析

使用Python实现快速搭建本地HTTP服务器

《使用Python实现快速搭建本地HTTP服务器》:本文主要介绍如何使用Python快速搭建本地HTTP服务器,轻松实现一键HTTP文件共享,同时结合二维码技术,让访问更简单,感兴趣的小伙伴可以了... 目录1. 概述2. 快速搭建 HTTP 文件共享服务2.1 核心思路2.2 代码实现2.3 代码解读3.

MySQL双主搭建+keepalived高可用的实现

《MySQL双主搭建+keepalived高可用的实现》本文主要介绍了MySQL双主搭建+keepalived高可用的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,... 目录一、测试环境准备二、主从搭建1.创建复制用户2.创建复制关系3.开启复制,确认复制是否成功4.同

MySQL使用binlog2sql工具实现在线恢复数据功能

《MySQL使用binlog2sql工具实现在线恢复数据功能》binlog2sql是大众点评开源的一款用于解析MySQLbinlog的工具,根据不同选项,可以得到原始SQL、回滚SQL等,下面我们就来... 目录背景目标步骤准备工作恢复数据结果验证结论背景生产数据库执行 SQL 脚本,一般会经过正规的审批

使用DeepSeek搭建个人知识库(在笔记本电脑上)

《使用DeepSeek搭建个人知识库(在笔记本电脑上)》本文介绍了如何在笔记本电脑上使用DeepSeek和开源工具搭建个人知识库,通过安装DeepSeek和RAGFlow,并使用CherryStudi... 目录部署环境软件清单安装DeepSeek安装Cherry Studio安装RAGFlow设置知识库总

Linux搭建Mysql主从同步的教程

《Linux搭建Mysql主从同步的教程》:本文主要介绍Linux搭建Mysql主从同步的教程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录linux搭建mysql主从同步1.启动mysql服务2.修改Mysql主库配置文件/etc/my.cnf3.重启主库my

国内环境搭建私有知识问答库踩坑记录(ollama+deepseek+ragflow)

《国内环境搭建私有知识问答库踩坑记录(ollama+deepseek+ragflow)》本文给大家利用deepseek模型搭建私有知识问答库的详细步骤和遇到的问题及解决办法,感兴趣的朋友一起看看吧... 目录1. 第1步大家在安装完ollama后,需要到系统环境变量中添加两个变量2. 第3步 “在cmd中

本地搭建DeepSeek-R1、WebUI的完整过程及访问

《本地搭建DeepSeek-R1、WebUI的完整过程及访问》:本文主要介绍本地搭建DeepSeek-R1、WebUI的完整过程及访问的相关资料,DeepSeek-R1是一个开源的人工智能平台,主... 目录背景       搭建准备基础概念搭建过程访问对话测试总结背景       最近几年,人工智能技术