WangScaler: 一个用心创作的作者。声明:才疏学浅,如有错误,恳请指正。
很多的技术文档都是使用的ReadtheDocs,比如Python文档、tornado文档等等。虽然不是很美观,但是对于技术博文来说够用了。
一、安装Python
使用ReadtheDocs需要有python3的环境,因为sphinx是基于 python3 的。那么我们首先安装python环境,如果你有python环境,可以直接跳过这一步,直接浏览安装Sphinx。
1、下载python
选择python官网下载。可能比较慢,慢慢等待一会。选择Download下载安装包。
根据你的环境下载,我这里下载的Windows的64位最新版本的安装包。
2、安装
Add python 3.8 to PATH
自动给环境变量添加python的路径。相当于给你的电脑自动配置了python环境,在全局任意位置均可直接使用python。
表示安装成功,那么我们的电脑到底是否具备了python环境,接下来进行验证。
3、验证
Win+R 打开cmd输入python。
我们看到了python的版本信息,恭喜你大功告成了,接下来正式进入我们的话题。
二、安装Sphinx
Sphinx 是一个功能强大的文档生成器,具有许多用于编写技术文档的强大功能,Sphinx 最早只是用来生成 Python 官方文档,随着工具的完善, 越来越多的知名的项目也用他来生成文档。
1、安装Sphinx
pip install sphinx sphinx-autobuild sphinx_rtd_theme
2.初始化
window下:
d:
cd /wangscaler/
sphinx-quickstar
linux下
# 创建文档根目录
mkdir -p /usr/local/wangscaler/
cd /usr/local/wangscaler/
# 可以回车按默认配置来写
sphinx-quickstart
下面是我填写的,其他基本上默认即可:
> Separate source and build directories (y/n) [n]:y
> Project name: WangScaler
> Author name(s): WangScaler
> Project version []: 0.2
> Project release [1.0]: 0.2.2
> Project language [en]: zh_CN
3.安装软件tree查看目录树结构:
windows
tree
linux
yum install tree
tree -C .
三、支持markdown编写以及切换主题
recommonmark是支持markdown的插件,而sphinx-markdown-tables则是主题,因为默认的主题是不好看的,我们可以使用这个主题来优化我们的界面。
1.安装插件
pip install recommonmark
pip install sphinx-markdown-tables
2.修改配置
然后更改conf.py,我的配置如下:
# Configuration file for the Sphinx documentation builder.
#
# This file only contains a selection of the most common options. For a full
# list see the documentation:
# https://www.sphinx-doc.org/en/master/usage/configuration.html
# -- Path setup --------------------------------------------------------------
# If extensions (or modules to document with autodoc) are in another directory,
# add these directories to sys.path here. If the directory is relative to the
# documentation root, use os.path.abspath to make it absolute, like shown here.
#
import os
import sys
import shlex
sys.path.insert(0, os.path.abspath('.'))
import recommonmark
from recommonmark.transform import AutoStructify
# -- Project information -----------------------------------------------------
source_suffix = ['.rst', '.md']
master_doc = 'index'
project = 'Interface'
copyright = '2021, WangScaler'
author = 'WangScaler'
version = recommonmark.__version__
# The full version, including alpha/beta/rc tags
release = recommonmark.__version__
# -- General configuration ---------------------------------------------------
# Add any Sphinx extension module names here, as strings. They can be
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom
# ones.
extensions = [
'sphinx.ext.autodoc',
'sphinx.ext.napoleon',
'sphinx.ext.mathjax',
'recommonmark',
'sphinx_markdown_tables',
]
# Add any paths that contain templates here, relative to this directory.
templates_path = ['_templates']
# The language for content autogenerated by Sphinx. Refer to documentation
# for a list of supported languages.
#
# This is also used if you do content translation via gettext catalogs.
# Usually you set "language" from the command line for these cases.
language = 'zh_CN'
# List of patterns, relative to source directory, that match files and
# directories to ignore when looking for source files.
# This pattern also affects html_static_path and html_extra_path.
exclude_patterns = ['_build']
default_role = None
pygments_style = 'sphinx'
todo_include_todos = False
# -- Options for HTML output -------------------------------------------------
# The theme to use for HTML and HTML Help pages. See the documentation for
# a list of builtin themes.
#
html_theme = 'sphinx_rtd_theme'
# Add any paths that contain custom static files (such as style sheets) here,
# relative to this directory. They are copied after the builtin static files,
# so a file named "default.css" will overwrite the builtin "default.css".
html_static_path = ['_static']
htmlhelp_basename = 'Recommonmarkdoc'
latex_elements = {
# The paper size ('letterpaper' or 'a4paper').
#'papersize': 'letterpaper',
# The font size ('10pt', '11pt' or '12pt').
#'pointsize': '10pt',
# Additional stuff for the LaTeX preamble.
#'preamble': '',
# Latex figure (float) alignment
#'figure_align': 'htbp',
}
# Grouping the document tree into LaTeX files. List of tuples
# (source start file, target name, title,
# author, documentclass [howto, manual, or own class]).
latex_documents = [
(master_doc, 'Recommonmark.tex', u'Recommonmark Documentation',
u'Lu Zero, Eric Holscher, and contributors', 'manual'),
]
man_pages = [
(master_doc, 'recommonmark', u'Recommonmark Documentation',
[author], 1)
]
texinfo_documents = [
(master_doc, 'Recommonmark', u'Recommonmark Documentation',
author, 'Recommonmark', 'One line description of project.',
'Miscellaneous'),
]
# At the bottom of conf.py
# def setup(app):
# app.add_config_value('recommonmark_config', {
# #'url_resolver': lambda url: github_doc_root + url,
# 'auto_toc_tree_section': 'Contents',
# 'enable_math': False,
# 'enable_inline_math': False,
# 'enable_eval_rst': True,
# 'enable_auto_doc_ref': True,
# }, True)
# app.add_transform(AutoStructify)
3、编写文件
框架已经搭建的差不多了,就来写我们的第一篇文章吧。
在source目录下新建hello.rst,内容如下:
# My New Learning
## 一、Mongo(Montor异步数据库)
4、修改index.rst文件
这个文件是我们博客的目录,当我们有了新的文章可以加入到这个目录中。
.. Test documentation master file, created by
sphinx-quickstart on Mon Jul 20 09:49:03 2020.
You can adapt this file completely to your liking, but it should at least
contain the root `toctree` directive.
Welcome to Interface Document!
================================
.. toctree::
:maxdepth: 2
:caption: 名字:
hello
Indices and tables
==================
* :ref:`genindex`
* :ref:`modindex`
* :ref:`search`
5、编译文件
在根目录执行make html
,将我们的文件编译成网页。我们的任务就完成了,准备访问我们的博客吧。
6、预览效果
进入build/html
目录后用浏览器打开index.html
7、静态网页的部署
编译的网页,可以通过nginx部署访问,可以参考我的往期文章Nginx的常用操作将静态网页放在指定的位置,即可轻松访问了。
四、遇到的问题
1.图片不显示
解决方案:
- 1、图片使用图床加载
- 2、使用相对路径,如/images/a.jpg
注意:路径是/而非\ 第二种不可行
2.表格不显示
pip install sphinx-markdown-tables
在conf.py文件中修改
extensions = [
'sphinx.ext.autodoc',
'sphinx.ext.napoleon',
'sphinx.ext.mathjax',
'recommonmark',
'sphinx_markdown_tables',#这句话支持markdown表格
]
来都来了,点个赞再走呗!关注WangScaler,祝你升职、加薪、不提桶!