-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtex.clj
77 lines (61 loc) · 2.61 KB
/
tex.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
(ns dntables.tex
(:require [dntables.util :refer [is-relative?]]
[clojure.java.io :as io]
[selmer.parser :as sp]
[selmer.util :refer [without-escaping]]
[selmer.filters :as f]
[clojure.string :as s]
[dntables.env :refer [env]]
[dntables.parsers.text :as p]))
(def escape-chars {\\ "\\\\" \{ "\\{" \} "\\}" \_ "\\_" \^ "\\^" \# "\\#" \& "\\&" \$ "\\$" \% "\\%" \~ "\\~"})
(defn latex-escape [t] (when t (s/escape t escape-chars)))
(f/add-filter! :latexescape latex-escape)
(f/add-filter! :dienormalize p/die-normalize)
(f/add-filter! :even even?)
(def tags {:tag-open \( :tag-close \)})
(defn ->tabularx [element]
(without-escaping
(sp/render-file (or (when-let [tt (get-in (env) [:tex :table])] (io/file tt)) (io/resource "tex/tpl-table.tex")) element tags)))
(defn ->document [elements]
(without-escaping
(sp/render-file (or (when-let [tt (get-in (env) [:tex :frame])] (io/file tt))
(io/resource "tex/tpl-memoir.tex"))
;; grab keys from the first table item, since the metadata is attached
;; to each one, and covers the entire set.
(merge
{:tables (map ->tabularx elements)}
(select-keys (first elements) p/metakeys))
tags)))
(defn ->tex [source output]
(let [troot (get-in (env) [:tex :templateroot])]
(if (and (get-in (env) [:tex :frame]) (get-in (env) [:tex :table]))
(let [root (or troot (-> (java.io.File. ".") .getCanonicalPath))]
(println "Setting template root to: " root)
(selmer.parser/set-resource-path! root))
(println "Using classpath (included) LaTeX templates."))
(spit output (-> (p/simple-reader source) ->document))))
(defn tex-template [source dest data]
(let [troot (get-in (env) [:tex :templateroot])
root (or troot (-> (java.io.File. ".") .getCanonicalPath))]
(println "Setting template root to: " root)
(selmer.parser/set-resource-path! root)
(spit dest
(without-escaping
(sp/render-file source data tags)))))
(comment
(->tex (io/file "inputexamples/rpgtalk/big.txt") "go.tex")
(->tex (io/file "inputexamples/goatmansgoblet/familyweapons.txt") "go.tex")
(->tex (io/file "inputexamples/dwdiscord/d6d.txt") "go.tex")
(as-> "goatmansgoblet/familyweapons.txt" $
(io/resource $)
(p/simple-reader $)
(last $)
(->document [$])
(println $))
(as-> "goatmansgoblet/familyweapons.txt" $
(io/resource $)
(p/simple-reader $)
(last $)
(->tabularx $)
(println $))
)