-
Notifications
You must be signed in to change notification settings - Fork 145
/
redirecting_html.py
30 lines (25 loc) · 1011 Bytes
/
redirecting_html.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
from __future__ import unicode_literals
from docutils import nodes
try:
from urllib.parse import urlparse
except ImportError:
from urlparse import urlparse
from sphinx.builders.html import StandaloneHTMLBuilder
class RedirectingHTMLBuilder(StandaloneHTMLBuilder):
name = 'redirecting_html'
def write_doc(self, docname, doctree):
"""Replace URLs external to NIST with a redirection
Based slightly on `CheckExternalLinksBuilder.check()`
"""
for node in doctree.traverse(nodes.reference):
try:
uri = node['refuri']
uri = urlparse(uri)
if uri.scheme in ["http", "https"]:
if not uri.netloc.endswith("nist.gov"):
node['refuri'] = "/cgi-bin/redirect.py?url=" + uri.geturl()
except KeyError:
continue
super(RedirectingHTMLBuilder, self).write_doc(docname, doctree)
def setup(app):
app.add_builder(RedirectingHTMLBuilder)