-
-
Notifications
You must be signed in to change notification settings - Fork 33
/
mkdocs_macros.py
112 lines (93 loc) · 2.97 KB
/
mkdocs_macros.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
import json
import glob
import subprocess
# https://mkdocs-macros-plugin.readthedocs.io/en/latest/macros/
def define_env(env):
@env.macro
# Set indent=True when you want to define a box containing version-specific info.
#
# Set inline=True when you want to define a simple inline version indicator,
# such as when emitting information into a table row.
def since(vers, indent=False, inline=False):
scope = "section"
expanded = ""
expander = "???"
rule = ""
if indent:
scope = "outlined box"
expander = "!!!"
rule = " <hr/>"
if vers == "dev":
first_line = "*Since: Dev Builds Only*"
if scope != "section":
expanded = "+"
# Determine the relative path traversal to the root,
# so that we can emit the link to the install page
rel_root = "../" * (len(env.page.url.split('/')) - 2)
blurb = f"""
*The functionality described in this {scope} requires a dev build of KumoMTA.
You can obtain a dev build by following the instructions in the
[Installation]({rel_root}userguide/installation/linux.md) section.*
"""
else:
first_line = f"*Since: Version {vers}*"
blurb = f"""
*The functionality described in this {scope} requires version {vers} of KumoMTA,
or a more recent version.*
"""
if inline:
return f"({first_line})"
# If we're not expandable, don't emit the expanded marker
if expander == "!!!":
expanded = ""
return f"""
{expander}{expanded} info "{first_line}"
{blurb}
{rule}
"""
@env.macro
def toml_data(caller):
toml = caller()
second_line = toml.split('\n')[1]
indentation = len(second_line) - len(second_line.lstrip())
indent = " " * indentation
tab_indent = " " * (indentation + 4)
def remove_indent(s):
result = []
for line in s.split('\n'):
result.append(line[indentation:])
return "\n".join(result)
def apply_indent(s):
result = []
for line in s.split('\n'):
result.append(tab_indent + line)
return "\n".join(result)
toml = remove_indent(toml)
adjusted_toml = apply_indent(toml)
p = subprocess.Popen(["/util/toml2jsonc"],
encoding='utf-8',
stdin=subprocess.PIPE,
stderr=subprocess.PIPE,
stdout=subprocess.PIPE)
json, err = p.communicate(toml)
if err:
err = apply_indent(err)
err = f"""{indent}!!! error
{tab_indent}```
{err}
{tab_indent}```
"""
adjusted_json = apply_indent(json)
result = f"""
{indent}=== \"TOML\"
{tab_indent}```toml
{adjusted_toml}
{tab_indent}```
{indent}=== \"JSON\"
{tab_indent}```json
{adjusted_json}
{tab_indent}```
{err}
"""
# print(result)
return result