-
-
Notifications
You must be signed in to change notification settings - Fork 38
/
config_update_to_v1.py
102 lines (89 loc) · 3.74 KB
/
config_update_to_v1.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
"""
mkdocs-static-i18n v0.x config migration script to v1.x.
This is provided as-is, pay special attention to the fact
that if you are using !ENV variables, they will be expanded
and converted in the resulting configuration so you will have
to update them back on your mkdocs.yml.
"""
import sys
from mkdocs.config.defaults import MkDocsConfig
from yaml import Dumper, dump
try:
mkdocs_config_path = sys.argv[1]
except IndexError:
mkdocs_config_path = "mkdocs.yml"
mkdocs_config = MkDocsConfig()
with open(mkdocs_config_path, "rb") as config_file:
mkdocs_config.load_file(config_file)
#
if "plugins" not in mkdocs_config:
raise ValueError(f"missing 'plugins' configuration key, found {mkdocs_config.keys()}")
for plugin in mkdocs_config["plugins"]:
if isinstance(plugin, dict) and "i18n" in plugin:
i18n_config = plugin["i18n"]
break
else:
raise ValueError("missing 'plugins.i18n' configuration!")
if "languages" not in i18n_config:
raise ValueError("missing 'plugins.i18n.languages' configuration!")
# basic check that we are using a v0.x config
if not isinstance(i18n_config["languages"], dict):
raise ValueError("your 'plugins.i18n.languages' configuration should be a dict")
# old config
default_language_only = i18n_config.get("default_language_only", False)
default_language = i18n_config["default_language"]
docs_structure = i18n_config.get("docs_structure", "suffix")
languages = i18n_config["languages"]
material_alternate = i18n_config.get("material_alternate", True)
nav_translations = i18n_config.get("nav_translations", {})
search_reconfigure = i18n_config.get("search_reconfigure", True)
# new config
v1_config = {
"docs_structure": docs_structure,
"reconfigure_material": material_alternate,
"reconfigure_search": search_reconfigure,
"fallback_to_default": True,
"languages": [],
}
for lang, lang_config in languages.items():
if lang == "default":
continue
new_config = {
"build": True,
"default": False,
"fixed_link": None,
"link": None,
"locale": lang,
"name": None,
"nav_translations": nav_translations.get(lang, {}),
"site_name": None,
}
# very old lang config as simple str
if isinstance(lang_config, str):
new_config["name"] = lang_config
new_config["default"] = lang == default_language
# v0.x dict language
if isinstance(lang_config, dict):
new_config["build"] = (
lang_config.get("build", True)
and not default_language_only
or lang == default_language
)
new_config["name"] = lang_config["name"]
new_config["default"] = lang_config.get("default", lang == default_language)
new_config["fixed_link"] = lang_config.get("fixed_link", None)
new_config["link"] = lang_config.get("link", None)
new_config["site_name"] = lang_config.get("site_name", None)
# cleanup useless keys
for key in list(new_config.keys()):
if new_config[key] in [None, {}]:
new_config.pop(key)
v1_config["languages"].append(new_config)
# ident two spaces for lists as advertised by mkdocs docs
#
class MyDumper(Dumper):
def increase_indent(self, flow=False, indentless=False):
return super(MyDumper, self).increase_indent(flow, False)
print("please update your mkdocs plugins.i18n configuration to:")
print("-" * 10)
print(dump({"i18n": v1_config}, Dumper=MyDumper, default_flow_style=False, allow_unicode=True))