Skip to content

Commit f128b1a

Browse files
sideshowbarkersvgeesus
authored andcommitted
Make GH Pages deployment create symlinks rather than redirects
This change makes the GH pages deployment create symlinks for all un-leveled shortnames, pointing to the appropriate leveled shortnames — eliminating the need for the web server to do any rewrites, and eliminating the need for any redirects (either server- or client-side). Thus, it replaces the existing build mechanism that generated index.html files containing client-side redirects.
1 parent f48a1f6 commit f128b1a

File tree

4 files changed

+10
-51
lines changed

4 files changed

+10
-51
lines changed

.github/workflows/build-specs.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ jobs:
5353
for file in ./**/Overview.html; do
5454
cp "$file" "$(dirname "$file")/index.html"
5555
done
56-
- name: Build index & redirects
56+
- name: Build index & symlinks
5757
run: python ./bin/build-index.py
5858
- run: rm -rf ./.git{,attributes,ignore}
5959

bin/build-index.py

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -108,27 +108,15 @@ def get_html_spec_metadata(folder_name, path):
108108
}
109109

110110

111-
def build_redirect(shortname, spec_folder):
112-
"""Builds redirects from the shortname to the current work for that spec.
113-
114-
Since Github Actions doesn't allow anything like mod_rewrite, we do this by
115-
creating an empty index.html in the shortname folder that redirects to the
116-
correct spec.
111+
def create_symlink(shortname, spec_folder):
112+
"""Creates a <shortname> symlink pointing to the given <spec_folder>.
117113
"""
118114

119-
template = jinja_env.get_template("redirect.html.j2")
120-
contents = template.render(spec_folder=spec_folder)
121-
122-
folder = os.path.join(".", shortname)
123115
try:
124-
os.mkdir(folder)
125-
except FileExistsError:
116+
os.symlink(spec_folder, shortname)
117+
except OSError:
126118
pass
127119

128-
index = os.path.join(folder, "index.html")
129-
with open(index, mode='w', encoding="UTF-8") as f:
130-
f.write(contents)
131-
132120

133121
CURRENT_WORK_EXCEPTIONS = {
134122
"css-conditional": 5,
@@ -148,7 +136,7 @@ def build_redirect(shortname, spec_folder):
148136
specgroups = defaultdict(list)
149137

150138
for entry in os.scandir("."):
151-
if entry.is_dir():
139+
if entry.is_dir(follow_symlinks=False):
152140
# Not actual specs, just examples.
153141
if entry.name in ["css-module"]:
154142
continue
@@ -172,7 +160,7 @@ def build_redirect(shortname, spec_folder):
172160
for shortname, specgroup in specgroups.items():
173161
if len(specgroup) == 1:
174162
if shortname != specgroup[0]["dir"]:
175-
build_redirect(shortname, specgroup[0]["dir"])
163+
create_symlink(shortname, specgroup[0]["dir"])
176164
else:
177165
specgroup.sort(key=lambda spec: spec["level"])
178166

@@ -194,9 +182,9 @@ def build_redirect(shortname, spec_folder):
194182
currentWorkDir = specgroup[-1]["dir"]
195183

196184
if shortname != currentWorkDir:
197-
build_redirect(shortname, currentWorkDir)
185+
create_symlink(shortname, currentWorkDir)
198186
if shortname == "css-snapshot":
199-
build_redirect("css", currentWorkDir)
187+
create_symlink("css", currentWorkDir)
200188

201189
with open("./index.html", mode='w', encoding="UTF-8") as f:
202190
template = jinja_env.get_template("index.html.j2")

bin/templates/index.html.j2

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,8 @@
5555

5656
<ul>
5757
{% for shortname, specgroup in specgroups|dictsort %}
58-
{% if specgroup|count == 1 %}
59-
{% set spec = specgroup[0] %}
60-
<li><a href="./{{ spec.dir }}">{{ spec.title }}</a></li>
61-
{% else %}
6258
<li>
63-
<p>{{ shortname }}</p>
59+
<p><a href="./{{ shortname }}">{{ shortname }}</a></p>
6460
<ul>
6561
{% for spec in specgroup %}
6662
<li><a href="./{{ spec.dir }}">{{ spec.title }}</a>
@@ -71,6 +67,5 @@
7167
{% endfor %}
7268
</ul>
7369
</li>
74-
{% endif %}
7570
{% endfor %}
7671
</ul>

bin/templates/redirect.html.j2

Lines changed: 0 additions & 24 deletions
This file was deleted.

0 commit comments

Comments
 (0)