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

2024-10-01 04:57:52 发布

您现在位置:Python中文网/ 问答频道 /正文

我刚刚用sphinx-quickstart.exealabaster主题创建了一个Sphinx文档。你知道吗

我想把文件的版本印在标题的某个地方。你知道吗

我在conf.py中填充了versionrelease变量

# -*- 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.12beta字符串。你知道吗

Resulting document


Tags: the文档refreleaseversiondocumentationsphinxquickstart
2条回答

|version|substitution对你有用吗?你知道吗

升级版 更新的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

除了@Slam提到的替换之外,您还可以避免为项目的conf.py中的每个版本手动更新此设置。你知道吗

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

然后|release|可以放在reST源文件中,或者{{ release }}放在主题的模板中。你知道吗

相关问题 更多 >