-
Notifications
You must be signed in to change notification settings - Fork 3k
/
Copy pathex_doc.exs
255 lines (223 loc) · 6.61 KB
/
ex_doc.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
cwd = File.cwd!()
## Read local configuration
local_config_file = Path.join(cwd, "docs.exs")
local_config =
case File.exists?(local_config_file) do
true ->
{local_config, _} = Code.eval_file(local_config_file)
local_config
false ->
[]
end
app =
if local_config[:application] do
local_config[:application]
else
Path.split(cwd) |> Enum.reverse() |> Enum.at(1) |> String.to_atom()
end
## Assert that cwd is a doc directory
true = String.equivalent?("doc", Path.split(cwd) |> Enum.reverse() |> Enum.at(0)) or app == :index
## Calculate relative path to $ERL_TOP
rootdir =
cond do
app in [:erts, :system] ->
"../../"
app == :index ->
"../../../"
true ->
"../../../"
end
libdir = Path.join(rootdir, "lib")
app_pos = length(Path.split(libdir))
link_libdir =
cond do
app == :index ->
Path.join(["..", "lib"])
true ->
Path.join("..", libdir)
end
deps =
Path.wildcard(Path.join(libdir, "*/ebin"))
|> Enum.map(fn path ->
app = Path.split(path) |> Enum.at(app_pos)
{String.to_atom(app), Path.join([link_libdir, app, "doc", "html"])}
end)
## Read the titles from all modules in order to create the groups_for_docs key
modules =
cond do
app == :erts ->
:erlang.pre_loaded()
app in [:system, :index] ->
[]
true ->
Application.load(app)
{:ok, modules} = :application.get_key(app, :modules)
modules
end
titles =
modules
|> Enum.reduce(
[],
fn module, acc ->
case Code.fetch_docs(module) do
{:docs_v1, _, :erlang, _, _, %{:titles => ts}, _} ->
acc ++ ts
_ ->
acc
end
end
)
|> Enum.group_by(fn e -> elem(e, 0) end)
groups_for_docs =
Enum.map(
Access.get(titles, :type, []),
fn {:type, title} ->
{"Types: #{title}",
fn a ->
a[:kind] == :type && String.equivalent?(Access.get(a, :title, ""), title)
end}
end
) ++
[Types: &(&1[:kind] == :type)] ++
Enum.map(
Access.get(titles, :callback, []),
fn {:callback, title} ->
{"Callbacks: #{title}",
fn a ->
a[:kind] == :callback && String.equivalent?(Access.get(a, :title, ""), title)
end}
end
) ++
[Callbacks: &(&1[:kind] == :callback)] ++
Enum.map(
Access.get(titles, :function, []),
fn {:function, title} ->
{"#{title}",
fn a ->
a[:kind] == :function && String.equivalent?(Access.get(a, :title, ""), title)
end}
end
)
## Create the correct source url to github
base_url = "https://github.com/" <> System.get_env("BASE_URL", "erlang/otp/blob/master/")
source_url_pattern = fn prefix, root ->
fn path, line ->
path = path |> Path.absname() |> Path.relative_to(root) |> String.replace_leading("./", "")
"#{base_url}#{prefix}#{path}#L#{line}"
end
end
source_url_pattern =
cond do
app == :erts ->
source_url_pattern.(
"erts/",
Path.join(:code.root_dir() |> String.Chars.to_string(), "erts")
)
app == :system ->
source_url_pattern.("", :code.root_dir() |> String.Chars.to_string())
app == :index ->
source_url_pattern.(
"system/doc/top/",
Path.join([:code.root_dir() |> String.Chars.to_string(), "system", "doc", "top"])
)
true ->
source_url_pattern.("lib/#{app}/", :code.lib_dir(app) |> String.Chars.to_string())
end
## Merge the local config with the default
extras =
(Access.get(local_config, :extras, []) ++
Path.wildcard("*.md") ++ Path.wildcard("{guides,references,internal_docs}/*.md"))
|> Enum.uniq()
annotations = Access.get(local_config, :annotations_for_docs, fn _ -> [] end)
current_datetime = System.os_time() |> DateTime.from_unix!(:native)
config = [
proglang: :erlang,
source_url_pattern: source_url_pattern,
assets: %{ Path.join(cwd, "/assets") => "assets" },
logo: Path.join(:code.root_dir(), "system/doc/assets/erlang-logo.png"),
before_closing_head_tag: fn _ -> "<style>.dark img { background-color: white; }</style>" end,
before_closing_footer_tag: fn _ ->
~s'<p>Copyright © 1996-#{current_datetime.year} <a href="https://www.ericsson.com">Ericsson AB</a></p>'
end,
annotations_for_docs: fn md ->
if Map.has_key?(md, :exported) && not md.exported do
["not exported"] ++ annotations.(md)
else
annotations.(md)
end
end,
groups_for_extras:
(Access.get(local_config, :groups_for_extras, []) ++
[
"User's Guides": ~r/guides/,
"Command Line Tools": ~r|references/.*_cmd.md$|,
References: ~r|references|,
"Internal Docs": ~r/internal_doc/
])
|> Enum.uniq(),
extras: extras,
skip_undefined_reference_warnings_on: ["notes.md"],
groups_for_docs:
(Access.get(local_config, :groups_for_docs, []) ++ groups_for_docs) |> Enum.uniq(),
deps:
deps ++
[
general_info: Path.join([link_libdir, "..", "doc"]),
erts: Path.join([link_libdir, "..", "erts", "doc", "html"]),
system: Path.join([link_libdir, "..", "system", "doc", "html"])
],
before_closing_body_tag: fn
:html ->
"""
<script>
function mermaidLoaded() {
mermaid.initialize({
startOnLoad: false,
theme: document.body.className.includes("dark") ? "dark" : "default"
});
let id = 0;
for (const codeEl of document.querySelectorAll("pre code.mermaid")) {
const preEl = codeEl.parentElement;
const graphDefinition = codeEl.textContent;
const graphEl = document.createElement("div");
const graphId = "mermaid-graph-" + id++;
mermaid.render(graphId, graphDefinition).then(({svg, bindFunctions}) => {
graphEl.innerHTML = svg;
bindFunctions?.(graphEl);
preEl.insertAdjacentElement("afterend", graphEl);
preEl.remove();
});
}
}
</script>
<script async src="https://cdn.jsdelivr.net/npm/[email protected]/dist/mermaid.min.js" onload="mermaidLoaded();"></script>
"""
_ ->
""
end,
before_closing_head_tag: fn
:epub ->
"""
<style type="text/css">
#monospace-font > code {
font-family: monospace;
}
.content-inner pre code.mermaid {
display: none;
}
</style>
"""
_ ->
"""
<style type="text/css">
.monospace-font > code {
font-family: monospace;
}
</style>
"""
end
]
Keyword.merge(
config,
local_config |> Keyword.drop([:extras, :groups_for_extras, :group_for_docs])
)