手把手教你Sphinx(一)

本文介绍了Sphinx这一强大的文档生成工具,它能够将reStructuredText格式的文本转换为多种格式,如HTML、PDF等。文章详细讲解了Sphinx的安装过程、项目创建步骤及文档转换方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

手把手教你Sphinx(一)

1. 什么是Sphinx?

所谓Sphinx,其实就是一个能将reStructuredText(类似于Markdown)语法的文本文件转换为HTMLPDFepub等格式的强大工具。

要知道,Python的官方文档就是利用这款工具书写而成的,并且被广泛用于各式各样的说明文档。

2. Sphinx的安装

由于Sphinx运行于python之上,因此可以在命令行使用pip进行安装:

$ pip install sphinx

3. 创建项目

为了验证安装的成功与否,可以运行下面的命令

sphinx-quickstart 文档名

为了演示方便,下面除了Project nameAuthor name(s)Project version这必填的3项外,直接跳过其他项(啥都不填,直接按回车)。

Welcome to the Sphinx 1.6.3 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: sphinx-test

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]:

Inside the root directory, two more directories will be created; "_templates"
for custom HTML templates and "_static" for custom stylesheets and other static
files. You can enter another prefix (such as ".") to replace the underscore.
> Name prefix for templates and static dir [_]:

The project name will occur in several places in the built documentation.
> Project name: SphinxTest
> Author name(s): hymanlu

Sphinx has the notion of a "version" and a "release" for the
software. Each version can have multiple releases. For example, for
Python the version is something like 2.5 or 3.0, while the release is
something like 2.5.1 or 3.0a1.  If you don't need this dual structure,
just set both to the same value.
> Project version []: 1.0.0
> Project release [1.0.0]:

If 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
http://sphinx-doc.org/config.html#confval-language.
> Project language [en]: zh_CN

The file name suffix for source files. Commonly, this is either ".txt"
or ".rst".  Only files with this suffix are considered documents.
> Source file suffix [.rst]:

One document is special in that it is considered the top node of the
"contents tree", that is, it is the root of the hierarchical structure
of the documents. Normally, this is "index", but if your "index"
document is a custom template, you can also set this to another filename.
> Name of your master document (without suffix) [index]:

Sphinx can also add configuration for epub output:
> Do you want to use the epub builder (y/n) [n]:

Please indicate if you want to use one of the following Sphinx extensions:
> autodoc: automatically insert docstrings from modules (y/n) [n]:
> doctest: automatically test code snippets in doctest blocks (y/n) [n]:
> intersphinx: link between Sphinx documentation of different projects (y/n) [n]:
> todo: write "todo" entries that can be shown or hidden on build (y/n) [n]:
> coverage: checks for documentation coverage (y/n) [n]:
> imgmath: include math, rendered as PNG or SVG images (y/n) [n]:
> mathjax: include math, rendered in the browser by MathJax (y/n) [n]:
> ifconfig: conditional inclusion of content based on config values (y/n) [n]:
> viewcode: include links to the source code of documented Python objects (y/n) [n]:
> githubpages: create .nojekyll file to publish the document on GitHub pages (y/n) [n]:

