Skip to content

Commit

Permalink
render returns a seq of lines now
Browse files Browse the repository at this point in the history
  • Loading branch information
joegallo committed Nov 12, 2011
1 parent b76677a commit 3885145
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 68 deletions.
51 changes: 35 additions & 16 deletions src/doric/core.clj
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -57,15 +57,11 @@
s s
(pad (Math/floor half-padding)))))) (pad (Math/floor half-padding))))))


(def ^{:dynamic true} th) (defn header [th cols]
(def ^{:dynamic true} td)
(def ^{:dynamic true} render)

(defn header [cols]
(for [col cols :when (:when col)] (for [col cols :when (:when col)]
(th col))) (th col)))


(defn body [cols rows] (defn body [td cols rows]
(for [row rows] (for [row rows]
(for [col cols :when (:when col)] (for [col cols :when (:when col)]
(td col row)))) (td col row))))
Expand Down Expand Up @@ -114,18 +110,41 @@
(def org 'doric.org) (def org 'doric.org)
(def raw 'doric.raw) (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] ([rows]
(table (vary-meta (keys (first rows)) (table* (vary-meta (keys (first rows))
merge (meta rows)) rows)) merge (meta rows)) rows))
([cols rows] ([cols rows]
(let [meta (meta cols) (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) cols (columns1 cols rows)
rows (format-rows cols rows) rows (format-rows cols rows)
cols (columns2 cols rows) cols (columns2 cols rows)]
format (or (:format meta) org)] (render (cons (header th cols) (body td cols rows))))))
(require format)
(binding [th (ns-resolve format 'th) (defn table
td (ns-resolve format 'td) {:arglists '[[rows]
render (ns-resolve format 'render)] [cols rows]]}
(render (cons (header cols) (body cols rows))))))) [& args]
(apply str (join "\n" (apply table* args))))
16 changes: 6 additions & 10 deletions src/doric/csv.clj
Original file line number Original file line Diff line number Diff line change
@@ -1,13 +1,11 @@
(ns doric.csv (ns doric.csv
(:refer-clojure :exclude [join]) (:refer-clojure :exclude [join])
(:use [clojure.string :only [join]] (:use [clojure.string :only [join]]
[doric.core :only [align-cell]])) [doric.core :only [unaligned-th unaligned-td]]))


(defn th [col] (def th unaligned-th)
(:title col))


(defn td [col row] (def td unaligned-td)
(row (:name col)))


(defn escape [s] (defn escape [s]
(let [s (.replaceAll s "\"" "\"\"")] (let [s (.replaceAll s "\"" "\"\"")]
Expand All @@ -16,8 +14,6 @@
s))) s)))


(defn render [table] (defn render [table]
(str (join "," (map escape (first table))) "\n" (cons (join "," (map escape (first table)))
(join "\n" (for [tr (rest table)]
(concat (join "," (map escape tr)))))
(for [tr (rest table)]
(join "," (map escape tr)))))))
20 changes: 10 additions & 10 deletions src/doric/html.clj
Original file line number Original file line Diff line number Diff line change
@@ -1,17 +1,17 @@
(ns doric.html (ns doric.html
(:refer-clojure :exclude [join]) (:refer-clojure :exclude [join])
(:use [clojure.string :only [join]] (:use [clojure.string :only [join]]
[doric.core :only [align-cell]])) [doric.core :only [unaligned-th unaligned-td]]))


(defn th [col] (def th unaligned-th)
(str "<th>" (:title col) "</th>"))


(defn td [col row] (def td unaligned-td)
(str "<td>" (row (:name col)) "</td>"))


(defn render [table] (defn render [table]
(str "<table>" (concat ["<table>"
(str "<tr>" (join (first table)) "</tr>") (str "<tr>" (join (for [c (first table)]
(join (for [tr (rest table)] (str "<th>" c "</th>"))) "</tr>")]
(str "<tr>" (join tr) "</tr>"))) (for [tr (rest table)]
"</table>")) (str "<tr>" (join (for [c tr]
(str "<td>" c "</td>"))) "</tr>"))
["</table>"]))
22 changes: 9 additions & 13 deletions src/doric/org.clj
Original file line number Original file line Diff line number Diff line change
@@ -1,25 +1,21 @@
(ns doric.org (ns doric.org
(:refer-clojure :exclude [join]) (:refer-clojure :exclude [join])
(:use [clojure.string :only [join]] (:use [clojure.string :only [join]]
[doric.core :only [align-cell]])) [doric.core :only [aligned-th aligned-td]]))


(defn th [col] (def th aligned-th)
(align-cell col (:title col) (:title-align col)))


(defn td [col row] (def td aligned-td)
(align-cell col (row (:name col)) (:align col)))


(defn render [table] (defn render [table]
(let [spacer (str "|-" (let [spacer (str "|-"
(join "-+-" (join "-+-"
(map #(apply str (repeat (.length %) "-")) (map #(apply str (repeat (.length %) "-"))
(first table))) (first table)))
"-|\n")] "-|")]
(apply str (concat [spacer
spacer (str "| " (join " | " (first table)) " |")
(str "| " (join " | " (first table)) " |\n") spacer]
spacer
(concat
(for [tr (rest table)] (for [tr (rest table)]
(str "| " (join " | " tr) " |\n")) (str "| " (join " | " tr) " |"))
[spacer])))) [spacer])))
15 changes: 6 additions & 9 deletions src/doric/raw.clj
Original file line number Original file line Diff line number Diff line change
@@ -1,16 +1,13 @@
(ns doric.raw (ns doric.raw
(:refer-clojure :exclude [join]) (:refer-clojure :exclude [join])
(:use [clojure.string :only [join]] (:use [clojure.string :only [join]]
[doric.core :only [align-cell]])) [doric.core :only [aligned-th aligned-td]]))


(defn th [col] (def th aligned-th)
(align-cell col (:title col) (:title-align col)))


(defn td [col row] (def td aligned-td)
(align-cell col (row (:name col)) (:align col)))


(defn render [table] (defn render [table]
(str (join " " (first table)) "\n" (cons (join " " (first table))
(join "\n" (for [tr (rest table)]
(for [tr (rest table)] (join " " tr))))
(join " " tr)))))
13 changes: 3 additions & 10 deletions test/doric/test/core.clj
Original file line number Original file line Diff line number Diff line change
@@ -1,15 +1,8 @@
(ns doric.test.core (ns doric.test.core
(:refer-clojure :exclude [format name when]) (:refer-clojure :exclude [format name when])
(:use [doric.core] :reload) (:use [doric.core]
(:use [clojure.test])) [clojure.test]

[doric.org :only [th td render]]))
(use-fixtures :once
(fn [f]
(require org)
(binding [th (ns-resolve org 'th)
td (ns-resolve org 'td)
render (ns-resolve org 'render)]
(f))))


(deftest test-title-case (deftest test-title-case
(is (= "Foo" (title-case "foo"))) (is (= "Foo" (title-case "foo")))
Expand Down

0 comments on commit 3885145

Please sign in to comment.