2020-07-02 11:14:28 +01:00
|
|
|
import os
|
2020-10-21 12:42:30 +01:00
|
|
|
import pathlib
|
2020-10-21 12:38:23 +01:00
|
|
|
from urllib.parse import urlparse
|
2020-07-02 11:14:28 +01:00
|
|
|
|
|
|
|
def create_redirect(dst):
|
|
|
|
tpl = '<html><head><meta http-equiv="refresh" content="0; url={0}"><script>window.location.replace("{0}")</script></head></html>'
|
|
|
|
return tpl.format(dst)
|
|
|
|
|
|
|
|
def create_redirects(app, docname):
|
|
|
|
if not app.builder.name == 'html':
|
|
|
|
return
|
2020-10-21 12:50:11 +01:00
|
|
|
for src, dst in app.config.html_redirects:
|
2020-07-02 11:14:28 +01:00
|
|
|
path = os.path.join(app.outdir, '{0}.html'.format(src))
|
2020-10-21 12:38:23 +01:00
|
|
|
|
2020-10-21 13:00:23 +01:00
|
|
|
os.makedirs(os.path.dirname(path), exist_ok=True)
|
2020-10-21 12:38:23 +01:00
|
|
|
|
|
|
|
if urlparse(dst).scheme == "":
|
2020-10-21 12:42:30 +01:00
|
|
|
dst = pathlib.posixpath.relpath(dst, start=os.path.dirname(src))
|
2020-10-21 12:38:23 +01:00
|
|
|
if not os.path.isfile(os.path.join(os.path.dirname(path), dst)):
|
|
|
|
raise Exception('{0} does not exitst'.format(dst))
|
|
|
|
|
2020-07-02 11:14:28 +01:00
|
|
|
with open(path, 'w') as f:
|
2020-09-25 11:49:03 +01:00
|
|
|
f.write(create_redirect(dst))
|
2020-07-02 11:14:28 +01:00
|
|
|
|
|
|
|
def setup(app):
|
2020-10-21 12:50:11 +01:00
|
|
|
app.add_config_value('html_redirects', [], '')
|
2020-07-02 11:14:28 +01:00
|
|
|
app.connect('build-finished', create_redirects)
|