7
7
from recommonmark .transform import AutoStructify
8
8
from recommonmark .parser import CommonMarkParser , splitext , urlparse
9
9
from sphinx_scylladb_theme .utils import multiversion_regex_builder
10
-
10
+ from redirects_cli import cli as redirects_cli
11
11
12
12
# -- General configuration ------------------------------------------------
13
13
44
44
}
45
45
autosectionlabel_prefix_document = True
46
46
47
- class CustomCommonMarkParser (CommonMarkParser ):
48
-
49
- def visit_document (self , node ):
50
- pass
51
-
52
- def visit_link (self , mdnode ):
53
- # Override to avoid checking if relative links exists
54
- ref_node = nodes .reference ()
55
- destination = mdnode .destination
56
- _ , ext = splitext (destination )
57
-
58
- url_check = urlparse (destination )
59
- scheme_known = bool (url_check .scheme )
60
-
61
- if not scheme_known and ext .replace ('.' , '' ) in self .supported :
62
- destination = destination .replace (ext , '' )
63
- ref_node ['refuri' ] = destination
64
- ref_node .line = self ._get_line (mdnode )
65
- if mdnode .title :
66
- ref_node ['title' ] = mdnode .title
67
- next_node = ref_node
68
-
69
- self .current_node .append (next_node )
70
- self .current_node = ref_node
71
-
72
- def replace_relative_links (app , docname , source ):
73
- result = source [0 ]
74
- for key in app .config .replacements :
75
- result = re .sub (key , app .config .replacements [key ], result )
76
- source [0 ] = result
77
-
78
- def setup (app ):
79
- app .add_source_parser (CustomCommonMarkParser )
80
- app .add_config_value ('recommonmark_config' , {
81
- 'enable_eval_rst' : True ,
82
- 'enable_auto_toc_tree' : False ,
83
- }, True )
84
- app .add_transform (AutoStructify )
85
-
86
- # Replace DataStax links
87
- current_slug = os .getenv ("SPHINX_MULTIVERSION_NAME" , "stable" )
88
- replacements = {r'docs.datastax.com/en/drivers/java\/(.*?)\/' : "java-driver.docs.scylladb.com/" + current_slug + "/api/" }
89
- app .add_config_value ('replacements' , replacements , True )
90
- app .connect ('source-read' , replace_relative_links )
91
-
92
47
# The master toctree document.
93
48
master_doc = 'contents'
94
49
@@ -177,3 +132,61 @@ def setup(app):
177
132
178
133
# Dictionary of values to pass into the template engine’s context for all pages
179
134
html_context = {'html_baseurl' : html_baseurl }
135
+
136
+ # -- Initialize Sphinx ----------------------------------------------
137
+
138
+ class CustomCommonMarkParser (CommonMarkParser ):
139
+
140
+ def visit_document (self , node ):
141
+ pass
142
+
143
+ def visit_link (self , mdnode ):
144
+ # Override MarkDownParser to avoid checking if relative links exists
145
+ ref_node = nodes .reference ()
146
+ destination = mdnode .destination
147
+ _ , ext = splitext (destination )
148
+
149
+ url_check = urlparse (destination )
150
+ scheme_known = bool (url_check .scheme )
151
+
152
+ if not scheme_known and ext .replace ('.' , '' ) in self .supported :
153
+ destination = destination .replace (ext , '' )
154
+ ref_node ['refuri' ] = destination
155
+ ref_node .line = self ._get_line (mdnode )
156
+ if mdnode .title :
157
+ ref_node ['title' ] = mdnode .title
158
+ next_node = ref_node
159
+
160
+ self .current_node .append (next_node )
161
+ self .current_node = ref_node
162
+
163
+ def replace_relative_links (app , docname , source ):
164
+ result = source [0 ]
165
+ for key in app .config .replacements :
166
+ result = re .sub (key , app .config .replacements [key ], result )
167
+ source [0 ] = result
168
+
169
+ def build_finished (app , exception ):
170
+ version_name = os .getenv ("SPHINX_MULTIVERSION_NAME" , "" )
171
+ version_name = "/" + version_name if version_name else ""
172
+ redirect_to = version_name + '/api/index.html'
173
+ out_file = app .outdir + '/api.html'
174
+ redirects_cli .create (redirect_to = redirect_to ,out_file = out_file )
175
+
176
+ def setup (app ):
177
+ # Setup Markdown parser
178
+ app .add_source_parser (CustomCommonMarkParser )
179
+ app .add_config_value ('recommonmark_config' , {
180
+ 'enable_eval_rst' : True ,
181
+ 'enable_auto_toc_tree' : False ,
182
+ }, True )
183
+ app .add_transform (AutoStructify )
184
+
185
+ # Replace DataStax links
186
+ current_slug = os .getenv ("SPHINX_MULTIVERSION_NAME" , "stable" )
187
+ replacements = {r'docs.datastax.com/en/drivers/java\/(.*?)\/' : "java-driver.docs.scylladb.com/" + current_slug + "/api/" }
188
+ app .add_config_value ('replacements' , replacements , True )
189
+ app .connect ('source-read' , replace_relative_links )
190
+
191
+ # Create redirect to JavaDoc API
192
+ app .connect ('build-finished' , build_finished )
0 commit comments