From a1439ba548dfdef1d8a8f1b27e6912d513eb5004 Mon Sep 17 00:00:00 2001 From: Philipp Gesang Date: Sat, 23 Jun 2018 14:58:57 +0200 Subject: cal: implement calendar printer Non-folding for the time being. --- cal.lua | 55 ++++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 48 insertions(+), 7 deletions(-) diff --git a/cal.lua b/cal.lua index 709597a..9382524 100755 --- a/cal.lua +++ b/cal.lua @@ -1,7 +1,7 @@ #!/usr/bin/env lua --[[-- - RFC2445 parser. + RFC2445 parser / printer. --]]-- @@ -36,16 +36,54 @@ local errorln = mk_out ("stderr", 0, true) local noiseln = mk_out ("stderr", 1, true) local debugln = mk_out ("stderr", 2, true) -local print_calendar -local fmt_calendar_params -do +local fmt_calendar do + local crlf = "\r\n" + + local fmt_line = function (ln) + local params = "" + local lparams = ln.params + if lparams ~= nil then + local acc = { } + for i = 1, #lparams do + local param = lparams [i] + local value = param.value + acc [#acc + 1] = ";" + acc [#acc + 1] = param.name + acc [#acc + 1] = "=" + for j = 1, #value do + if j > 1 then + acc [#acc + 1] = "," + end + acc [#acc + 1] = value [j] + end + end + params = tableconcat (acc) + end + return stringformat ("%s%s:%s", ln.name, params, ln.value) + end + + fmt_calendar = function (cal, newline, i, acc) + if i == nil then return fmt_calendar (cal, newline, 1, { }) end + if i > #cal then return tableconcat (acc, newline or crlf) end + + local cur = cal [i] + + acc [#acc + 1] = fmt_line (cur) + + return fmt_calendar (cal, newline, i + 1, acc) + end +end + +local fmt_calendar_params do fmt_calendar_params = function (params) local acc = { } local len = #params for i = 1, len do - local params = params [i] - acc [i] = stringformat ("“%s” → “%s”", params.name, params.value) + local param = params [i] + acc [i] = stringformat ("“%s” → [%s]", + param.name, + tableconcat (param.value, ", ")) end return tableconcat (acc, ", ") @@ -236,8 +274,11 @@ local main = function (argv) end local raw = loaddata (argv [1]) + local cal = parse_calendar (raw) + + if cal ~= nil then println (fmt_calendar (cal, "\n")) end - return parse_calendar (raw) + return 0 end return main (arg) -- cgit v1.2.3