Hiccupでtableを出力する単純な関数を作ってみた 改善1
改善点:カラムタイプに応じたセルの生成
project.clj
(defproject mylang "0.1.0-SNAPSHOT" :description "mylang" :url "http://d.hatena.ne.jp/oknknic/" :dependencies [[org.clojure/clojure "1.5.1"] [hiccup "1.0.3"]] :main mylang.core)
core.clj
(ns mylang.core (:use [hiccup.core] [hiccup.page] [clojure.java.io :only (writer)])) (defn- table-header-detail [cols] (map #(vector :th (:name %)) cols)) (defn- table-header [cols] [:tr (table-header-detail cols)]) (defmulti table-record-detail-type {:private true} (fn [cell col & _] (:type col))) (defmethod table-record-detail-type :input [cell col & _] [:input (merge {:type (:input-type col) :value cell} (:options col))]) (defmethod table-record-detail-type :textarea [cell col & _] [:textarea cell]) (defmethod table-record-detail-type :text [cell col & _] cell) (defmethod table-record-detail-type :default [cell col & _] cell) (defn- table-record-detail [record cols] (map #(vector :td (table-record-detail-type %1 %2)) record cols)) (defn- table-record [record cols] [:tr (table-record-detail record cols)]) (defn- table-records [records cols] (map #(table-record % cols) records)) (defn table [cols records & options] [:table (table-header cols) (table-records records cols)]) ;実行サンプル (defn -main [] (with-open [w (writer "out.html" :encoding "UTF-8")] (.write w (html4 [:body (table [{:name "デフォルト"} {:name "テキスト" :type :text} {:name "テキストボックス" :type :input :input-type :text} {:name "テキストボックス(読取専用)" :type :input :input-type :text :options {:readonly "readonly"}} {:name "チェックボックス" :type :input :input-type :checkbox} {:name "テキストエリア" :type :textarea}] [["1-1" "1-2" "1-3" "1-4" "" "1-6"] ["2-1" "2-2" "2-3" "2-4" "" "2-6"]])]))))