summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPhilipp Gesang <phg@phi-gamma.net>2018-06-23 14:58:57 +0200
committerPhilipp Gesang <phg@phi-gamma.net>2018-06-26 13:57:26 +0200
commita1439ba548dfdef1d8a8f1b27e6912d513eb5004 (patch)
treebd79fa416d9a7a5efa12e2d42a217e2ef0aab06b
parenta743c730417d9abcdab792724e869da4c431964c (diff)
downloadcaldr-a1439ba548dfdef1d8a8f1b27e6912d513eb5004.tar.gz
cal: implement calendar printer
Non-folding for the time being.
-rwxr-xr-xcal.lua55
1 files 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)