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+ }
0 commit comments