From 6980dd6517e0211380e89c91e23e11a20f90f7b4 Mon Sep 17 00:00:00 2001 From: Anders Ingemann Date: Sun, 12 Apr 2015 13:18:10 +0200 Subject: [PATCH] Add sphinx extension that replaces absolut RTD urls This makes it possible to add proper links when showing an rst on github, while also resolving it to relative links on readthedocs --- README.rst | 4 ++-- docs/conf.py | 1 + docs/replace_rtd_links.py | 19 +++++++++++++++++++ 3 files changed, 22 insertions(+), 2 deletions(-) create mode 100644 docs/replace_rtd_links.py diff --git a/README.rst b/README.rst index 93c699f..9a54e5c 100644 --- a/README.rst +++ b/README.rst @@ -7,8 +7,8 @@ bootstrap-vz runs without any user intervention and generates ready-to-boot images for a number of virtualization platforms. Its aim is to provide a reproducable bootstrapping process using -`manifests `__ as -well as supporting a high degree of customizability through plugins. +`manifests `__ +as well as supporting a high degree of customizability through plugins. bootstrap-vz was coded from scratch in python once the bash script architecture that was used in the diff --git a/docs/conf.py b/docs/conf.py index 7f412db..f6deef4 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -31,6 +31,7 @@ sys.path.insert(0, os.path.abspath(os.pardir)) extensions = ['sphinx.ext.coverage', 'sphinx.ext.autodoc', 'sphinx.ext.linkcode', + 'docs.replace_rtd_links', ] # Add any paths that contain templates here, relative to this directory. diff --git a/docs/replace_rtd_links.py b/docs/replace_rtd_links.py new file mode 100644 index 0000000..c18d1c4 --- /dev/null +++ b/docs/replace_rtd_links.py @@ -0,0 +1,19 @@ + +def setup(app): + app.connect('doctree-resolved', replace_rtd_links) + + return {'version': '0.1'} + + +def replace_rtd_links(app, doctree, fromdocname): + from docutils import nodes + import re + + rtd_baseurl = 'http://bootstrap-vz.readthedocs.org/en/master/' + search = re.compile('^' + re.escape(rtd_baseurl) + '(.*)$') + for node in doctree.traverse(nodes.reference): + if 'refuri' not in node: + continue + if not node['refuri'].startswith(rtd_baseurl): + continue + node['refuri'] = re.sub(search, r'\1', node['refuri'])