A Makefile and a Windows command file can be generated for you so that you
only have to run e.g. `make html' instead of invoking sphinx-build
directly.
> Create Makefile? (y/n) [y]:
> Create Windows command file? (y/n) [y]:

Creating file sphinx-test\conf.py.
Creating file sphinx-test\index.rst.
Creating file sphinx-test\Makefile.
Creating file sphinx-test\make.bat.

Finished: An initial directory structure has been created.

You should now populate your master file sphinx-test\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.

4. 文档转换

在项目创建完之后,我们就可以将原始的reStructuredText文档转换成HTML网页代码:

cd sphinx-test/
make html

对于windows平台的用户可以执行下面的命令:

./make.bat html

转换生成的HTML文件保存在_build/html/index.html 目录下

同样地,我们可以通过make epub或者./make.bat epub来转换得到epub文件

Sphinx个在GPLv2 下发布的个全文检索引擎,商业授权(例如, 嵌入到其他程序中)需要联系我们(Sphinxsearch.com)以获得商业授权。 般而言,Sphinx个独立的搜索引擎,意图为其他应用提供高速、低空间占用、高结果相关度的全文搜索功能。Sphinx可以非常容易的与SQL数据库和脚本语言集成。 当前系统内置MySQL和PostgreSQL 数据库数据源的支持,也支持从标准输入读取特定格式的XML数据。通过修改源代码,用户可以自行增加新的数据源(例如:其他类型的DBMS 的原生支持)。 搜索API支持PHP、Python、Perl、Rudy和Java,并且也可以用作MySQL存储引擎。搜索API非常简单,可以在若干个小时之内移植到新的语言上。 Sphinx 是SQL Phrase Index的缩写,但不幸的和CMU的Sphinx 目录 1. 简介 1.1. 什么是 Sphinx 1.2. Sphinx 的特性 1.3. 如何获得 Sphinx 1.4. 许可协议 1.5. 作者和贡献者 1.6. 开发历史 2. 安装 2.1. 支持的操作系统 2.2. 依赖的工具 2.3. 安装 Sphinx 2.4. 已知的问题和解决方法 2.5. Sphinx 快速入门程 3. 建立索引 3.1. 数据源 3.2. 属性 3.3. 多值属性 ( MVA : multi-valued attributes) 3.4. 索引 3.5. 数据源的限制 3.6. 字符集 , 大小写转换 , 和转换表 3.7. SQL 数据源 (MySQL, PostgreSQL) 3.8. xmlpipe 数据源 3.9. xmlpipe2 数据源 3.10. 实时索引 更新 3.11. 索引合并 4. 搜索 4.1. 匹配模式 4.2. 布尔查询 4.3. 扩展查询 4.4. 权值计算 4.5. 排序模式 4.6. 结果分组(聚类) 4.7. 分布式搜索 4.8. searchd 日志格式 5. API 参考 5.1. 通用 API 方法 5.1.1. GetLastError 5.1.2. GetLastWarning 5.1.3. SetServer 5.1.4. SetRetries 5.1.5. SetArrayResult 5.2. 通用搜索设置 5.2.1. SetLimits 5.2.2. SetMaxQueryTime 5.3. 全文搜索设置 5.3.1. SetMatchMode 5.3.2. SetRankingMode 5.3.3. SetSortMode 5.3.4. SetWeights 5.3.5. SetFieldWeights 5.3.6. SetIndexWeights 5.4. 结果集过滤设置 5.4.1. SetIDRange 5.4.2. SetFilter 5.4.3. SetFilterRange 5.4.4. SetFilterFloatRange 5.4.5. SetGeoAnchor 5.5. GROUP BY 设置 5.5.1. SetGroupBy 5.5.2. SetGroupDistinct 5.6. 搜索 5.6.1. Query 5.6.2. AddQuery 5.6.3. RunQueries 5.6.4. ResetFilters 5.6.5. ResetGroupBy 5.7. 额外的方法 5.7.1. BuildExcerpts 5.7.2. UpdateAttributes 6. MySQL 存储引擎 (SphinxSE) 6.1. SphinxSE 概览 6.2. 安装 SphinxSE 6.2.1. 在 MySQL 5.0.x 上 编译 SphinxSE 6.2.2. 在 MySQL 5.1.x 上编译 SphinxSE 6.2.3. SphinxSE 安装测试 6.3. 使用 SphinxSE 7. 报告 bugs 8. sphinx.conf 选项参考 8.1. Data source 配置选项 8.1.1. type 8.1.2. sql_host 8.1.3. sql_port 8.1.4. sql_user 8.1.5. sql_pass 8.1.6. sql_db 8.1.7. sql_sock 8.1.8. mysql_connect_flags 8.1.9. sql_query_pre 8.1.10. sql_query 8.1.11. sql_query_range 8.1.12. sql_range_step 8.1.13. sql_attr_uint 8.1.14. sql_attr_bool 8.1.15. sql_attr_timestamp 8.1.16. sql_attr_str2ordinal 8.1.17. sql_attr_float 8.1.18. sql_attr_multi 8.1.19. sql_query_post 8.1.20. sql_query_post_index 8.1.21. sql_ranged_throttle 8.1.22. sql_query_info 8.1.23. xmlpipe_command 8.1.24. xmlpipe_field 8.1.25. xmlpipe_attr_uint 8.1.26. xmlpipe_attr_bool 8.1.27. xmlpipe_attr_timestamp 8.1.28. xmlpipe_attr_str2ordinal 8.1.29. xmlpipe_attr_float 8.1.30. xmlpipe_attr_multi 8.2. 索引配置选项 8.2.1. type 8.2.2. source 8.2.3. path 8.2.4. docinfo 8.2.5. mlock 8.2.6. morphology 8.2.7. stopwords 8.2.8. wordforms 8.2.9. exceptions 8.2.10. min_word_len 8.2.11. charset_type 8.2.12. charset_table 8.2.13. ignore_chars 8.2.14. min_prefix_len 8.2.15. min_infix_len 8.2.16. prefix_fields 8.2.17. infix_fields 8.2.18. enable_star 8.2.19. ngram_len 8.2.20. ngram_chars 8.2.21. phrase_boundary 8.2.22. phrase_boundary_step 8.2.23. html_strip 8.2.24. html_index_attrs 8.2.25. html_remove_elements 8.2.26. local 8.2.27. agent 8.2.28. agent_connect_timeout 8.2.29. agent_query_timeout 8.2.30. preopen 8.2.31. charset_dictpath 8.3. indexer 程序配置选项 8.3.1. mem_limit 8.3.2. max_iops 8.3.3. max_iosize 8.4. searchd 程序配置选项
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值