-
Notifications
You must be signed in to change notification settings - Fork 278
/
docsite.erl
executable file
·111 lines (102 loc) · 3.5 KB
/
docsite.erl
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
#!/usr/bin/env escript
%% -*- erlang -*-
%%!
%% This script takes the EDoc output page, and tries to bring them to
%% a more modern format suitable for a recon site.
%% Run with `escript docsite.erl' or as `./docsite.erl'. The script
%% will call the `./rebar doc' command and generate a static site in
%% the site/ directory, which can then be shoved wherever.
-module(docsite).
-mode(compile).
-export([main/0, main/1]).
-define(DOCPATH, "doc/").
-define(SITEPATH, "site/").
main() -> main(["ex_doc"]).
main(["edoc"]) ->
ok = filelib:ensure_dir(?SITEPATH), % output directory
Output = os:cmd("rebar3 edoc"), % build docs
{match, _} = re:run(Output, "Running edoc"),
Overview = ?DOCPATH "overview-summary.html",
Modules = filelib:wildcard(filename:join(?DOCPATH, "recon*.html")),
[Pre,Post] = base(["index.html" | Modules]),
[write(?SITEPATH,
case File of
Overview -> "index.html";
_ -> File
end,
[Pre,content(File),Post])
|| File <- [Overview|Modules]],
halt(0);
main(_) ->
ok = filelib:ensure_dir(?SITEPATH), % output directory
Output = os:cmd("rebar3 ex_doc"), % build docs
{match, _} = re:run(Output, "Running ex_doc"),
os:cmd("cp -r " ++ ?DOCPATH ++ " " ++ ?SITEPATH),
halt(0).
content(File) ->
filter(
lists:reverse(
lists:foldl(fun("<h1>"++_ = Line, init) -> {keep, [Line]};
("<hr>"++_, {keep, Lines}) -> Lines;
(Line, {keep, Lines}) -> {keep, [Line|Lines]};
(_, Lines) -> Lines
end,
init,
lines(File))
)
).
filter(Content) ->
%% We filter using regexes. This is terrible but so easy.
lists:foldl(fun({RE,Replacement},Text) ->
re:replace(Text, RE, Replacement, [global])
end,
unicode:characters_to_binary(Content, latin1, utf8),
[{"h5>", "h6>"}, {"<h5", "<h6"},
{"h4>", "h5>"}, {"<h4", "<h5"},
{"h3>", "h4>"}, {"<h3", "<h4"},
{"h2>", "h3>"}, {"<h2", "<h3"},
{"h1>", "h2>"}, {"<h1", "<h2"},
{"tt>", "code>"}, {"<b>","<strong>"},
{"</b>", "</strong>"}]).
lines(File) ->
{ok, IoDevice} = file:open(File, [read, raw]),
get_lines(IoDevice).
get_lines(Device) ->
case file:read_line(Device) of
{ok, Data} -> [Data | get_lines(Device)];
eof -> []
end.
base(Files) ->
Modules = [{filename:basename(F,".html"),
filename:basename(F)} || F <- Files],
[["
<!DOCTYPE html>
<html lang=\"en\">
<head>
<meta http-equiv=\"Content-Type\" content=\"text/html;charset=UTF-8\">
<link href='http://fonts.googleapis.com/css?family=Titillium+Web:400,300italic,600italic,700,600,700italic' rel='stylesheet' type='text/css'>
<link href='http://fonts.googleapis.com/css?family=Ubuntu+Mono:400,700,700italic,400italic' rel='stylesheet' type='text/css'>
<link rel=\"stylesheet\" href=\"screen.css\" media=\"screen, projection\" />
<title>Recon Library</title>
</head>
<body>
<header>
<h1>Recon</h1>
<nav>
<ul>
",
[["<li><a href=\"", Path, "\">", Name, "</a>"] || {Name,Path} <- Modules],
"
</ul>
</nav>
</header>
<article>
"],
"
</article>
</body>
</html>
"].
write(DropPath, OriginalPath, Content) ->
NewPath = filename:join(DropPath, filename:basename(OriginalPath)),
file:write_file(NewPath, Content).