forked from JonyEpsilon/gorilla-renderable
-
Notifications
You must be signed in to change notification settings - Fork 0
/
repl.cljc
73 lines (56 loc) · 1.68 KB
/
repl.cljc
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
(ns picasso.repl
"helper functions which should be available from the repl"
(:require
[picasso.protocols :refer [make Renderable render]] ; define Renderable (which has render function)
))
(defn text!
"renders text to a gorilla cell"
[text]
(reify Renderable
(render [_]
(make :text text))))
(defn html!
"renders html to a gorilla cell
if (type string) assumes it is valid html and renders as is
otherwise will assume it is valid hiccup syntax and render hiccup syntax"
[html-as-string]
(reify Renderable
(render [_]
(make :html html-as-string))))
(defn R!
"renders a pinkie hiccup spec"
[r]
(reify Renderable
(render [_]
(make :pinkie r))))
(defn R [data]
(R! data))
;; table-view
(defrecord TableView [contents opts])
(defn table-view [contents & opts]
(TableView. contents opts))
(defn- list-like
[data value open close separator]
{:type :list-like
:open open
:close close
:separator separator
:items data
:value value})
(extend-type TableView
Renderable
(render [self]
(let [contents (:contents self)
opts-map (apply hash-map (:opts self))
rows (map (fn [r] (list-like (map render r) (pr-str r) "<tr><td>" "</td></tr>" "</td><td>")) contents)
heading (if-let [cols (:columns opts-map)]
[(list-like (map render cols) (pr-str cols) "<tr><th>" "</th></tr>" "</th><th>")]
[])
body (list-like (concat heading rows) (pr-str self) "<center><table>" "</table></center>" "\n")]
body)))
(comment
(render (R! '[:h1 "hello, world"]))
(render (R! '[clock "hello, world"]))
(render (html! [:h1 "hello"]))
; comment end
)