Skip to content

Commit 90c2061

Browse files
committed
Update gen_config to work with new Hugo config structure
1 parent e749f62 commit 90c2061

File tree

2 files changed

+47
-22
lines changed

2 files changed

+47
-22
lines changed

config.yaml.in

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,21 +27,24 @@ languages:
2727
title: NumPy
2828
weight: 1
2929
contentDir: content/en
30-
< content/en/config.yaml >
31-
< content/en/tabcontents.yaml >
30+
include-files:
31+
- content/en/config.yaml
32+
- content/en/tabcontents.yaml
3233

3334
# Portuguese
3435
pt:
3536
title: NumPy
3637
weight: 2
3738
contentDir: content/pt
38-
< content/pt/config.yaml >
39-
< content/pt/tabcontents.yaml >
39+
include-files:
40+
- content/pt/config.yaml
41+
- content/pt/tabcontents.yaml
4042

4143
# Japanese
4244
ja:
4345
title: NumPy
4446
weight: 3
4547
contentDir: content/ja
46-
< content/ja/config.yaml >
47-
< content/ja/tabcontents.yaml >
48+
include-files:
49+
- content/ja/config.yaml
50+
- content/ja/tabcontents.yaml

gen_config.py

Lines changed: 38 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,45 @@
11
import os
22
import re
3+
import yaml
34

45

5-
with open('config.yaml.in', 'r', encoding='utf-8') as templ:
6-
lines = templ.readlines()
6+
config = yaml.load(
7+
open("config.yaml.in", "r", encoding="utf-8"), Loader=yaml.SafeLoader
8+
)
79

8-
pattern = re.compile('< content\/\w\w\/\w*.yaml >')
9-
with open('config.yaml', 'w', encoding='utf-8') as f:
10-
for line in lines:
11-
match = pattern.search(line)
12-
if match:
13-
with open(match.group()[2:-2], 'r', encoding='utf-8') as f2:
14-
for f2_line in f2.readlines():
15-
# indent to get correct yaml formatting
16-
f.write(' ' + f2_line)
17-
elif line.startswith('disableLanguages'):
18-
if os.environ.get('NUMPYORG_WITH_TRANSLATIONS'):
19-
line = "#" + line
2010

21-
f.write(line)
11+
def merge_dicts(d1, d2):
12+
for key, value in d2.items():
13+
if key in d1:
14+
if isinstance(value, list):
15+
d1[key].extend(value)
16+
elif isinstance(value, dict):
17+
merge_dicts(d1[key], value)
2218
else:
23-
f.write(line)
19+
d1[key] = value
20+
21+
return d1
22+
23+
24+
def include_files(d):
25+
external = {}
26+
for key, val in d.items():
27+
if isinstance(val, dict):
28+
d[key] = include_files(val)
29+
elif key == "include-files":
30+
for otherfile in val:
31+
external_data = yaml.load(
32+
open(otherfile, "r", encoding="utf-8"), Loader=yaml.SafeLoader
33+
)
34+
external = merge_dicts(external, external_data)
35+
36+
d.pop("include-files", None)
37+
return {**d, **external}
38+
39+
40+
config = include_files(config)
41+
if os.environ.get("NUMPYORG_WITH_TRANSLATIONS"):
42+
del config["disableLanguages"]
43+
44+
45+
yaml.dump(config, open('config.yaml', 'w', encoding='utf-8'), sort_keys=False)

0 commit comments

Comments
 (0)