-
Notifications
You must be signed in to change notification settings - Fork 175
/
page.clj
87 lines (77 loc) · 2.83 KB
/
page.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
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
(ns hiccup.page
"Functions for setting up HTML pages."
(:require [hiccup.core :refer [html]]
[hiccup.def :refer [defelem]]
[hiccup.util :as util]))
(def doctype
"Map of doctype strings."
{:html4
(util/raw-string "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01//EN\" "
"\"http://www.w3.org/TR/html4/strict.dtd\">\n")
:xhtml-strict
(util/raw-string "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" "
"\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n")
:xhtml-transitional
(util/raw-string "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" "
"\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n")
:html5
(util/raw-string "<!DOCTYPE html>\n")})
(defelem xhtml-tag
"Create an XHTML element for the specified language."
[lang & contents]
[:html {:xmlns "http://www.w3.org/1999/xhtml"
"xml:lang" lang
:lang lang}
contents])
(defn xml-declaration
"Create a standard XML declaration for the following encoding."
[encoding]
(util/raw-string "<?xml version=\"1.0\" encoding=\"" encoding "\"?>\n"))
(defmacro html4
"Create a HTML 4 document with the supplied contents. The first argument
may be an optional attribute map."
[& contents]
`(html {:mode :sgml}
(doctype :html4)
[:html ~@contents]))
(defmacro xhtml
"Create a XHTML 1.0 strict document with the supplied contents. The first
argument may be an optional attribute map. The following attributes are
treated specially:
`:lang`
: The language of the document
`:encoding`
: The character encoding of the document (defaults to UTF-8)."
[options & contents]
(if-not (map? options)
`(xhtml {} ~options ~@contents)
`(let [options# ~options]
(html {:mode :xml}
(xml-declaration (options# :encoding "UTF-8"))
(doctype :xhtml-strict)
(xhtml-tag (options# :lang) ~@contents)))))
(defmacro html5
"Create a HTML5 document with the supplied contents."
[options & contents]
(if-not (map? options)
`(html5 {} ~options ~@contents)
(if (options :xml?)
`(let [options# (dissoc ~options :xml?)]
(html {:mode :xml}
(xml-declaration (options# :encoding "UTF-8"))
(doctype :html5)
(xhtml-tag options# (options# :lang) ~@contents)))
`(let [options# (dissoc ~options :xml?)]
(html {:mode :html}
(doctype :html5)
[:html options# ~@contents])))))
(defn include-js
"Include a list of external javascript files."
[& scripts]
(for [script scripts]
[:script {:type "text/javascript", :src (util/to-uri script)}]))
(defn include-css
"Include a list of external stylesheet files."
[& styles]
(for [style styles]
[:link {:type "text/css", :href (util/to-uri style), :rel "stylesheet"}]))