Mired in code
Check-in [9671e5df1b]
Not logged in
Public Repositories
mwm's Repositories

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Initial version of the bitchin framework from the blog.
Timelines: family | descendants | trunk
Files: files | file ages | folders
SHA1:9671e5df1bb352cd73f08d26a4d664c288556bae
User & Date: mwm@mired.org 2010-02-13 23:13:49
Context
2010-02-13
23:15
Initial version of the zfs backup script. check-in: d98a5c8151 user: mwm@mired.org tags: trunk
23:13
Initial version of the bitchin framework from the blog. check-in: 9671e5df1b user: mwm@mired.org tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace

Added bitchin/html.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
;;
;; bitchin html - html templating for bitchin.
;;

(ns org.mired.bitchin.html
  (:use [clojure.contrib.str-utils :only (str-join re-split)]))

(defmacro make-tags
  ([sep name & names]
     `(do
	(make-tags ~sep ~name)
	(make-tags ~sep ~@names)))
  ([sep name]
     (let [name# (first (re-split #"_" (str name)))]
       `(defn ~name
	  ([] (format "<%s />"  ~name#))
	  ([attribs# & args#]
	     (if (associative? attribs#)
	       (str "<" ~name# (make-attrib-strings attribs#)
		    (if (nil? args#)
		      " />"
		      (format ">%s%s%s</%s>"
			      ~sep (str-join ~sep args#) ~sep ~name#)))
	       (apply ~name (concat [{} attribs#] args#))))))))

(defn- make-attrib-strings [attrs]
  (apply str (map #(format " %s=%s" (if (keyword? %) (name %) %)
			   (quoted-attribute (attrs %)))
		  (keys attr)))
			   

(defn quoted-attribute [input]
  (format "\"%s\"" 
	  (apply str (replace {\& "&amp;", \" "&quot;", \> "&gt;"} (str input)))))

;; The standard html tags with newlines between element in the contents
(make-tags "\n" html head meta_ base link script style body div dl ol ul li table 
	   tr colgroup col thead tbody tfoot form select input)

;; And now the same, with nothing between elements in the contents.
(make-tags "" title h1 h2 h3 h4 h5 h6 p pre address blockquote hr dd dt th td
	   caption option label textarea button a b i big small strong em q
	   tt a cite dfn abbr acronym code samp kbd var sub del ins font span img
	   br map_ area object param embed noembed)

Added bitchin/test.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
;;
;; bitchin - the GUI toolkit that uses the web as a back end.
;;

;; Unit tests

(ns org.mired.bitchin.test
  [:use [clojure.test] :only (is)]
  [:use org.mired.bitchin.html])

(deftest inline-elements
  (is (= "<p />" (p)) "empty contents")
  (is (= "<map />" (map_)) "keyword tag")
  (is (= "<p>now</p>" (p "now")) "string contents")
  (is (= "<p>now</p>" (p 'now)) "symbol contents")
  (is (= "<p>:now</p>" (p :now)) "keyword contents")
  (is (= "<p><br /></p>" (p (br))) "element content")
  (is (= "<p>now &amp;</p>" (p "now &")) "ampersand encoding")
  (is (= "<p>now &lt;</p>" (p "now <")) "less than encoding")
  (is (= "<p>now is the</p>" (p "now " 'is " the")) "multipart contents"))

(deftest block-elements
  (is (= "<div />" (div)) "empty contents")
  (is (= "<meta />" (meta_)) "keyword tag")
  (is (= "<div>\nthen\n</div>" (div "then")) "string contents")
  (is (= "<div>\nthen\n</div>" (div 'then)) "symbol contents")
  (is (= "<div>\n:then\n</div>" (div :then)) "keyword contents")
  (is (= "<div>\n<br />\n</div>" (div (br))) "element contents")
  (is (= "<p>now &amp;</p>" (p "now &")) "ampersand encoding")
  (is (= "<p>now &lt;</p>" (p "now <")) "less than encoding")
  (is (= "<div>\nthen\nwas\nthe\n</div>" (div "then" 'was "the"))
      "multipart contents"))

(deftest mixed-content
  (is (= "<div>\n<span>now is the</span>\n<b>time</b>\n</div>"
	 (div (span "now is the") (b 'time)))))

(deftest attributes-block
  (is (= "<div border=\"0\" />" (div {:border 0})) "keyword name")
  (is (= "<div border=\"0\" />" (div {'border 0})) "symbol name")
  (is (= "<div border=\"0\" />" (div {"border" 0})) "string name")
  (is (= "<div width=\"100%\" />" (div {:width "100%"})) "string value")
  (is (= "<div border=\"0\" width=\"100%\" />" (div {:border 0, 'width "100%"}))
      "multiple attributes")
  (is (= "<div border=\"0\" width=\"100%\">\n<span width=\"90%\">Text</span>\n</div>"
	 (div {"border" 0, :width "100%"} (span {'width "90%"} "Text")))
      "multiple attributes with content"))

(deftest attributes-inline
  (is (= "<span border=\"0\" />" (span {:border 0})) "keyword name")
  (is (= "<span border=\"0\" />" (span {'border 0})) "symbol name")
  (is (= "<span border=\"0\" />" (span {"border" 0})) "string name")
  (is (= "<span width=\"100%\" />" (span {:width "100%"})) "string value")
  (is (= "<span border=\"0\" width=\"100%\" />" (span {:border 0, 'width "100%"}))
      "multiple attributes")
  (is (= "<span class=\"now&amp;then\" />" (span {:class "now&then"})) 
      "ampersand encoding")
  (is (= "<span class=\"&quot;FOO&quot;\" />" (span {:class "\"FOO\""}))
	 "quote encoding")
  (is (= "<span class=\"x&gt;3\" />" (span {:class "x>3"}))
      "greater than encoding")
  (is (= "<span border=\"0\" width=\"100%\"><b width=\"90%\">Text</b></span>"
	 (span {"border" 0, :width "100%"} (b {'width "90%"} "Text")))
      "multiple attributes with content"))

(run-tests 'org.mired.bitchin.test)