-
Notifications
You must be signed in to change notification settings - Fork 4
/
website.lisp
219 lines (177 loc) · 6.47 KB
/
website.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
(in-package :jeffrey.website)
;; ## Website module
;;
;; Server and initial dispatch table
(defvar *server*)
(defun local (rel-path)
(concatenate 'string *local-directory* rel-path))
(load-nodes-html)
(setq hunchentoot:*dispatch-table* ; dispatches folders, files, and pages
(list (hunchentoot:create-static-file-dispatcher-and-handler
"/logo.jpg" (local "www/logo.jpg"))
(hunchentoot:create-folder-dispatcher-and-handler
"/examples/preview/" (local "examples/preview/"))
(hunchentoot:create-folder-dispatcher-and-handler
"/examples/" (local "examples/"))
(hunchentoot:create-static-file-dispatcher-and-handler
"/random-header.js"
(local "www/random-header.js"))
(hunchentoot:create-static-file-dispatcher-and-handler
"/jeffrey.css" (local "www/jeffrey.css"))
(hunchentoot:create-prefix-dispatcher
"/diagram" 'diagram)
(hunchentoot:create-prefix-dispatcher
"/random-diagram" 'random-diagram)
(hunchentoot:create-prefix-dispatcher
"/selection-diagram" 'selection-diagram)
(hunchentoot:create-prefix-dispatcher
"/examples" 'examples)
(hunchentoot:create-folder-dispatcher-and-handler
"/math-snippets/" (local "math-snippets/"))
(hunchentoot:create-prefix-dispatcher
"/names-and-statements" 'names-and-statements)
(hunchentoot:create-prefix-dispatcher
"/selected-diagram" 'selected-diagram)
(hunchentoot:create-prefix-dispatcher
"/" 'generate-index-page)))
(defun start-website (&key (address "localhost") (port "8080"))
(setq *server* (hunchentoot:start
(make-instance 'easy-acceptor
:address address
:port port))))
;; strangely, the above does not work for localhost on my side...
;; but the below does transmit the website fine at
;; http://127.0.0.1:8080/
(defun start-website?? (&key (port 8080))
(setq *server* (hunchentoot:start
(make-instance 'easy-acceptor
:port port))))
(defun stop-website ()
(stop *server*))
(setq html-template:*default-template-pathname*
(concatenate 'string *local-directory* "www/"))
;; ## Index page
(defun generate-index-page ()
(with-output-to-string (stream)
(html-template:fill-and-print-template
#P"jeffrey.tmpl"
(list :title "Choiceless Grapher"
:index? T
:error? NIL
:bad-forms (format nil "~{~a~^, ~}" *bad-forms*))
:stream stream)))
;; ## Examples page
(defun examples ()
(with-output-to-string (stream)
(html-template:fill-and-print-template
#P"examples.tmpl" '()
:stream stream)))
;; ## names-and-statements page
(defun names-and-statements ()
(with-output-to-string (stream)
(html-template:fill-and-print-template
#P"forms-statements.tmpl" '()
:stream stream)))
;; ## Diagrams pages
;; ### Encoding filenames
;;
;; I represent a subset of '(0 1 ... 430) as a 431-long
;; sequence of 0s and 1s: position i will be 1 if the form with
;; HR. number i is in the subset. This sequence can be seen as an
;; integer in base 2. The encoding is this integer in base 36.
(defun names->bits (names)
(loop for i from 0 to 430
if (member i names)
sum (expt 2 i)))
(defun names->code (names)
(write-to-string (names->bits names) :base 36))
;; ### Getting the variables for the templates and the lock
(defun names->filename (names label-style)
(format nil "~a-~a" label-style (names->code names)))
(defun names->uri (names label-style)
(format nil "/~a.png" (names->filename names label-style)))
(defun names->rel-path (names label-style)
(format nil "diagrams~a" (names->uri names label-style)))
(defun names->temp (names label-style)
(format nil "diagrams/~a.temp"
(names->filename names label-style)))
(defun print-names (names &key kind)
(concatenate
'string
(case kind
("random"
(format nil
"the following (pseudo) randomly chosen forms: "))
("standard"
(format nil
"the forms with HR numbers: ")))
(format nil "~{~a~^, ~}" names)))
;; ## Graphing
;;
;; To take care of concurrency, I create a temp file when graphing,
;; whose existence `web-graph` has to check before attempting
;; to graph.
(defun create-temp (names label-style)
(with-open-file (out (local (names->temp names label-style))
:direction :output
:if-exists :error
:if-does-not-exist :create)
(format out "")))
(defun delete-temp (names label-style)
(delete-file (local (names->temp names label-style))))
(defun lock-graph (names label-style)
(create-temp names label-style)
(graph names (names->filename names label-style) label-style)
(delete-temp names label-style))
(defun file-exists-p (rel-path)
(probe-file (local rel-path)))
(defun web-graph (names label-style)
(unless (file-exists-p (names->rel-path names label-style))
(if #1=(file-exists-p (names->temp names label-style))
(loop while #1#
do (sleep 1))
(lock-graph names label-style))))
;; ### Filling the diagram templates and getting the pages online.
(defmacro get-graph (names label-style)
`(push
(hunchentoot:create-static-file-dispatcher-and-handler
(names->uri ,names ,label-style)
(local (names->rel-path ,names ,label-style)))
hunchentoot:*dispatch-table*))
(defmacro fill-template (names label-style bad-input &key kind)
`(with-output-to-string (stream)
(html-template:fill-and-print-template
#P"jeffrey.tmpl"
(list :title "Implication diagram"
:index? NIL
:error? NIL
:print-names (print-names ,names :kind ,kind)
:bad-input ,bad-input
:path-to-image (names->uri ,names ,label-style))
:stream stream)))
(defun diagram ()
(destructuring-bind (names bad-input label-style) (process-input)
(web-graph names label-style)
(get-graph names label-style)
(fill-template names label-style bad-input :kind "standard")))
(defun random-diagram ()
(let* ((n (parse-integer (parameter "n")))
(names (sort (random-HR-numbers n) #'<)))
(web-graph names "fancy")
(get-graph names "fancy")
(fill-template names "fancy" nil :kind "random")))
(defun selection-diagram ()
(let ((names (loop for name being the hash-keys of jeffrey.graph:*graph*
for string-name = (format nil "~a" name)
when (parameter string-name)
collect name)))
(web-graph names "fancy")
(get-graph names "fancy")
(fill-template names "fancy" nil :kind "standard")))
;;; debugging
(defun debug-mode-on ()
(setq *catch-errors-p* NIL)
(setq *show-lisp-errors-p* T))
(defun debug-mode-off ()
(setq *catch-errors-p* T)
(setq *show-lisp-errors-p* NIL))