summaryrefslogtreecommitdiff
path: root/conf.py
diff options
context:
space:
mode:
authorJeff Epler <jepler@gmail.com>2020-06-14 13:04:21 -0500
committerJeff Epler <jepler@gmail.com>2020-06-14 13:04:21 -0500
commitcb8539b06dd4bce11207341a311bffca741d190c (patch)
tree9a685e011a87f6a229ca2a7ad78c44672dbd06cf /conf.py
parentf83b0c2507611a95ab4dea0c87b25ba85a0de9d6 (diff)
docs: Generate redirects for autoapi renaming
Diffstat (limited to 'conf.py')
-rw-r--r--conf.py45
1 files changed, 45 insertions, 0 deletions
diff --git a/conf.py b/conf.py
index 99423841b..d39f74bd7 100644
--- a/conf.py
+++ b/conf.py
@@ -14,6 +14,7 @@
# serve to show the default.
import json
+import logging
import os
import subprocess
import sys
@@ -82,6 +83,8 @@ autoapi_template_dir = 'docs/autoapi/templates'
autoapi_python_use_implicit_namespaces = True
autoapi_root = "shared-bindings"
+redirects_file = 'docs/redirects.txt'
+
# The encoding of source files.
#source_encoding = 'utf-8-sig'
@@ -376,5 +379,47 @@ intersphinx_mapping = {"cpython": ('https://docs.python.org/3/', None),
"bus_device": ('https://circuitpython.readthedocs.io/projects/busdevice/en/latest/', None),
"register": ('https://circuitpython.readthedocs.io/projects/register/en/latest/', None)}
+# Adapted from sphinxcontrib-redirects
+from sphinx.builders import html as builders
+
+TEMPLATE = """<html>
+ <head><meta http-equiv="refresh" content="0; url=%s"/></head>
+</html>
+"""
+
+
+def generate_redirects(app):
+ path = os.path.join(app.srcdir, app.config.redirects_file)
+ if not os.path.exists(path):
+ app.info("Could not find redirects file at '%s'" % path)
+ return
+
+ # TODO(stephenfin): Add support for DirectoryHTMLBuilder
+ if not type(app.builder) == builders.StandaloneHTMLBuilder:
+ logging.warn("The 'sphinxcontib-redirects' plugin is only supported "
+ "by the 'html' builder. Skipping...")
+ return
+
+ with open(path) as redirects:
+ for line in redirects.readlines():
+ from_path, to_path = line.rstrip().split(' ')
+
+ logging.debug("Redirecting '%s' to '%s'" % (from_path, to_path))
+
+ from_path = os.path.splitext(from_path)[0] + ".html"
+ to_path_prefix = '..%s' % os.path.sep * (
+ len(from_path.split(os.path.sep)) - 1)
+ to_path = to_path_prefix + to_path
+
+ redirected_filename = os.path.join(app.builder.outdir, from_path)
+ redirected_directory = os.path.dirname(redirected_filename)
+ if not os.path.exists(redirected_directory):
+ os.makedirs(redirected_directory)
+
+ with open(redirected_filename, 'w') as f:
+ f.write(TEMPLATE % urllib.parse.quote(to_path, '#/'))
+
def setup(app):
app.add_css_file("customstyle.css")
+ app.add_config_value('redirects_file', 'redirects', 'env')
+ app.connect('builder-inited', generate_redirects)