-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathmkdocs_env.py
More file actions
109 lines (94 loc) · 3.46 KB
/
Copy pathmkdocs_env.py
File metadata and controls
109 lines (94 loc) · 3.46 KB
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
from typing import Any, Generator, Set
from rigour.mime.types import LABELS
from rigour.territories import get_territories
from rigour.ids import get_identifier_formats
from followthemoney import model, __version__
from followthemoney import registry
from followthemoney.property import Property
from followthemoney.schema import Schema
from followthemoney.types.common import PropertyType
def define_env(env):
"""
This is the hook for the variables, macros and filters.
"""
env.variables["model"] = model
env.variables["registry"] = registry
env.variables["ftm_version"] = __version__
env.variables["rigour_mime_types"] = LABELS
env.variables["rigour_territories"] = get_territories()
env.variables["rigour_id_formats"] = get_identifier_formats()
env.variables["risk_topics"] = registry.topic.RISKS
env.variables["NULL"] = "-"
@env.macro
def bool_icon(val: bool) -> str:
if val:
return ":material-check:"
return ":material-close:"
@env.macro
def prop_hidden_icon(prop: Property) -> str:
if prop.hidden:
return ":material-eye-off:"
return ""
@env.macro
def prop_featured_icon(prop: Property) -> str:
if prop.name in prop.schema.featured:
return ":material-star-circle-outline:"
return ""
@env.macro
def prop_matchable_icon(prop: Property) -> str:
if prop.matchable:
return ":material-transit-connection-horizontal:"
return ""
@env.macro
def prop_caption_icon(prop: Property) -> str:
if prop.name in prop.schema.caption:
return ":material-closed-caption-outline:"
return ""
@env.macro
def doc_string(val: Any) -> str:
return val.__doc__ or ""
@env.macro
def schema_ref(schema: Schema | str) -> str:
if isinstance(schema, Schema):
schema = schema.name
return f"[`{schema}`](/explorer/schemata/{schema}.md)"
@env.macro
def type_ref(type_: PropertyType | str) -> str:
return f"[`{type_}`](/explorer/types/{type_}.md)"
@env.macro
def prop_ref(prop: Property | str) -> str:
if isinstance(prop, Property):
schema, name = prop.schema.name, prop.name
else:
schema, name = prop.split(":")
qname = ":".join((schema, name))
return f"[`{qname}`](/explorer/schemata/{schema}.md#{name})"
@env.macro
def select_schema(name: str) -> Schema:
s = model.get(name)
assert s is not None, f"Schema not in model: `{name}`"
return s
@env.macro
def select_type(name: str) -> PropertyType:
t = registry.get(name)
assert t is not None, f"Property type not in model: `{name}`"
return t
@env.macro
def sorted_props(schema: Schema) -> Generator[Property, None, None]:
"""Sort properties by featured, required, own schema, other schemata"""
seen: Set[str] = set()
for prop in schema.caption:
seen.add(prop)
yield schema.properties[prop]
for prop in schema.featured:
if prop not in seen:
seen.add(prop)
yield schema.properties[prop]
for prop in schema.required:
if prop not in seen:
seen.add(prop)
yield schema.properties[prop]
for prop in schema.properties.values():
if prop.name not in seen:
seen.add(prop.name)
yield prop