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