Use nicer URLs without .html suffix on website

Also easier insertion of analytics tag for the website without
needing a custom template.

The old .html URLs are redirected via meta http-equiv refresh, which is
all gh-pages supports
This commit is contained in:
Kovid Goyal 2021-07-17 09:59:46 +05:30
parent 8e5175e56b
commit 7c1b819728
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
3 changed files with 26 additions and 31 deletions

View File

@ -2,7 +2,7 @@
# #
# You can set these variables from the command line. # You can set these variables from the command line.
SPHINXOPTS = -j auto -T $(FAIL_WARN) SPHINXOPTS = -j auto -T $(FAIL_WARN) $(OPTS)
SPHINXBUILD = sphinx-build SPHINXBUILD = sphinx-build
SPHINXPROJ = kitty SPHINXPROJ = kitty
SOURCEDIR = . SOURCEDIR = .

View File

@ -1,6 +0,0 @@
{% extends "!layout.html" %}
{%- block extrahead %}
<!-- kitty analytics placeholder -->
{{ super() }}
{% endblock %}

View File

@ -63,29 +63,31 @@ def run_man(args: Any) -> None:
def run_html(args: Any) -> None: def run_html(args: Any) -> None:
call('make FAIL_WARN=-W html', cwd=docs_dir) call('make FAIL_WARN=-W "OPTS=-D html_theme_options.analytics_id=UA-20736318-2" dirhtml', cwd=docs_dir)
add_old_redirects('docs/_build/dirhtml')
def add_analytics() -> None: def add_old_redirects(loc: str) -> None:
analytics = ''' for dirpath, dirnames, filenames in os.walk(loc):
<!-- Global site tag (gtag.js) - Google Analytics --> if dirpath != loc:
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-20736318-2"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-20736318-2');
</script>
'''
for dirpath, firnames, filenames in os.walk(publish_dir):
for fname in filenames: for fname in filenames:
if fname.endswith('.html'): if fname == 'index.html':
with open(os.path.join(dirpath, fname), 'r+b') as f: bname = os.path.basename(dirpath)
html = f.read().decode('utf-8') base = os.path.dirname(dirpath)
html = html.replace('<!-- kitty analytics placeholder -->', analytics, 1) link_name = os.path.join(base, f'{bname}.html') if base else f'{bname}.html'
f.seek(0), f.truncate() with open(link_name, 'w') as f:
f.write(html.encode('utf-8')) f.write(f'''
<html>
<head>
<title>Redirecting...</title>
<link rel="canonical" href="{bname}/" />
<meta http-equiv="refresh" content="0;url={bname}/" />
</head>
<body>
<p>Redirecting, please wait...</p>
</body>
</html>
''')
def run_docs(args: Any) -> None: def run_docs(args: Any) -> None:
@ -95,8 +97,7 @@ def run_docs(args: Any) -> None:
def run_website(args: Any) -> None: def run_website(args: Any) -> None:
if os.path.exists(publish_dir): if os.path.exists(publish_dir):
shutil.rmtree(publish_dir) shutil.rmtree(publish_dir)
shutil.copytree(os.path.join(docs_dir, '_build', 'html'), publish_dir) shutil.copytree(os.path.join(docs_dir, '_build', 'dirhtml'), publish_dir, symlinks=True)
add_analytics()
with open(os.path.join(publish_dir, 'current-version.txt'), 'w') as f: with open(os.path.join(publish_dir, 'current-version.txt'), 'w') as f:
f.write(version) f.write(version)
shutil.copy2(os.path.join(docs_dir, 'installer.sh'), publish_dir) shutil.copy2(os.path.join(docs_dir, 'installer.sh'), publish_dir)
@ -408,7 +409,7 @@ def main() -> None:
if ans.lower() != 'y': if ans.lower() != 'y':
return return
if actions == ['website']: if actions == ['website']:
actions.insert(0, 'docs') actions.insert(0, 'html')
for action in actions: for action in actions:
print('Running', action) print('Running', action)
cwd = os.getcwd() cwd = os.getcwd()