-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgenerate-markdown.exs
executable file
·175 lines (151 loc) · 3.76 KB
/
generate-markdown.exs
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#!/usr/bin/env elixir
Mix.install([
{:hocon, github: "emqx/hocon", ref: "dcddb940ab2af56d6de2015347b6a7eb56948bdb"},
# {:hocon, path: "../hocon/"},
:jason
])
defmodule Main do
Code.require_file("md.exs")
Code.require_file("schema_md.exs")
def parse_args!(args) do
lang =
case OptionParser.parse!(args, strict: [lang: :string]) do
{[lang: lang], _} ->
lang
_ ->
Mix.raise("usage: generate-markdown.exs --lang {en,zh}")
end
unless lang in ["en", "zh"] do
Mix.raise("Invalid language #{lang}; options are `en`, `zh`")
end
%{lang: lang}
end
def sections() do
generic_section = [
%{
slug: "others",
title: "Other Configurations"
}
]
common_sections = [
%{
slug: "emqx",
title: "EMQX",
},
%{
slug: "authn",
title: "Authentication"
},
%{
slug: "authz",
title: "Authorization"
},
%{
slug: "bridges",
title: "Bridges"
},
%{
slug: "rule",
title: "Rule Engine"
},
%{
slug: "gateway",
title: "Gateways"
}
]
sections_ce = common_sections ++ generic_section
sections_ee =
common_sections ++
[
%{
slug: "license",
title: "License"
}
] ++ generic_section
%{
sections_ce: sections_ce,
sections_ee: sections_ee,
}
end
def make_index(sections, lang) do
preamble = File.read!("preamble.#{lang}.md")
entries =
sections
|> Enum.map(fn %{slug: slug, title: title} ->
MD.link(title, "./#{slug}.md")
end)
|> MD.ul()
"""
#{preamble}
# Sections
#{entries}
"""
end
@doc """
Creates an index mapping full names to files/sections, as they will
be cross-referenced by fields/structs in different sections.
"""
def index_full_names(dist_dir, sections) do
for %{slug: slug, title: _title} <- sections,
%{full_name: full_name} <- read_structs!(dist_dir, slug),
reduce: %{} do
acc ->
if Map.has_key?(acc, full_name) and acc[full_name] != slug do
ctx = %{
full_name: full_name,
acc: acc,
slug: slug,
existing: acc[full_name],
}
raise "repeated header! #{inspect(ctx, pretty: true)}"
end
Map.put(acc, full_name, slug)
end
end
def read_structs!(dist_dir, slug) do
[dist_dir, "json", "#{slug}.json"]
|> Path.join()
|> File.read!()
|> Jason.decode!(keys: :atoms)
end
def generate_markdown(opts) do
dist_dir_ce = "dist/emqx/"
dist_dir_ee = "dist/emqx-enterprise/"
%{lang: lang} = opts
%{
sections_ce: sections_ce,
sections_ee: sections_ee,
} = sections()
[
{dist_dir_ce, sections_ce},
{dist_dir_ee, sections_ee}
]
|> Enum.each(fn {dist_dir, sections} ->
Enum.each(sections, fn %{slug: slug, title: title} = section ->
index = make_index(sections, lang)
outfile = Path.join([dist_dir, "md", "index.md"])
File.write!(outfile, index)
header_index = index_full_names(dist_dir, sections)
body = Map.get(section, :body, "")
md =
dist_dir
|> read_structs!(slug)
|> SchemaMD.gen_from_structs(%{
title: "# #{title}",
body: body,
env_prefix: "EMQX_",
header_index: header_index,
current_slug: slug,
})
outfile = Path.join([dist_dir, "md", "#{slug}.md"])
File.write!(outfile, md)
end)
end)
end
def main() do
System.argv()
|> parse_args!()
|> generate_markdown()
end
end
Main.main()