-
Notifications
You must be signed in to change notification settings - Fork 51
/
setup-pages-for-supported-languages.py
46 lines (42 loc) · 1.82 KB
/
setup-pages-for-supported-languages.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
#!/usr/bin/env python3
# original source from https://github.com/guardianproject/info/blob/master/setup-pages-for-supported-languages.py
import glob
import os
import re
import shutil
import sys
import yaml
from yaml.loader import SafeLoader
with open('config.yaml') as fp:
#config = yaml.load(fp)
config = yaml.load(fp, Loader=SafeLoader)
rootfiles = dict()
languages = config['languages'].keys()
localized_pat = re.compile(r'.+\.([a-z][a-z](|-[A-Z][A-Z]|-Han[st]))$')
for root, dirs, files in os.walk('content'):
#if root.startswith('content/blog'):
# continue # skip since it is not translated
for f in files:
fileroot, extension = os.path.splitext(f)
if extension == '.md':
m = localized_pat.match(fileroot)
if m:
lang = m.group(1)
if lang in languages:
#print(f, '\t', m.group(1))
filename = os.path.join(root, f.replace('.' + lang + '.md', '.md'))
if not filename in rootfiles:
rootfiles[filename] = []
rootfiles[filename].append(lang)
continue
for lang in languages:
lang_file = os.path.join(root, fileroot + '.' + lang + extension)
if not os.path.exists(lang_file):
with open(os.path.join(root, f)) as fp:
contents = fp.read()
# for some reason, index.*.md copies need the menu entries
if f != 'index.md':
contents = re.sub(r'\naliases:.*?\n([^ ])', r'\n\1', contents, flags=re.DOTALL)
contents = re.sub(r'\nmenu:.*?\n([^ ])', r'\n\1', contents, flags=re.DOTALL)
with open(lang_file, 'w') as fp:
fp.write(contents)