1
1
from concurrent .futures import ThreadPoolExecutor , as_completed
2
2
from pathlib import Path
3
3
from io import StringIO
4
+ from itertools import product
5
+ from re import finditer
4
6
from subprocess import run
5
7
from tempfile import TemporaryDirectory
6
8
7
9
from yaml import safe_load , dump as yaml_dump
8
10
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
+
9
26
def parse_yaml_header (f ):
10
27
metadata = StringIO ()
11
28
# advance to metadata section
@@ -36,8 +53,8 @@ def parse_yaml_header(f):
36
53
with ThreadPoolExecutor (4 ) as pool :
37
54
for name , uri in remotes .items ():
38
55
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
41
58
)
42
59
43
60
for name , result in futures .items ():
@@ -56,6 +73,10 @@ def parse_yaml_header(f):
56
73
57
74
with open (repo_root / '_materials' / f'{ name } .md' , 'w' ) as f :
58
75
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
+ ]
59
80
yaml_dump (remark_data , f , default_flow_style = False )
60
81
f .write ('---\n ' )
61
82
f .write (body )
0 commit comments