-
-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathconfig.py
126 lines (112 loc) · 5.02 KB
/
config.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
from re import compile
from mkdocs.config import config_options
from mkdocs.config.base import Config, ValidationError
RE_LOCALE = compile(r"(^[a-z]{2}(-[A-Za-z]{4})?(-[A-Z]{2})?$)|(^[a-z]{2}_[A-Z]{2}$)")
class Locale(config_options.Type):
"""
Locale Config Option
Validate the locale config option against a given Python type.
"""
def run_validation(self, value):
value = super().run_validation(value)
if not RE_LOCALE.match(value) and value.lower() != "null":
raise ValidationError(
"Language code values must be either ISO-639-1 lower case "
"or represented with their territory/region/country codes, "
f"received '{value}' expected forms examples: 'en' or 'en-US' or 'en_US'."
)
return value
class I18nPluginLanguage(Config):
""" """
build = config_options.Type(bool, default=True)
copyright = config_options.Optional(config_options.Type(str))
default = config_options.Type(bool, default=False)
extra = config_options.Optional(config_options.Type(dict))
fixed_link = config_options.Optional(config_options.Type(str))
link = config_options.Optional(config_options.Type(str))
locale = Locale(str)
name = config_options.Type(str)
nav = config_options.Optional(config_options.Nav())
nav_translations = config_options.Optional(config_options.Type(dict))
admonition_translations = config_options.Optional(config_options.Type(dict))
site_author = config_options.Optional(config_options.Type(str))
site_description = config_options.Optional(config_options.Type(str))
site_name = config_options.Optional(config_options.Type(str))
site_url = config_options.Optional(config_options.Type(str))
theme = config_options.Optional(config_options.Type(dict))
def validate(self):
failed, warnings = super().validate()
# set defaults
if self.link is None:
if self.default:
self.link = "/"
else:
self.link = f"/{self.locale}/"
# link should be absolute and end with a trailing /
else:
if not self.link.startswith("/") or not self.link.endswith("/"):
failed.append(
(
"link",
ValidationError(
f"languages[{self.locale}].link should be an absolute link starting "
f"with a leading / and ending with a / (like /{self.locale}/)."
),
)
)
# special "null" locale can be used to insert an item in the language switcher
# so it must never be built
if self.locale == "null":
self.build = False
if self.fixed_link is None:
failed.append(
(
"fixed_link",
ValidationError(
f"languages[{self.locale}].fixed_link should be set to a valid URL."
),
)
)
return failed, warnings
class I18nPluginConfig(Config):
""" """
build_only_locale = config_options.Optional(Locale(str))
docs_structure = config_options.Choice(["folder", "suffix"], default="suffix")
fallback_to_default = config_options.Type(bool, default=True)
reconfigure_material = config_options.Type(bool, default=True)
reconfigure_search = config_options.Type(bool, default=True)
languages = config_options.ListOfItems(
config_options.SubConfig(I18nPluginLanguage, validate=True)
)
def validate(self):
failed, warnings = super().validate()
if not failed:
if self.build_only_locale:
# check that the build_only_locale is valid
if self.build_only_locale not in [lang.locale for lang in self.languages]:
failed.append(
(
"build_only_locale",
ValidationError(
f"Locale {self.build_only_locale} is not present in languages"
),
)
)
# set other settings to valid values
else:
for lang in self.languages:
is_the_only_lang = lang.locale == self.build_only_locale
lang.default = is_the_only_lang
lang.build = is_the_only_lang
# check that we have at least a default language to build
if not any(filter(lambda c: c.default and c.build, self.languages)):
failed.append(
(
"languages",
ValidationError(
"Could not find a default language to build "
"(expecting 'default: true' AND 'build: true')."
),
)
)
return failed, warnings