Skip to content

Commit 32a1314

Browse files
committed
Use environment variables to get project and version slugs
1 parent f42a01a commit 32a1314

3 files changed

Lines changed: 17 additions & 4 deletions

File tree

README.rst

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,16 @@ Then in your ``conf.py`` you have to add ``versionwarning`` in the ``extensions`
5959
Enable it and setup the Read the Docs project's slug::
6060

6161
versionwarning_enabled = True
62-
versionwarning_project_slug = 'sphinx-version-warning'
6362

6463
Remember to configure the ``version`` of your Sphinx project since it's the key for this to work properly::
6564

6665
version = '0.0.1'
6766

67+
.. warning::
68+
69+
If ``READTHEDOCS_VERSION`` variable is defined, the extension will use its value.
70+
Otherwise, ``versionwarning_project_version`` will be used and if it's not defined, ``version`` as last resource.
71+
6872

6973
Customization
7074
-------------
@@ -87,7 +91,10 @@ versionwarning_message_placeholder (string)
8791
text to be replaced by the version number link from the message
8892

8993
versionwarning_project_slug (string)
90-
slug of the project under Read the Docs
94+
slug of the project under Read the Docs (default to ``READTHEDOCS_PROJECT`` environment variable)
95+
96+
versionwarning_project_version (string)
97+
slug of the version for the current documentation (default to ``READTHEDOCS_VERSION`` environment variable or ``version`` variable from ``conf.py``)
9198

9299
versionwarning_api_url (string)
93100
API URL to retrieve all versions for this project

versionwarning/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66

77
def setup(app):
8+
import os
89
import sphinx
910
from .signals import process_version_warning_banner, generate_versionwarning_data_json
1011

@@ -33,7 +34,8 @@ def setup(app):
3334
app.add_config_value('versionwarning_banner_id_div', 'version-warning-banner', 'html')
3435
app.add_config_value('versionwarning_body_default_selector', 'div.body', 'html')
3536
app.add_config_value('versionwarning_body_extra_selector', 'div.document', 'html')
36-
app.add_config_value('versionwarning_project_slug', None, 'html')
37+
app.add_config_value('versionwarning_project_slug', os.environ.get('READTHEDOCS_PROJECT', None), 'html')
38+
app.add_config_value('versionwarning_project_version', os.environ.get('READTHEDOCS_VERSION', None), 'html')
3739

3840
app.connect('doctree-resolved', process_version_warning_banner)
3941

versionwarning/banner.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,11 @@ def _default_admonition_type(self):
8989

9090
@property
9191
def _current_doc_version_slug(self):
92-
return self.app.config.version
92+
return (
93+
os.environ.get('READTHEDOCS_VERSION', None) or
94+
self.app.config.versionwarning_project_version or
95+
self.app.config.version
96+
)
9397

9498
@property
9599
def _latest_doc_version(self):

0 commit comments

Comments
 (0)