summaryrefslogtreecommitdiff
path: root/vendor/html.fnl
blob: 753bbb9d86cca022c9648662f1f6737cf3d51b0e (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
(local entity-replacements {"&" "&" ; must be first!
                            "<" "&lt;"
                            ">" "&gt;"
                            "\"" "&quot;"})

(local entity-search
       (.. "[" (table.concat (icollect [k (pairs entity-replacements)] k)) "]"))

(fn escape [s]
  (assert (= (type s) :string))
  (s:gsub entity-search entity-replacements))

(fn tag [tag-name attrs self-closing?]
  (assert (= (type attrs) "table") (.. "Missing attrs table: " tag-name))
  (let [attr-str (table.concat (icollect [k v (pairs attrs)]
                                 (if (= v true) k
                                     (.. k "=\"" (escape v)"\""))) " ")]
    (.. "<" tag-name " " attr-str (if self-closing? " />" ">"))))

(fn render [document allow-no-escape?]
  (if (= (type document) :string)
      (escape document)
      (and allow-no-escape? (= (. document 1) :NO-ESCAPE))
      (. document 2)
      (let [[tag-name attrs & body] document
            self-closing? (= 0 (# body))]
        (.. (tag tag-name attrs self-closing?)
            (table.concat (icollect [_ element (ipairs body)]
                            (render element allow-no-escape?)) " ")
            (if (not self-closing?) (.. "</" tag-name ">") "")))))

{ :render render }