Browse files

render returns a seq of lines now

  • Loading branch information...
1 parent b76677a commit 3885145d5f70ff5fb3d497498db6f151908b671c @joegallo committed Nov 12, 2011
Showing with 69 additions and 68 deletions.
  1. +35 −16 src/doric/core.clj
  2. +6 −10 src/doric/csv.clj
  3. +10 −10 src/doric/html.clj
  4. +9 −13 src/doric/org.clj
  5. +6 −9 src/doric/raw.clj
  6. +3 −10 test/doric/test/core.clj
View
51 src/doric/core.clj
@@ -57,15 +57,11 @@
s
(pad (Math/floor half-padding))))))
-(def ^{:dynamic true} th)
-(def ^{:dynamic true} td)
-(def ^{:dynamic true} render)
-
-(defn header [cols]
+(defn header [th cols]
(for [col cols :when (:when col)]
(th col)))
-(defn body [cols rows]
+(defn body [td cols rows]
(for [row rows]
(for [col cols :when (:when col)]
(td col row))))
@@ -114,18 +110,41 @@
(def org 'doric.org)
(def raw 'doric.raw)
-(defn table
+;; table format helpers
+;; aligned th and td are useful for whitespace sensitive formats, like
+;; raw and org
+(defn aligned-th [col]
+ (align-cell col (:title col) (:title-align col)))
+
+(defn aligned-td [col row]
+ (align-cell col (row (:name col)) (:align col)))
+
+;; unalighed-th and td are useful for whitespace immune formats, like
+;; csv and html
+(defn unaligned-th [col]
+ (:title col))
+
+(defn unaligned-td [col row]
+ (row (:name col)))
+
+(defn table*
([rows]
- (table (vary-meta (keys (first rows))
- merge (meta rows)) rows))
+ (table* (vary-meta (keys (first rows))
+ merge (meta rows)) rows))
([cols rows]
(let [meta (meta cols)
+ format (or (:format meta) org)
+ _ (require format)
+ th (ns-resolve format 'th)
+ td (ns-resolve format 'td)
+ render (ns-resolve format 'render)
cols (columns1 cols rows)
rows (format-rows cols rows)
- cols (columns2 cols rows)
- format (or (:format meta) org)]
- (require format)
- (binding [th (ns-resolve format 'th)
- td (ns-resolve format 'td)
- render (ns-resolve format 'render)]
- (render (cons (header cols) (body cols rows)))))))
+ cols (columns2 cols rows)]
+ (render (cons (header th cols) (body td cols rows))))))
+
+(defn table
+ {:arglists '[[rows]
+ [cols rows]]}
+ [& args]
+ (apply str (join "\n" (apply table* args))))
View
16 src/doric/csv.clj
@@ -1,13 +1,11 @@
(ns doric.csv
(:refer-clojure :exclude [join])
(:use [clojure.string :only [join]]
- [doric.core :only [align-cell]]))
+ [doric.core :only [unaligned-th unaligned-td]]))
-(defn th [col]
- (:title col))
+(def th unaligned-th)
-(defn td [col row]
- (row (:name col)))
+(def td unaligned-td)
(defn escape [s]
(let [s (.replaceAll s "\"" "\"\"")]
@@ -16,8 +14,6 @@
s)))
(defn render [table]
- (str (join "," (map escape (first table))) "\n"
- (join "\n"
- (concat
- (for [tr (rest table)]
- (join "," (map escape tr)))))))
+ (cons (join "," (map escape (first table)))
+ (for [tr (rest table)]
+ (join "," (map escape tr)))))
View
20 src/doric/html.clj
@@ -1,17 +1,17 @@
(ns doric.html
(:refer-clojure :exclude [join])
(:use [clojure.string :only [join]]
- [doric.core :only [align-cell]]))
+ [doric.core :only [unaligned-th unaligned-td]]))
-(defn th [col]
- (str "<th>" (:title col) "</th>"))
+(def th unaligned-th)
-(defn td [col row]
- (str "<td>" (row (:name col)) "</td>"))
+(def td unaligned-td)
(defn render [table]
- (str "<table>"
- (str "<tr>" (join (first table)) "</tr>")
- (join (for [tr (rest table)]
- (str "<tr>" (join tr) "</tr>")))
- "</table>"))
+ (concat ["<table>"
+ (str "<tr>" (join (for [c (first table)]
+ (str "<th>" c "</th>"))) "</tr>")]
+ (for [tr (rest table)]
+ (str "<tr>" (join (for [c tr]
+ (str "<td>" c "</td>"))) "</tr>"))
+ ["</table>"]))
View
22 src/doric/org.clj
@@ -1,25 +1,21 @@
(ns doric.org
(:refer-clojure :exclude [join])
(:use [clojure.string :only [join]]
- [doric.core :only [align-cell]]))
+ [doric.core :only [aligned-th aligned-td]]))
-(defn th [col]
- (align-cell col (:title col) (:title-align col)))
+(def th aligned-th)
-(defn td [col row]
- (align-cell col (row (:name col)) (:align col)))
+(def td aligned-td)
(defn render [table]
(let [spacer (str "|-"
(join "-+-"
(map #(apply str (repeat (.length %) "-"))
(first table)))
- "-|\n")]
- (apply str
- spacer
- (str "| " (join " | " (first table)) " |\n")
- spacer
- (concat
+ "-|")]
+ (concat [spacer
+ (str "| " (join " | " (first table)) " |")
+ spacer]
(for [tr (rest table)]
- (str "| " (join " | " tr) " |\n"))
- [spacer]))))
+ (str "| " (join " | " tr) " |"))
+ [spacer])))
View
15 src/doric/raw.clj
@@ -1,16 +1,13 @@
(ns doric.raw
(:refer-clojure :exclude [join])
(:use [clojure.string :only [join]]
- [doric.core :only [align-cell]]))
+ [doric.core :only [aligned-th aligned-td]]))
-(defn th [col]
- (align-cell col (:title col) (:title-align col)))
+(def th aligned-th)
-(defn td [col row]
- (align-cell col (row (:name col)) (:align col)))
+(def td aligned-td)
(defn render [table]
- (str (join " " (first table)) "\n"
- (join "\n"
- (for [tr (rest table)]
- (join " " tr)))))
+ (cons (join " " (first table))
+ (for [tr (rest table)]
+ (join " " tr))))
View
13 test/doric/test/core.clj
@@ -1,15 +1,8 @@
(ns doric.test.core
(:refer-clojure :exclude [format name when])
- (:use [doric.core] :reload)
- (:use [clojure.test]))
-
-(use-fixtures :once
- (fn [f]
- (require org)
- (binding [th (ns-resolve org 'th)
- td (ns-resolve org 'td)
- render (ns-resolve org 'render)]
- (f))))
+ (:use [doric.core]
+ [clojure.test]
+ [doric.org :only [th td render]]))
(deftest test-title-case
(is (= "Foo" (title-case "foo")))

0 comments on commit 3885145

Please sign in to comment.