From e466d925f69eb145b6576be6a2b7603bb8d0f9b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Hoguin?= Date: Mon, 31 Oct 2016 17:49:49 +0200 Subject: Add compress and outdir options to man translator --- src/asciideck.erl | 6 +++++- src/asciideck_to_manpage.erl | 25 ++++++++++++++++++------- 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/src/asciideck.erl b/src/asciideck.erl index cc50a57..749ccec 100644 --- a/src/asciideck.erl +++ b/src/asciideck.erl @@ -20,6 +20,7 @@ -export([parse/2]). -export([to_manpage/1]). +-export([to_manpage/2]). parse_file(Filename) -> parse_file(Filename, #{}). @@ -37,4 +38,7 @@ parse(Data, St) -> parse(iolist_to_binary(Data), St). to_manpage(AST) -> - asciideck_to_manpage:translate(AST). + asciideck_to_manpage:translate(AST, #{}). + +to_manpage(AST, Opts) -> + asciideck_to_manpage:translate(AST, Opts). diff --git a/src/asciideck_to_manpage.erl b/src/asciideck_to_manpage.erl index 4158b4d..eafd275 100644 --- a/src/asciideck_to_manpage.erl +++ b/src/asciideck_to_manpage.erl @@ -12,26 +12,37 @@ %% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF %% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +%% The Groff documentation section 4.1 has a pretty good +%% description of the format expected for man pages. -module(asciideck_to_manpage). --export([translate/1]). +-export([translate/2]). -translate(AST) -> - Output = man(AST), - file:write_file("/tmp/man.3.gz", zlib:gzip(Output)), - ok. +translate(AST, Opts) -> + {Man, Section, Output0} = man(AST), + {CompressExt, Output} = case Opts of + #{compress := gzip} -> {".gz", zlib:gzip(Output0)}; + _ -> {"", Output0} + end, + case Opts of + #{outdir := Path} -> + file:write_file(binary_to_list(iolist_to_binary( + [Path, "/", Man, ".", Section, CompressExt])), Output); + _ -> + Output + end. man([{title, #{level := 0}, Title0, _Ann}|AST]) -> [Title, << Section:1/binary, _/bits >>] = binary:split(Title0, <<"(">>), Extra1 = "2016-10-17", %% @todo Extra2 = "Project 1.0", %% @todo Extra3 = "Project Function Reference", %% @todo - [ + {Title, Section, [ ".TH \"", Title, "\" \"", Section, "\" \"", Extra1, "\" \"", Extra2, "\" \"", Extra3, "\"\n" ".ta T 4n\n\\&\n", man(AST, []) - ]. + ]}. man([], Acc) -> lists:reverse(Acc); -- cgit v1.2.3