forked from joegallo/doric
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Major refactor: switch to protocol-based render
- Loading branch information
Joshua Davey
committed
Mar 17, 2017
1 parent
ac02767
commit 290b2e7
Showing
10 changed files
with
127 additions
and
105 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,20 @@ | ||
(ns doric.csv | ||
(:require [clojure.string :as str] | ||
[doric.protocols :refer [tabular-renderer]] | ||
[doric.formatting :refer [unaligned-th unaligned-td]])) | ||
|
||
(def th unaligned-th) | ||
|
||
(def td unaligned-td) | ||
|
||
(defn escape [s] | ||
(let [s (.replaceAll (str s) "\"" "\"\"")] | ||
(if (re-find #"[,\n\"]" s) | ||
(str "\"" s "\"") | ||
s))) | ||
|
||
(defn render [table] | ||
(cons (str/join "," (map escape (first table))) | ||
(for [tr (rest table)] | ||
(str/join "," (map escape tr))))) | ||
(defn assemble [rows] | ||
(cons (str/join "," (first rows)) | ||
(for [tr (rest rows)] | ||
(str/join "," tr)))) | ||
|
||
(def renderer (tabular-renderer {:th (comp escape unaligned-th) | ||
:td (comp escape unaligned-td) | ||
:assemble assemble})) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,17 @@ | ||
(ns doric.html | ||
(:use [clojure.string :as str] | ||
[doric.formatting :refer [unaligned-th unaligned-td]])) | ||
(:require [clojure.string :as str] | ||
[doric.protocols :refer [tabular-renderer]] | ||
[doric.formatting :refer [unaligned-th unaligned-td]])) | ||
|
||
(def th unaligned-th) | ||
|
||
(def td unaligned-td) | ||
|
||
(defn render [table] | ||
(defn assemble [rows] | ||
(concat ["<table>" | ||
(str "<tr>" (str/join (for [c (first table)] | ||
(str "<tr>" (str/join (for [c (first rows)] | ||
(str "<th>" c "</th>"))) "</tr>")] | ||
(for [tr (rest table)] | ||
(for [tr (rest rows)] | ||
(str "<tr>" (str/join (for [c tr] | ||
(str "<td>" c "</td>"))) "</tr>")) | ||
["</table>"])) | ||
|
||
(def renderer (tabular-renderer {:th unaligned-th | ||
:td unaligned-td | ||
:assemble assemble})) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,21 @@ | ||
(ns doric.org | ||
(:require [clojure.string :as str] | ||
[doric.protocols :refer [tabular-renderer]] | ||
[doric.formatting :refer [aligned-th aligned-td]])) | ||
|
||
(def th aligned-th) | ||
|
||
(def td aligned-td) | ||
|
||
(defn render [table] | ||
(defn assemble [rows] | ||
(let [spacer (str "|-" | ||
(str/join "-+-" | ||
(map #(apply str (repeat (.length %) "-")) | ||
(first table))) | ||
(map #(apply str (repeat (.length %) "-")) | ||
(first rows))) | ||
"-|")] | ||
(concat [spacer | ||
(str "| " (str/join " | " (first table)) " |") | ||
(str "| " (str/join " | " (first rows)) " |") | ||
spacer] | ||
(for [tr (rest table)] | ||
(for [tr (rest rows)] | ||
(str "| " (str/join " | " tr) " |")) | ||
[spacer]))) | ||
|
||
(def renderer (tabular-renderer {:th aligned-th | ||
:td aligned-td | ||
:assemble assemble})) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
(ns doric.protocols | ||
(:require [clojure.string :as str])) | ||
|
||
(defprotocol Render | ||
(-render-lazy [_ cols data]) | ||
(-render [_ cols data])) | ||
|
||
(defrecord TabularRender [th td assemble] | ||
Render | ||
(-render [this cols data] | ||
(str/join "\n" | ||
(-render-lazy this cols data))) | ||
(-render-lazy [_ cols data] | ||
(assemble | ||
(cons (for [col cols | ||
:when (:when col)] | ||
(th col (:title col))) | ||
(for [row data] | ||
(for [col cols | ||
:when (:when col)] | ||
(td col (get row (:name col))))))))) | ||
|
||
(defn render-lazy [renderer cols data] | ||
(-render-lazy renderer cols data)) | ||
|
||
(defn render [renderer cols data] | ||
(-render renderer cols data)) | ||
|
||
(defn tabular-renderer [{:keys [td th assemble] :as fns}] | ||
(map->TabularRender fns)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,13 @@ | ||
(ns doric.raw | ||
(:require [clojure.string :as str] | ||
[doric.protocols :refer [tabular-renderer]] | ||
[doric.formatting :refer [aligned-th aligned-td]])) | ||
|
||
(def th aligned-th) | ||
|
||
(def td aligned-td) | ||
|
||
(defn render [table] | ||
(cons (str/join " " (first table)) | ||
(for [tr (rest table)] | ||
(defn assemble [rows] | ||
(cons (str/join " " (first rows)) | ||
(for [tr (rest rows)] | ||
(str/join " " tr)))) | ||
|
||
(def renderer (tabular-renderer {:th aligned-th | ||
:td aligned-td | ||
:assemble assemble})) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.