aboutsummaryrefslogtreecommitdiff
path: root/module/calp/html/util.scm
blob: 8410472caeb715da250977ec918624847c30a3f2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
(define-module (calp html util)
  :use-module ((base64) :select (base64encode base64decode))
  :use-module (util))

;;; @var{html-attr} & @var{html-unattr} used to just strip any
;;; attributes not valid in css. That allowed a human reader to
;;; quickly see what data it was. The downside was that it was one
;;; way. The new base64 based system supports both an encode and a
;;; decode without problem.
;;;
;;; The encoded string substitutes { + => å, / => ä, = => ö } to be
;;; valid CSS selector names.

;; Retuns an HTML-safe version of @var{str}.
(define-public (html-attr str)
  (string-map (lambda (c)
                (case c
                  ((#\+) #)
                  ((#\/) #)
                  ((#\=) #)
                  (else c)))
              (base64encode str)))

(define-public (html-unattr str)
  (base64decode
    (string-map (lambda (c)
                  (case c
                    ((#) #\+)
                    ((#) #\/)
                    ((#) #\=)
                    (else c)))
                str)))


(define-public (date-link date)
  ((@ (datetime) date->string) date "~Y-~m-~d"))



;; Generate an html id for an event.
;; TODO? same event placed multiple times, when spanning multiple cells
(define-public html-id
  (let ((id (make-object-property)))
    (lambda (ev)
      (or (id ev)
          (set/r! (id ev) (symbol->string (gensym "__html_id_")))))))