| 1 |
%% Copyright (c) 2016-2018, Loïc Hoguin <[email protected]> |
| 2 |
%% |
| 3 |
%% Permission to use, copy, modify, and/or distribute this software for any |
| 4 |
%% purpose with or without fee is hereby granted, provided that the above |
| 5 |
%% copyright notice and this permission notice appear in all copies. |
| 6 |
%% |
| 7 |
%% THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 8 |
%% WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 9 |
%% MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
| 10 |
%% ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 11 |
%% WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 12 |
%% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
| 13 |
%% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 14 |
|
| 15 |
%% The Groff documentation section 4.1 has a pretty good |
| 16 |
%% description of the format expected for man pages. |
| 17 |
-module(asciideck_to_manpage). |
| 18 |
|
| 19 |
-export([translate/2]). |
| 20 |
|
| 21 |
translate(AST, Opts) -> |
| 22 |
{Man, Section, Output0} = man(AST, Opts), |
| 23 |
{CompressExt, Output} = case Opts of |
| 24 |
#{compress := gzip} -> {".gz", zlib:gzip(Output0)}; |
| 25 |
_ -> {"", Output0} |
| 26 |
end, |
| 27 |
case Opts of |
| 28 |
#{outdir := Path} -> |
| 29 |
file:write_file(binary_to_list(iolist_to_binary( |
| 30 |
[Path, "/", Man, ".", Section, CompressExt])), Output); |
| 31 |
_ -> |
| 32 |
Output |
| 33 |
end. |
| 34 |
|
| 35 |
%% Header of the man page file. |
| 36 |
|
| 37 |
man([{section_title, #{level := 0}, Title0, _Ann}|AST], Opts) -> |
| 38 |
ensure_name_section(AST), |
| 39 |
[Title, << Section:1/binary, _/bits >>] = binary:split(Title0, <<"(">>), |
| 40 |
Extra1 = maps:get(extra1, Opts, today()), |
| 41 |
Extra2 = maps:get(extra2, Opts, ""), |
| 42 |
Extra3 = maps:get(extra3, Opts, ""), |
| 43 |
{Title, Section, [ |
| 44 |
".TH \"", Title, "\" \"", Section, "\" \"", |
| 45 |
Extra1, "\" \"", Extra2, "\" \"", Extra3, "\"\n" |
| 46 |
".ta T 4n\n\\&\n", |
| 47 |
ast(AST) |
| 48 |
]}. |
| 49 |
|
| 50 |
ensure_name_section([{section_title, #{level := 1}, Title, _}|_]) -> |
| 51 |
case string:to_lower(string:strip(binary_to_list(Title))) of |
| 52 |
"name" -> ok; |
| 53 |
_ -> error(badarg) |
| 54 |
end; |
| 55 |
ensure_name_section(_) -> |
| 56 |
error(badarg). |
| 57 |
|
| 58 |
today() -> |
| 59 |
{{Y, M, D}, _} = calendar:universal_time(), |
| 60 |
io_lib:format("~b-~2.10.0b-~2.10.0b", [Y, M, D]). |
| 61 |
|
| 62 |
%% Loop over all types of AST nodes. |
| 63 |
|
| 64 |
ast(AST) -> |
| 65 |
fold(AST, fun ast_node/1). |
| 66 |
|
| 67 |
fold(AST, Fun) -> |
| 68 |
lists:reverse(lists:foldl( |
| 69 |
fun(Node, Acc) -> [Fun(Node)|Acc] end, |
| 70 |
[], AST)). |
| 71 |
|
| 72 |
ast_node(Node={Type, _, _, _}) -> |
| 73 |
try |
| 74 |
case Type of |
| 75 |
section_title -> section_title(Node); |
| 76 |
paragraph -> paragraph(Node); |
| 77 |
listing_block -> listing_block(Node); |
| 78 |
list -> list(Node); |
| 79 |
table -> table(Node); |
| 80 |
comment_line -> comment_line(Node); |
| 81 |
_ -> |
| 82 |
io:format("Ignored AST node ~p~n", [Node]), |
| 83 |
[] |
| 84 |
end |
| 85 |
catch _:_ -> |
| 86 |
io:format("Ignored AST node ~p~n", [Node]), |
| 87 |
[] |
| 88 |
end. |
| 89 |
|
| 90 |
%% Section titles. |
| 91 |
|
| 92 |
section_title({section_title, #{level := 1}, Title, _}) -> |
| 93 |
[".SH ", string:to_upper(binary_to_list(Title)), "\n"]; |
| 94 |
section_title({section_title, #{level := 2}, Title, _}) -> |
| 95 |
[".SS ", Title, "\n"]. |
| 96 |
|
| 97 |
%% Paragraphs. |
| 98 |
|
| 99 |
paragraph({paragraph, _, Text, _}) -> |
| 100 |
[".LP\n", inline(Text), "\n.sp\n"]. |
| 101 |
|
| 102 |
%% Listing blocks. |
| 103 |
|
| 104 |
listing_block({listing_block, Attrs, Listing, _}) -> |
| 105 |
[ |
| 106 |
case Attrs of |
| 107 |
#{<<"title">> := Title} -> |
| 108 |
[".PP\n\\fB", Title, "\\fR\n"]; |
| 109 |
_ -> |
| 110 |
[] |
| 111 |
end, |
| 112 |
".if n \\{\\\n" |
| 113 |
".RS 4\n" |
| 114 |
".\\}\n" |
| 115 |
".nf\n", |
| 116 |
Listing, |
| 117 |
"\n" |
| 118 |
".fi\n" |
| 119 |
".if n \\{\\\n" |
| 120 |
".RE\n" |
| 121 |
".\\}\n" |
| 122 |
]. |
| 123 |
|
| 124 |
%% Lists. |
| 125 |
|
| 126 |
list({list, #{type := bulleted}, Items, _}) -> |
| 127 |
fold(Items, fun bulleted_list_item/1); |
| 128 |
list({list, #{type := labeled}, Items, _}) -> |
| 129 |
fold(Items, fun labeled_list_item/1). |
| 130 |
|
| 131 |
bulleted_list_item({list_item, _, [{paragraph, _, Text, _}|AST], _}) -> |
| 132 |
[ |
| 133 |
".ie n \\{\\\n" |
| 134 |
".RS 2\n" |
| 135 |
"\\h'-02'\\(bu\\h'+01'\\c\n" |
| 136 |
".\\}\n" |
| 137 |
".el \\{\\\n" |
| 138 |
".RS 4\n" |
| 139 |
".sp -1\n" |
| 140 |
".IP \\(bu 2.3\n" |
| 141 |
".\\}\n", |
| 142 |
inline(Text), "\n", |
| 143 |
ast(AST), |
| 144 |
".RE\n" |
| 145 |
]. |
| 146 |
|
| 147 |
labeled_list_item({list_item, #{label := Label}, [{paragraph, _, Text, _}|AST], _}) -> |
| 148 |
[ |
| 149 |
".PP\n" |
| 150 |
"\\fB", inline(Label), "\\fR\n", |
| 151 |
".RS 4\n", |
| 152 |
inline(Text), "\n", |
| 153 |
ast(AST), |
| 154 |
".RE\n" |
| 155 |
]. |
| 156 |
|
| 157 |
%% Tables. |
| 158 |
|
| 159 |
table({table, _, Rows0, _}) -> |
| 160 |
Rows = table_apply_options(Rows0), |
| 161 |
[ |
| 162 |
".TS\n" |
| 163 |
"allbox tab(:);\n", |
| 164 |
table_style(Rows), ".\n", |
| 165 |
table_contents(Rows), |
| 166 |
".TE\n" |
| 167 |
".sp 1\n" |
| 168 |
]. |
| 169 |
|
| 170 |
%% @todo Currently acts as if options="headers" was always set. |
| 171 |
table_apply_options([{row, RAttrs, Headers0, RAnn}|Tail]) -> |
| 172 |
Headers = [{cell, CAttrs, [{strong, #{}, CText, CAnn}], CAnn} |
| 173 |
|| {cell, CAttrs, CText, CAnn} <- Headers0], |
| 174 |
[{row, RAttrs, Headers, RAnn}|Tail]. |
| 175 |
|
| 176 |
table_style(Rows) -> |
| 177 |
[[table_style_cells(Cells), "\n"] |
| 178 |
|| {row, _, Cells, _} <- Rows]. |
| 179 |
|
| 180 |
table_style_cells(Cells) -> |
| 181 |
["lt " || {cell, _, _, _} <- Cells]. |
| 182 |
|
| 183 |
table_contents(Rows) -> |
| 184 |
[[table_contents_cells(Cells), "\n"] |
| 185 |
|| {row, _, Cells, _} <- Rows]. |
| 186 |
|
| 187 |
table_contents_cells([FirstCell|Cells]) -> |
| 188 |
[table_contents_cell(FirstCell), |
| 189 |
[[":", table_contents_cell(Cell)] || Cell <- Cells]]. |
| 190 |
|
| 191 |
table_contents_cell({cell, _, Text, _}) -> |
| 192 |
["T{\n", inline(Text), "\nT}"]. |
| 193 |
|
| 194 |
%% Comment lines are printed in the generated file |
| 195 |
%% but are not visible in viewers. |
| 196 |
|
| 197 |
comment_line({comment_line, _, Text, _}) -> |
| 198 |
["\\# ", Text, "\n"]. |
| 199 |
|
| 200 |
%% Inline formatting. |
| 201 |
|
| 202 |
inline(Text) when is_binary(Text) -> |
| 203 |
Text; |
| 204 |
%% When the link is the text we only print it once. |
| 205 |
inline({link, #{target := Link}, Link, _}) -> |
| 206 |
Link; |
| 207 |
inline({link, #{target := Link}, Text, _}) -> |
| 208 |
case re:run(Text, "^([-_:.a-zA-Z0-9]*)(\\([0-9]\\))$", [{capture, all, binary}]) of |
| 209 |
nomatch -> [Text, " (", Link, ")"]; |
| 210 |
{match, [_, ManPage, ManSection]} -> ["\\fB", ManPage, "\\fR", ManSection] |
| 211 |
end; |
| 212 |
inline({emphasized, _, Text, _}) -> |
| 213 |
["\\fI", inline(Text), "\\fR"]; |
| 214 |
inline({strong, _, Text, _}) -> |
| 215 |
["\\fB", inline(Text), "\\fR"]; |
| 216 |
%% We are already using a monospace font. |
| 217 |
inline({inline_literal_passthrough, _, Text, _}) -> |
| 218 |
inline(Text); |
| 219 |
%% Xref links appear as plain text in manuals. |
| 220 |
inline({xref, _, Text, _}) -> |
| 221 |
inline(Text); |
| 222 |
inline(Text) when is_list(Text) -> |
| 223 |
[inline(T) || T <- Text]. |