summaryrefslogtreecommitdiff
path: root/compojure-3.2/src/compojure/html/page_helpers.clj
blob: 8c10e30d8c5e589644810a5884e833fc89799413 (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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
;; Copyright (c) James Reeves. All rights reserved.
;; The use and distribution terms for this software are covered by the Eclipse
;; Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) which
;; can be found in the file epl-v10.html at the root of this distribution. By
;; using this software in any fashion, you are agreeing to be bound by the
;; terms of this license. You must not remove this notice, or any other, from
;; this software.

(ns compojure.html.page-helpers
  "Functions for generating document and header boilerplate."
  (:use compojure.control
        compojure.html.gen
        compojure.str-utils
        clojure.contrib.str-utils)
  (:import java.net.URLEncoder))

(def doctype
  {:html4
   (str "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01//EN\" "
        "\"http://www.w3.org/TR/html4/strict.dtd\">\n")

   :html5
   (str "<!DOCTYPE html>")

   :xhtml-strict
   (str "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" "
        "\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n")

   :xhtml-transitional
   (str "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" "
        "\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n")})

(defn xhtml-tag
  "Create an XHTML tag for the specified locale.
   e.g. (xhtml \"en\"
          [:head ...]
          [:body ...])"
  [lang & contents]
  [:html {:xmlns "http://www.w3.org/1999/xhtml"
          "xml:lang" lang
          :lang lang}
    contents])

(defn include-js
  "Include a list of external javascript files."
  [& scripts]
  (domap [script scripts]
    [:script {:type "text/javascript" :src script}]))

(defn include-css
  "Include a list of external stylesheet files."
  [& styles]
  (domap [style styles]
    [:link {:type "text/css" :href style :rel "stylesheet"}]))

(defn javascript-tag
  "Wrap the supplied javascript up in script tags and a CDATA section."
  [script]
  [:script {:type "text/javascript"}
    (str "//<![CDATA[\n" script "\n//]]>")])

(defn link-to
  "Wraps some content in a HTML hyperlink with the supplied URL."
  [url & content]
  [:a {:href url} content])

(defn url-encode
  "Encodes a single string or sequence of key/value pairs."
  [string-or-map]
  (let [enc #(URLEncoder/encode (str* %))]
    (if (string? string-or-map)
      (enc string-or-map)
      (str-join "&"
        (map (fn [[key val]] (str (enc key) "=" (enc val)))
             string-or-map)))))

(defn url-params
  "Encodes a map of parameters and adds them onto the end of an existing
  address.
  e.g. (url-params \"http://example.com\" {:lang \"en\", :offset 10})
       => \"http://example.com?lang=en&offset=10\""
  [address param-map]
  (str address "?" (url-encode param-map)))

(defn unordered-list
  "Wrap a collection in an unordered list"
  [coll]
  [:ul {}
    (domap [x coll]
      [:li x])])

(defn ordered-list
  "Wrap a collection in an unordered list"
  [coll]
  [:ol {}
    (domap [x coll]
      [:li x])])

(decorate-with optional-attrs
  xhtml-tag
  link-to
  unordered-list
  ordered-list)