Skip to content

Commit 237d352

Browse files
committed
add case-insensitive redirects
1 parent 43754c0 commit 237d352

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

_config.yml

+6
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,9 @@ defaults:
4141
type: 'team'
4242
values:
4343
order: 99
44+
45+
plugins:
46+
- jekyll-redirect-from
47+
48+
whitelist:
49+
- jekyll-redirect-from

scripts/populate_materials.py

+23-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,28 @@
11
from concurrent.futures import ThreadPoolExecutor, as_completed
22
from pathlib import Path
33
from io import StringIO
4+
from itertools import product
5+
from re import finditer
46
from subprocess import run
57
from tempfile import TemporaryDirectory
68

79
from yaml import safe_load, dump as yaml_dump
810

11+
def generate_case_combinations(name):
12+
# Find all uppercase letters and their positions
13+
uppercase_positions = [
14+
(m.start(), m.group()) for m in finditer(r'[A-Z]', name)
15+
]
16+
17+
# Generate all combinations of keeping the letter uppercase or converting to lowercase
18+
variations = product(*([ch.lower(), ch] for _, ch in uppercase_positions))
19+
20+
for variation in variations:
21+
name_list = list(name)
22+
for (pos, _), replacement in zip(uppercase_positions, variation):
23+
name_list[pos] = replacement
24+
yield ''.join(name_list)
25+
926
def parse_yaml_header(f):
1027
metadata = StringIO()
1128
# advance to metadata section
@@ -36,8 +53,8 @@ def parse_yaml_header(f):
3653
with ThreadPoolExecutor(4) as pool:
3754
for name, uri in remotes.items():
3855
futures[name] = pool.submit(
39-
lambda name, uri: run(['git', 'clone', '--sparse', uri], cwd=tmpdir),
40-
name=name, uri=uri
56+
lambda uri: run(['git', 'clone', '--sparse', uri], cwd=tmpdir),
57+
uri=uri
4158
)
4259

4360
for name, result in futures.items():
@@ -56,6 +73,10 @@ def parse_yaml_header(f):
5673

5774
with open(repo_root / '_materials' / f'{name}.md', 'w') as f:
5875
f.write('---\n')
76+
remark_data.setdefault('redirect_from', [])
77+
remark_data['redirect_from'] += [
78+
n for n in generate_case_combinations(name) if n != name
79+
]
5980
yaml_dump(remark_data, f, default_flow_style=False)
6081
f.write('---\n')
6182
f.write(body)

0 commit comments

Comments
 (0)