-
Notifications
You must be signed in to change notification settings - Fork 3
/
genversions.py
95 lines (79 loc) · 2.78 KB
/
genversions.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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#!/usr/bin/env python3
from bs4 import BeautifulSoup
import copy
import glob
import jinja2
import re
tpl_str = """
<div id="cvc5-versions" class="rst-versions shift-up" data-toggle="rst-versions" role="note" aria-label="versions">
<span class="rst-current-version" data-toggle="rst-current-version">
<i class="fas fa-tags"></i> Other versions
</span>
<div class="rst-other-versions">
<dl>
{% for version in versions %}
<dd>
{%- if version == curversion %}<strong>{% endif -%}
{%- if version == "cvc5-main" %}
<a href="https://cvc5.github.io/docs-ci/docs-main/">cvc5-main</a>
{%- else -%}
<a href="%URLROOT%/{{ version }}/">{{ version }}</a>
{% endif -%}
{%- if version == curversion %}</strong>{% endif -%}
</dd>
{% endfor %}
</dl>
</div>
</div>
"""
tpl = jinja2.Template(tpl_str)
tpl_redirect_str = """
<!DOCTYPE html>
<meta charset="utf-8">
<title>Redirect to latest release</title>
<meta http-equiv="refresh" content="0; URL={{ release }}/">
<link rel="canonical" href="{{ release }}/">
"""
tpl_redirect = jinja2.Template(tpl_redirect_str)
def put_versions_in_file(filename, newblock):
"""Insert or replace the `cvc5-versions` block with the new block."""
doc = BeautifulSoup(open(filename).read(), 'lxml')
# identify nav bar where the `cvc5-version` block shall go
nav = doc.find('nav', class_='wy-nav-side')
if not nav:
return
# find relative path to root dir
urlroot = doc.find('script', id='documentation_options')
if not urlroot:
return
urlroot = f'{urlroot["data-url_root"]}..'
for a in newblock.find_all('a'):
a['href'] = a['href'].replace('%URLROOT%', urlroot)
cur = nav.find(id='cvc5-versions')
if cur:
cur.replace_with(newblock)
else:
nav.append(newblock)
open(filename, 'w').write(doc.prettify())
def collect_versions():
"""Collect all paths / versions."""
return glob.glob('cvc5-*.*.*')
def list_files(basepath):
"""List all html files from this base directory."""
yield from glob.iglob(f'{basepath}/**/*.html', recursive=True)
release_versions = collect_versions()
# map "cvc5-x.y.z" to [x, y, z]
release_versions.sort(key=lambda v: list(
map(int,
re.match('cvc5-([0-9]+).([0-9]+).([0-9]+)', v).groups())),
reverse=True)
versions = ["cvc5-main"] + release_versions
for version in versions:
print(f"Process {version}...")
newvers = tpl.render(curversion=version, versions=versions)
newvers = BeautifulSoup(newvers, 'html.parser')
for file in list_files(version):
put_versions_in_file(file, copy.copy(newvers))
latest_version = release_versions[0]
newindex = tpl_redirect.render(release=latest_version)
open("index.html", 'w').write(newindex)