Permalink
Browse files

made formats pluggable, including new csv, html, and raw implementations

  • Loading branch information...
1 parent 57e0c44 commit 98cb90c263019472497871012ce28a130a2c0667 @joegallo committed Nov 11, 2011
Showing with 99 additions and 23 deletions.
  1. +18 −23 src/doric/core.clj
  2. +23 −0 src/doric/csv.clj
  3. +17 −0 src/doric/html.clj
  4. +25 −0 src/doric/org.clj
  5. +16 −0 src/doric/raw.clj
View
@@ -55,11 +55,9 @@
s
(pad (Math/floor half-padding))))))
-(defn th [col]
- (align-cell col (:title col) (:title-align col)))
-
-(defn td [col row]
- (align-cell col (row (:name col)) (:align col)))
+(def th)
+(def td)
+(def render)
(defn header [cols]
(for [col cols :when (:when col)]
@@ -104,26 +102,23 @@
(merge col
(column2 col (col-data col rows)))))
-(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
- (for [tr (rest table)]
- (str "| " (join " | " tr) " |\n"))
- [spacer]))))
+(def csv 'doric.csv)
+(def html 'doric.html)
+(def org 'doric.org)
+(def raw 'doric.raw)
@dwwoelfel
dwwoelfel Sep 2, 2013

You might want to consider a hash-map with keyword options here, e.g. (def formats {:csv 'doric.csv, :html 'doric.html, ...}).

It's much easier to pass options through multiple functions that way. Then the calling function can pass a keyword instead of having to know the doric namespaced formats.

(defn table
([rows]
- (table (keys (first rows)) rows))
+ (table (vary-meta (keys (first rows))
+ merge (meta rows)) rows))
([cols rows]
- (let [cols (columns1 cols rows)
+ (let [meta (meta cols)
+ cols (columns1 cols rows)
rows (format-rows cols rows)
- cols (columns2 cols rows)]
- (render (cons (header cols) (body 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)))))))
View
@@ -0,0 +1,23 @@
+(ns doric.csv
+ (:refer-clojure :exclude [join])
+ (:use [clojure.string :only [join]]
+ [doric.core :only [align-cell]]))
+
+(defn th [col]
+ (:title col))
+
+(defn td [col row]
+ (row (:name col)))
+
+(defn escape [s]
+ (let [s (.replaceAll s "\"" "\"\"")]
+ (if (re-find #"[,\n\"]" s)
+ (str "\"" s "\"")
+ s)))
+
+(defn render [table]
+ (str (join "," (map escape (first table))) "\n"
+ (join "\n"
+ (concat
+ (for [tr (rest table)]
+ (join "," (map escape tr)))))))
View
@@ -0,0 +1,17 @@
+(ns doric.html
+ (:refer-clojure :exclude [join])
+ (:use [clojure.string :only [join]]
+ [doric.core :only [align-cell]]))
+
+(defn th [col]
+ (str "<th>" (:title col) "</th>"))
+
+(defn td [col row]
+ (str "<td>" (row (:name col)) "</td>"))
+
+(defn render [table]
+ (str "<table>"
+ (str "<tr>" (join (first table)) "</tr>")
+ (join (for [tr (rest table)]
+ (str "<tr>" (join tr) "</tr>")))
+ "</table>"))
View
@@ -0,0 +1,25 @@
+(ns doric.org
+ (:refer-clojure :exclude [join])
+ (:use [clojure.string :only [join]]
+ [doric.core :only [align-cell]]))
+
+(defn th [col]
+ (align-cell col (:title col) (:title-align col)))
+
+(defn td [col row]
+ (align-cell col (row (:name col)) (:align col)))
+
+(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
+ (for [tr (rest table)]
+ (str "| " (join " | " tr) " |\n"))
+ [spacer]))))
View
@@ -0,0 +1,16 @@
+(ns doric.raw
+ (:refer-clojure :exclude [join])
+ (:use [clojure.string :only [join]]
+ [doric.core :only [align-cell]]))
+
+(defn th [col]
+ (align-cell col (:title col) (:title-align col)))
+
+(defn td [col row]
+ (align-cell col (row (:name col)) (:align col)))
+
+(defn render [table]
+ (str (join " " (first table)) "\n"
+ (join "\n"
+ (for [tr (rest table)]
+ (join " " tr)))))

0 comments on commit 98cb90c

Please sign in to comment.