如何在标题中打印文档版本?

时间:2019-02-05 12:03:49

标签: python python-sphinx restructuredtext

我刚刚使用主题为sphinx-quickstart.exe的{​​{1}}创建了一个Sphinx文档。

我想在标题的某个位置打印文档的版本。

我在alabaster中填充了versionrelease变量

conf.py

# -*- coding: utf-8 -*- # # Configuration file for the Sphinx documentation builder. # -- Project information ----------------------------------------------------- project = 'MWE' copyright = '2019, and1er' author = 'and1er' # The short X.Y version version = '2.4.12' # The full version, including alpha/beta/rc tags release = 'beta' extensions = [ ] templates_path = ['_templates'] source_suffix = '.rst' master_doc = 'index' language = None exclude_patterns = ['_build', 'Thumbs.db', '.DS_Store'] pygments_style = 'sphinx' html_theme = 'alabaster' html_static_path = ['_static'] htmlhelp_basename = 'MWEdoc'

index.rst

结果文档没有.. MWE documentation master file, created by sphinx-quickstart on Tue Feb 5 14:51:07 2019. You can adapt this file completely to your liking, but it should at least contain the root `toctree` directive. Welcome to MWE's documentation! =============================== .. toctree:: :maxdepth: 2 :caption: Contents: Indices and tables ================== * :ref:`genindex` * :ref:`modindex` * :ref:`search` 2.4.12字符串。

Resulting document

2 个答案:

答案 0 :(得分:2)

|version| substitution对您有用吗?

UPD 更新的MWE

index.rst

.. MWE documentation master file, created by
   sphinx-quickstart on Tue Feb  5 14:51:07 2019.
   You can adapt this file completely to your liking, but it should at least
   contain the root `toctree` directive.

Welcome to MWE's documentation!
===============================

.. toctree::
   :maxdepth: 2
   :caption: Contents:

Document version:
|version|
|release|


Indices and tables
==================

* :ref:`genindex`
* :ref:`modindex`
* :ref:`search`

The expected result

答案 1 :(得分:1)

除了提到的@Slam之外,您还可以避免为项目conf.py中的每个发行版手动更新此设置。

import pkg_resources
version = pkg_resources.get_distribution('myproject').version
release = version

然后,|release|可以放在您的reST源文件中,也可以放在主题模板的{{ release }}中。