summaryrefslogtreecommitdiff
path: root/src/mulk/benki/util.clj
blob: 18c36071d128ad77a370aea7afee1078cf5c002b (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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
(ns mulk.benki.util
  (:refer-clojure)
  (:use [hiccup core page-helpers]
        [clojure.core.match :only [match]]
        noir.core)
  (:require [noir.session  :as session]
            [noir.request  :as request]
            [noir.response :as response])
  (:import [java.text DateFormat]))


(def fmt clojure.pprint/cl-format)


(defonce #^:private finished-initializations (atom #{}))

(defmacro do-once [key & body]
  `(while (not (@(deref #'finished-initializations) key))
     (let [fininit-copy# @(deref #'finished-initializations)]
       (when (compare-and-set! (deref #'finished-initializations)
                               fininit-copy#
                               (conj fininit-copy# key))
         (do ~@body)))))


;; defpartial is just defn + html.
(defpartial layout [kind title & content]
  (html5 {:xml? true}
   [:head
    [:title title]
    ;; jQuery
    [:script {:type "text/javascript"
              :src (resolve-uri "/3rdparty/jquery/jquery-1.7.min.js")}]
    (:head kind)]
   [:body [:h1 title]
    content
    (:bottom kind)]))


(defn fresolve [s & args]
  (resolve-uri (apply fmt nil s args)))

(defn link [& args]
  (match [(vec args)]
    [[:login]]           (fresolve "/login")
    [[:marx]]            (fresolve "/marx")
    [[:wiki title & xs]] (fresolve "/wiki/~a~@[~a~]" title (first xs))
    ))

(defn call-with-auth [thunk]
  (if (session/get :user)
    (thunk)
    (do (session/flash-put! (:uri (request/ring-request)))
        (response/redirect "/login"))))

(defmacro with-auth [& body]
  `(call-with-auth (fn [] ~@body)))

(defn redirect [x]
  {:status 302, :headers {"Location" x}, :body ""})

(defn format-date [x]
  (.format (DateFormat/getDateTimeInstance DateFormat/FULL DateFormat/FULL)
           x))