-
Notifications
You must be signed in to change notification settings - Fork 4
/
latex-in-html.lisp
305 lines (261 loc) · 9.71 KB
/
latex-in-html.lisp
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
;;; # Package :jeffrey.latex-in-html
;;;
(in-package :jeffrey.latex-in-html)
;;;
;;; ## Description
;;;
;;; This package converts the LaTeX-formatted statements of the forms
;;; `(node-latex node)`
;;; into HTML renderable versions, and prints a template for the package
;;; `HTML_template`, with a table of these statements.
;;; It only takes care of the LaTeX-symbols which appear in the
;;; statements of the forms in the Consequences of the Axiom of Choice
;;; project.
;;; One can easily add more symbols by adding pairs or triplets to
;;; `*character-pairs*` or `*{}-triplets*`, respectively.
;;;
;;; For my purposes, there are three styles of things to be transformed
;;; to html from LaTeX. The first two are stored in `*character-pairs*`
;;; and the last in `*{}-triplets`:
;;;
;;; * math snippets (LaTeX-formatted text enclosed in $, will be displayed
;;; as a png file),
;;;
;;; * individual characters in standard text (e.g. "\\\"o" to "ö"), and
;;;
;;; * standard text in curly brackets with an identifier before or after
;;; the left curly bracket.
;;;
;;; ## Code
;;;
;;; ### Local storage
;;;
;;; A database to hold the html formatted statements:
(defvar *nodes-html* (make-hash-table))
(defun make-node-html (node-name statement) ;=> new entry in hash-table
(if (gethash node-name *nodes-html*)
(error "Node with name ~a has already been added, with statement: ~a~%"
node-name statement)
(setf (gethash node-name *nodes-html*)
statement)))
(defun update-node-html (node-name new-statement) ;=> updates hash-table
(if (gethash node-name *nodes-html*)
(setf (gethash node-name *nodes-html*)
new-statement)
(error "Node with name ~a not in the database.~%"
node-name)))
(defvar *database-filename*
(concatenate 'string
*local-directory*
"nodes-html.db"))
(defun save-nodes-html () ;=> saves database in file
(let ((list (loop for name being the hash-keys of *nodes-html*
using (hash-value statement)
collect (list name statement))))
(with-open-file (out *database-filename*
:direction :output
:if-exists :supersede)
(with-standard-io-syntax
(print list out)))))
(defun load-nodes-html () ;=> loads database from file
(let ((list '()))
(with-open-file (in *database-filename*)
(with-standard-io-syntax
(setf list (read in))))
(setf *nodes-html (make-hash-table))
(loop for (name statement) in list
do (setf (gethash name *nodes-html*) statement))))
(defun get-by-name (name)
(list :name name :statement (gethash name *nodes-html*)))
(defun select-by-content (search-words)
(loop for node-name being the hash-keys of *nodes-html*
using (hash-value statement)
when (some (lambda (word)
(search word statement))
search-words)
collect (list :name node-name :statement statement)))
;;; Temporary storage for the '(math-snippet identifier) pairs:
(defvar *math-snippets* nil
"Math-snippets are two-element lists, of one LaTeX-formatted
math string without the surrounding $s, and one unique number.")
;;; ### First transform parens outside of $...$ of the parameters
;;;
;;; The parameters all have their parens outside of $...$, which
;;; does not look good after processing.
(defun parameter-transform (statement)
(if (and #1=(search "$)" statement)
#2=(search "}" statement)
(< #1# #2#))
(let ((begin (subseq statement 0 #2#))
(rest (subseq statement #2#)))
(concatenate 'string
(jeffrey.process-strings:search-replace
"$)" ")}$" (jeffrey.process-strings:search-replace
"($" "$\\mathbf{(" begin))
rest))
statement))
(assert (equal "{bla 5333 $\\mathbf{(foo)}$.} blablablalblablabla"
(parameter-transform "{bla 5333 ($foo$).} blablablalblablabla")))
;;; ### Extract math-snippets from statements.
(defun get-save-identifier (snippet)
"Gets the identifier of a snippet, if one exists, otherwise
creates a new identifier, and pushes '(snippet identifier) to
*math-snippets*"
(let ((found? (member snippet *math-snippets*
:test #'equal :key #'first)))
(if found?
(second (first found?))
(let ((n (length *math-snippets*)))
(push (list snippet n) *math-snippets*)
n))))
(defun transform-math (statement start end snip-number)
(concatenate
'string
(subseq statement 0 start)
(format nil "<img src=\"math-snippets/snip-~a.png\">"
snip-number)
(find-save-transform-math (subseq statement (+ 1 end)))))
(defun find-save-transform-math (statement) ;=> html-text
(let ((start (position #\$ statement)))
(if start
(let* ((snip-length (position #\$
#1=(subseq statement
(+ start 1))))
(snip (string-trim maxpc.char:*whitespace*
(subseq #1# 0 snip-length)))
(number (get-save-identifier snip)))
(transform-math statement
start
(+ start snip-length 1)
number))
statement)))
(defun find-save-transform-maths () ;=> updates *nodes-html*
(loop for node being the hash-values of *graph*
using (hash-key name)
for statement = (parameter-transform (node-LaTeX node))
do (print name)
do (print statement)
do (make-node-html name (find-save-transform-math statement))))
;;; ### Create a .tex and a .png file for each unique snippet
(defun snippet->tex (stream snip)
(format
stream "
\\PassOptionsToPackage{usenames,dvipsnames}{xcolor}
\\documentclass[tikz,xcolor]{standalone}
\\usepackage{amsmath,amssymb,color,xcolor}
\\newcommand{\\Cal}{\\mathcal}
\\begin{document}
\\begin{tikzpicture}
\\node[minimum size = 16pt] at (0,0) {$~a$};
\\end{tikzpicture}
\\end{document}"
snip))
(defun snippet-tex-file (number)
(concatenate 'string
jeffrey.main:*local-directory*
(format nil "math-snippets/snip-~a.tex" number)))
(defun make-snippet-texs ()
(loop for (snip number) in *math-snippets*
for filename = (snippet-tex-file number)
do (with-open-file (out filename
:direction :output
:if-exists :supersede)
(snippet->tex out snip))))
;;; To create the .png files I wrote the bash script make-math-snippets.sh
(defun make-snippets ()
(print "Finding snippets in statements")
(find-save-transform-maths)
(print "Making the tex files")
(make-snippet-texs)
(print "Running the bash script make-math-snippets.sh")
(external-program:run "/bin/bash" '("./make-math-snippets.sh")
:error T :output T))
;;; ### Adjust the standard text of each statement
;;;
;;; Set the translations of individual characters:
(defvar *character-pairs* nil
"Translation pairs of the form '(latex html)")
(setf *character-pairs*
(list (list "\\\"o" "ö")
(list "\\\"a" "ä")
(list "\\'e" "é")
(list "\\L " "Ł")
(list "\\'s" "ś")
(list "\\newline" "")))
;;; Set the translations of standard text in curly brackets:
(defvar *{}-triplets*
"Translation triplets of the form '(latex-start html-start html-end)")
(setf *{}-triplets*
(list (list "{HR" "<strong>" "</strong>")
(list "{\\it" "<i>" "</i>")
(list "\\ac{" "see <font class=\"author\">" "</font>")
(list "\\cite{" "<font class=\"year\">" "</font>")
(list "\\itemitem{" "<font class=\"enum\">" "</font>")
(list "\\icopy{" "<font class=\"icopy\">" "</font>")
(list "\\hbox{" "" "")))
;;; Use the translations to adjust the statements for printing in html:
(defun translate-character (statement translation-pair)
(destructuring-bind (this that) translation-pair
(jeffrey.process-strings:search-replace this that statement)))
(defun translate-{} (statement translation-triplet)
(destructuring-bind (latex-start html-start html-end) translation-triplet
(let ((start (search latex-start statement)))
(if start
(let ((end (position #\} (subseq statement start))))
(format t "start: ~a, end: ~a~%" start end)
(concatenate 'string
(subseq statement 0 start)
html-start
(subseq statement
(+ start (length latex-start))
(+ start end))
html-end
(subseq statement (+ start end 1))))
statement))))
(defun adjust-characters (statement)
(reduce #'translate-character *character-pairs*
:initial-value statement))
(defun adjust-{} (statement)
(reduce #'translate-{} *{}-triplets*
:initial-value statement))
(defun adjust-statement (statement)
(adjust-characters (adjust-{} statement)))
(defun adjust-statements ()
(loop for name being the hash-keys of *nodes-html*
using (hash-value statement)
do (update-node-html name (adjust-statement statement))))
;;; ### Create a template with the list of forms and their statements
(defvar *template-file* (concatenate 'string
*local-directory*
"www/forms-statements.tmpl"))
(defun create-html-template ()
(let ((checkforms (loop for i from 0 to 430
unless (member i *bad-forms*)
collect (get-by-name i)))
(*string-modifier* #'identity))
(with-open-file (out *template-file*
:direction :output
:if-exists :supersede)
(format out
"~a~%"
(with-output-to-string (stream)
(html-template:fill-and-print-template
#P"names-and-statements.tmpl"
(list :checkforms checkforms)
:stream stream))))))
;;; ### The whole process:
(defun make-files-and-database ()
(print "Reseting variables...")
(setf *math-snippets* nil)
(setf *nodes-html* (make-hash-table))
(print "Making snippets...")
(make-snippets)
(print "Adjusting statements...")
(adjust-statements)
(print "Saving html statements of nodes...")
(save-nodes-html)
(print "Creating HTML_Template template...")
(create-html-template))
;;; After evaluating (make-files-and-database), the function
;;; (load-nodes-html) can be used from website.lisp to
;;; populate the database.