From 770dbeaed202987b2d4f406a4c680e085d2e358c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hugo=20H=C3=B6rnquist?= Date: Thu, 30 Jul 2020 21:05:06 +0200 Subject: Move type formatters away from HTML. --- module/vcomponent/datetime/output.scm | 72 +++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 module/vcomponent/datetime/output.scm (limited to 'module/vcomponent/datetime/output.scm') diff --git a/module/vcomponent/datetime/output.scm b/module/vcomponent/datetime/output.scm new file mode 100644 index 00000000..eb127ceb --- /dev/null +++ b/module/vcomponent/datetime/output.scm @@ -0,0 +1,72 @@ +(define-module (vcomponent datetime output) + :use-module (util) + :use-module (util config) + :use-module (util exceptions) + :use-module (datetime) + :use-module (vcomponent base) + :use-module (text util) + ) + +(define-config summary-filter (lambda (_ a) a) + "" + pre: (ensure procedure?)) + +(define-config description-filter (lambda (_ a) a) + "" + pre: (ensure procedure?)) + +;; ev → sxml +(define-public (format-recurrence-rule ev) + `("Upprepas " + ,((@ (vcomponent recurrence display) format-recurrence-rule) + (prop ev 'RRULE)) + ,@(awhen (prop* ev 'EXDATE) + (list + ", undantaget " + (add-enumeration-punctuation + (map (lambda (d) + (if (date? d) + ;; NOTE possibly show year? + (date->string d "~e ~b") + ;; NOTE only show time when it's different than the start time? + ;; or possibly only when FREQ is hourly or lower. + (if (memv ((@ (vcomponent recurrence internal) freq) + (prop ev 'RRULE)) + '(HOURLY MINUTELY SECONDLY)) + (datetime->string d "~e ~b ~k:~M") + (datetime->string d "~e ~b")))) + (map value it))))) + ".")) + +(define-public (format-summary ev str) + ((get-config 'summary-filter) ev str)) + +;; NOTE this should have information about context (html/term/...) +(define-public (format-description ev str) + (catch #t (lambda () ((get-config 'description-filter) ev str)) + (lambda (err . args) + (warning "~a on formatting description, ~s" err args) + str))) + +;; Takes an event, and returns a pretty string for the time interval +;; the event occupies. +(define-public (fmt-time-span ev) + (cond [(prop ev 'DTSTART) date? + => (lambda (s) + (cond [(prop ev 'DTEND) + => (lambda (e) + (if (date= e (date+ s (date day: 1))) + (date->string s) ; start = end, only return one value + (values (date->string s) + (date->string e))))] + ;; no end value, just return start + [else (date->string s)]))] + [else ; guaranteed datetime + (let ((s (prop ev 'DTSTART)) + (e (prop ev 'DTEND))) + (if e + (let ((fmt-str (if (date= (get-date s) (get-date e)) + "~H:~M" "~Y-~m-~d ~H:~M"))) + (values (datetime->string s fmt-str) + (datetime->string e fmt-str))) + (datetime->string s "~Y-~m-~d ~H:~M")))])) -- cgit v1.2.3