Skip to content

Commit f3cdd03

Browse files
dgarcia360Michal-Leszczynski
authored andcommitted
docs: add dynamic substitutions
1 parent 5a1176a commit f3cdd03

3 files changed

Lines changed: 61 additions & 0 deletions

File tree

Binary file not shown.
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
"""Sphinx extension to add dynamic substitutions
2+
based on the current documentation version.
3+
4+
Usage:
5+
6+
Add substitutions like |CURRENT_VERSION| in your
7+
reStructuredText files.
8+
9+
The extension will replace
10+
them with the appropriate values during the build process.
11+
"""
12+
13+
import os
14+
from sphinx.application import Sphinx
15+
from sphinx.util import logging
16+
17+
LOGGER = logging.getLogger(__name__)
18+
19+
class DynamicSubstitutions:
20+
21+
def _get_current_version(self, app):
22+
current_version = os.environ.get('SPHINX_MULTIVERSION_NAME', '')
23+
stable_version = app.config.smv_latest_version
24+
prefix = 'branch-'
25+
version = current_version
26+
27+
if current_version.startswith(prefix):
28+
version = current_version
29+
elif not stable_version.startswith(prefix):
30+
LOGGER.error("Invalid stable_version format in conf.py. It should start with 'branch-'")
31+
else:
32+
version = stable_version
33+
34+
return version.replace(prefix, '')
35+
36+
def run(self, app):
37+
current_version = self._get_current_version(app)
38+
39+
if not hasattr(app.config, 'rst_prolog') or app.config.rst_prolog is None:
40+
app.config.rst_prolog = ""
41+
elif not app.config.rst_prolog.endswith('\n'):
42+
app.config.rst_prolog += '\n'
43+
44+
app.config.rst_prolog += f"""
45+
.. |CURRENT_VERSION| replace:: {current_version}
46+
.. |UBUNTU_SCYLLADB_LIST| replace:: scylladb-manager-{current_version}.list
47+
.. |CENTOS_SCYLLADB_REPO| replace:: scylladb-manager-{current_version}.repo
48+
"""
49+
50+
def setup(app: Sphinx):
51+
app.connect("builder-inited", DynamicSubstitutions().run)
52+
53+
return {
54+
"version": "0.1",
55+
"parallel_read_safe": True,
56+
"parallel_write_safe": True,
57+
}

docs/source/conf.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
# -*- coding: utf-8 -*-
2+
import os
3+
import sys
24
from datetime import date
35

46
from sphinx_scylladb_theme.utils import multiversion_regex_builder
7+
sys.path.insert(0, os.path.abspath('./_ext'))
58

69
# -- Global variables
710

@@ -29,6 +32,7 @@
2932
"sphinx_sitemap",
3033
"sphinx_scylladb_theme",
3134
'sphinxcontrib.datatemplates',
35+
"scylladb_dynamic_substitutions",
3236
"sphinx_multiversion",
3337
"recommonmark",
3438
]

0 commit comments

Comments
 (0)