Skip to content

Instantly share code, notes, and snippets.

@michaelbarton
Created March 9, 2012 03:18
Show Gist options
  • Save michaelbarton/2004840 to your computer and use it in GitHub Desktop.
Save michaelbarton/2004840 to your computer and use it in GitHub Desktop.
#!/usr/bin/env clj
(ns format
(:require [clojure.string :as str1])
(:require [clojure.contrib.string :as str2]))
(def csv
(map #(str2/split #"," %)
(str2/split-lines
(slurp (first *command-line-args*)))))
(defn escape [s]
(str1/replace s #"(#|%)" "\\\\$1"))
(defn format-line [line]
(str
(str2/join " & " (map #(format "%1$6s" (escape %)) line))
" \\\\"))
(def header (first csv))
(def body (rest csv))
(println "\\documentclass{article}")
(println "\\usepackage{booktabs}")
(println "\\usepackage[hmargin=2cm,vmargin=2cm,landscape]{geometry}")
(println "\\begin{document}")
(println (str "\\begin{tabular}{" (str2/join " " (take (count header) (repeat "r"))) "}"))
(println "\\toprule")
(println (format-line header))
(println "\\midrule")
(dorun (map #(println (format-line %)) body))
(println "\\toprule")
(println "\\end{tabular}")
(println "\\end{document}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment