-
-
Notifications
You must be signed in to change notification settings - Fork 101
/
section.clj
59 lines (46 loc) · 1.83 KB
/
section.clj
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
47
48
49
50
51
52
53
54
55
56
57
58
59
(ns clj-pdf.section
(:require [clj-pdf.utils :refer [split-classes-from-tag get-class-attributes]])
(:import com.lowagie.text.Chunk))
(declare ^:dynamic *cache*)
(defmulti render (fn [tag meta & els] tag))
(defmethod render :default [tag meta & els]
(throw (ex-info (str "invalid tag: " tag) {:meta meta :content els})))
(defn- keywordize [value]
(if (string? value) (keyword value) value))
(defn make-section
([element]
(cond
(instance? Chunk element) element
(empty? element) (Chunk. "")
(every? sequential? element) (doseq [item element]
(make-section item))
element (make-section {} element)
:else (Chunk. "")))
([meta element]
(try
(cond
(instance? Chunk element) element
(string? element) (Chunk. element)
(nil? element) (Chunk. "")
(number? element) (Chunk. (str element))
:else
(let [[tag & content] element
tag (keywordize tag)
[tag & classes] (split-classes-from-tag tag)
[attrs elements] (if (map? (first content))
[(first content) (rest content)]
[nil content])
stylesheet (:stylesheet meta)
new-meta (cond-> meta
stylesheet (merge (get-class-attributes stylesheet classes))
attrs (merge attrs))]
(apply render tag new-meta elements)))
(catch Exception e
#_(prn meta element)
(throw (ex-info "failed to parse element" {:meta meta :element element} e))))))
(defn make-section-or [if-string meta item]
(if (string? item)
(render if-string meta item)
(make-section meta item)))
;; that require is here to overcome circular import
(require '[clj-pdf.section core cell chart svg table])