-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemplate-designer.lisp
463 lines (426 loc) · 21.5 KB
/
template-designer.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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
(defpackage :template-designer
(:use :cl :cl-who :arrows)
(:export #:start
#:stop
#:open-browser
#:*project-name*
#:*project-directory*
#:*templates-directory*
#:*config-directory*
#:*assets-directory*
#:*template-files-pattern*
#:project-directory
#:templates-directory
#:config-directory
#:assets-directory))
(in-package :template-designer)
(defvar *project-name*)
(defparameter *project-directory* nil
"The root directory of the project.
If not specified, then *default-pathname-defaults*/<project-name>/ is used.")
(defparameter *templates-directory* nil
"The directory where the template files are stored.
If not specified, then <project-directory>/templates/ is used as directory.")
(defparameter *config-directory* nil
"The directory where template configuration is stored.
Template configuration has arguments and data urls for the templates.
If not specified, then <project-directory>/templates-config/ is used.")
(defparameter *assets-directory* nil
"The directory where web assets (like images) are stored.
If not specified, then <project-directory>/assets/ is used.")
(defparameter *template-files-pattern* uiop/pathname:*wild-file-for-directory*
"Pattern for listing the template files from the templates directory.
By default, all files are listed.
Example value: *.html")
(defparameter *templates* (make-hash-table :test 'equalp))
(defun project-directory ()
(ensure-directories-exist
(or *project-directory*
(merge-pathnames (uiop/pathname:ensure-directory-pathname *project-name*)
*default-pathname-defaults*))))
(defun assets-directory ()
(ensure-directories-exist
(or *assets-directory*
(merge-pathnames "assets/" (project-directory)))))
(defun templates-directory ()
(ensure-directories-exist
(cond
((and *templates-directory* (uiop/pathname:absolute-pathname-p *templates-directory*))
*templates-directory*)
((and *templates-directory* (uiop/pathname:relative-pathname-p *templates-directory*))
(merge-pathnames *templates-directory* (project-directory)))
(t
(merge-pathnames #p"templates/" (project-directory))))))
(defun config-directory ()
(ensure-directories-exist
(cond
((and *config-directory* (uiop/pathname:absolute-pathname-p *config-directory*))
*config-directory*)
((and *config-directory* (uiop/pathname:relative-pathname-p *config-directory*))
(merge-pathnames *config-directory* (project-directory)))
(t
(merge-pathnames #p"templates-config/" (project-directory))))))
(defclass template ()
((id :initarg :id
:accessor template-id
:type string)
(filename :initarg :filename
:accessor template-filename
:type string)
(rendering-engine :initarg :rendering-engine
:initform :djula)
(data-url :initarg :data-url
:initform nil
:type (or null string)
:accessor template-data-url)
(arguments :initarg :arguments
:initform nil
:accessor template-arguments)))
(defun template-source (template)
(alexandria:read-file-into-string (merge-pathnames (template-filename template) (templates-directory))))
(defun load-template (template)
(let ((config-file (merge-pathnames (template-filename template) (config-directory))))
(when (uiop:file-exists-p config-file)
(let ((config (read-from-string (alexandria:read-file-into-string config-file))))
(setf (template-data-url template) (cdr (assoc "data-url" config :test #'string=)))
;; if template data url is present, then don't load arguments
(when (str:blankp (template-data-url template))
(setf (template-arguments template) (cdr (assoc "arguments" config :test #'string=)))))))
template)
(defun list-template-files ()
(uiop/filesystem:directory-files (templates-directory)
*template-files-pattern*))
(defun register-template (template)
(setf (gethash (template-id template) *templates*) template)
template)
(defun make-template-id (filepath)
(base64:string-to-base64-string
(map 'string #'code-char (sha1:sha1-digest (princ-to-string filepath)))
:uri t))
(defun load-templates ()
(mapcar (lambda (filepath)
(-> (make-instance 'template
:id (make-template-id filepath)
:filename (file-namestring filepath))
(load-template)
(register-template)))
(list-template-files)))
(defun find-template (id)
(find-if (lambda (template) (string= (template-id template) id))
(load-templates)))
(defun condition-message (condition)
"Get the descriptive message of CONDITION."
(with-output-to-string (s)
(write condition :escape nil :stream s)))
(defparameter +template-designer.js+ (asdf:system-relative-pathname :template-designer "template-designer.js"))
(defun with-site-html (stream body)
(with-html-output (stream)
(write-string "<!doctype html>" stream)
(:html
(:head
(:title "Template designer")
(:link :rel "stylesheet" :href "https://cdn.jsdelivr.net/npm/bulma@1.0.0/css/bulma.min.css")
(:meta :name "viewport" :content "width=device-width, initial-scale=1"))
(:body
(:script :src "https://code.jquery.com/jquery-3.7.1.js"
:integrity"sha256-eKhayi8LEQwp4NKxN+CfCh+3qOVUtJn3QNZ0TciWLP4="
:crossorigin "anonymous")
(:script :src "https://cdnjs.cloudflare.com/ajax/libs/ace/1.34.2/ace.js"
:integrity "sha512-WdJDvPkK4mLIW1kpkWRd7dFtAF6Z0xnfD3XbfrNsK2/f36vMNGt/44iqYQuliJZwCFw32CrxDRh2hpM2TJS1Ew=="
:crossorigin "anonymous" :referrerpolicy "no-referrer")
(:script :src "https://unpkg.com/htmx.org@1.9.12")
(funcall body)))))
(defun render-navigation-bar (html)
(with-html-output (html)
(:nav :class "navbar is-dark"
(:div :class "navbar-menu"
(:div :class "navbar-start"
(:a :class "navbar-item"
:href "/"
(str "Templates"))
(:a :class "navbar-item"
:href "/settings"
(str "Settings"))
(:a :class "navbar-item"
:href "/help"
(str "Help")))))))
(defun render-main-page (destination &optional template)
(uiop:with-output (html destination)
(with-site-html html
(lambda ()
(with-html-output (html)
(render-navigation-bar html)
;; templates form
(:form :action "template" :method :post
(:div :class "fixed-grid has-4-cols"
(:div :class "grid"
(:div :class "cell"
(:section :class "section"
(:div :class "container"
(:h1 (str "Templates"))
(:select :size 5 :style "width: 100%;"
:onchange
(ps:ps-inline (redirect-to-template this))
(dolist (tmpl (load-templates))
(htm (:option :value (template-id tmpl)
:selected (and template (string= (template-id tmpl) (template-id template)))
(str (template-filename tmpl))
))))
(render-template-form template html)
)))
(:div :class "cell is-col-span-3"
(:section :class "section"
(:div :class "container"
(:h1 (str "Template source"))
(:textarea :id "editor"
:class "ace"
:name "source"
:style (cl-css:inline-css '(:width "100%" :height "400px"))
:rows 50
:width "100%"
:height "105px"
(str (or (and template (template-source template)) "<html></html>")))))))
(when template
(htm (:div :class "cell is-col-span-4"
(:h1 (str "Rendered template")
(:a :class "button is-small" :style "margin-left:10px;"
:href (format nil "/render?name=~a&id=~a" (template-filename template) (template-id template))
:target "_blank" (str "Open in new tab")))
(:iframe :width "100%" :style "border: 1px solid gray; width 100vw; height:100vh"
:src (format nil "/render?name=~a&id=~a" (template-filename template) (template-id template))))))))
(:script :type "text/javascript"
(str (alexandria:read-file-into-string +template-designer.js+)))
(:script :type "text/javascript"
(str
(ps:ps
(defun redirect-to-template (elem)
(let* ((selected-index (ps:getprop elem 'selected-index))
(selected-option (aref (ps:getprop elem 'options) selected-index)))
(setf
(ps:chain window location href)
(concatenate
'string
"/?"
(ps:chain
(ps:new (-U-R-L-Search-Params (ps:create "id" (ps:getprop selected-option 'value)
"name" (ps:getprop selected-option 'text))))
(to-string)))))))))
)))))
(hunchentoot:define-easy-handler (main-handler :uri "/")
(id)
(render-main-page nil (find-template id)))
(defun render-template-form (template out)
(with-html-output (out)
(when template
(htm
(:input :type "hidden"
:name "id"
:value (template-id template))))
(:div :class "field is-small"
(:label :class "label is-small" (str "Filename"))
(:div :class "control"
(:input :name "filename"
:class "input is-small"
:type "text"
:placeholder "mytemplate.html"
:value (when template (template-filename template)))))
(:div :class "field is-small"
(:label :class "label is-small" (str "Data url"))
(:div :class "control"
(:input :name "data-url"
:class "input is-small"
:type "text"
:placeholder "http://site/data.json"
:value (when template (template-data-url template)))))
(:div :class "field is-small"
(:label :class "label is-small" (str "Arguments"))
(:div :class "control"
(:textarea :name "arguments"
:class "textarea"
:placeholder "{\"arg1\": \"foo\", \"arg2\" : \"bar\"}"
:rows 5
:style (cl-css:inline-css '(:width "100%"))
;; when arguments specified...
(when (and template
(or (not (str:blankp (template-arguments template)))
(not (str:blankp (template-data-url template)))))
(if (not (str:blankp (template-arguments template)))
(str (template-arguments template))
(str
(prin1-to-string
(handler-case
(read-template-arguments template)
(error (e)
(format nil "Error reading template arguments: ~a" (condition-message e)))))))))))
(:div :class "field is-grouped"
(:div :class "control"
(:button :class "button is-primary is-small"
:type "submit"
:name "save"
(str "Save")))
(:div :class "control"
(:button :class "button is-warning is-small"
:type "submit"
:name "reload"
(str "Reload")))
(:div :class "control"
(:button :class "button is-danger is-small"
:type "submit"
:name "delete"
(str "Delete"))))
))
(hunchentoot:define-easy-handler (template-handler :uri "/template")
()
(cond
((hunchentoot:post-parameter "save")
(when (str:blankp (hunchentoot:post-parameter "filename"))
(hunchentoot:redirect "/"))
;; Save the template file
(let ((filepath (merge-pathnames (file-namestring (hunchentoot:post-parameter "filename")) (templates-directory))))
(with-open-file (f filepath :direction :output
:if-does-not-exist :create
:if-exists :supersede)
(write-string (hunchentoot:post-parameter "source") f))
;; Save the template configuration
(let ((template-config (merge-pathnames (file-namestring (hunchentoot:post-parameter "filename")) (config-directory))))
(with-open-file (f template-config :direction :output
:if-does-not-exist :create
:if-exists :supersede)
(prin1 (hunchentoot:post-parameters*) f)))
(hunchentoot:redirect (format nil "/?name=~a&id=~a" (hunchentoot:post-parameter "filename") (make-template-id filepath)))))
((hunchentoot:post-parameter "delete")
(uiop/filesystem:delete-file-if-exists (merge-pathnames (file-namestring (hunchentoot:post-parameter "filename")) (templates-directory)))
(hunchentoot:redirect "/"))
((hunchentoot:post-parameter "reload")
(hunchentoot:redirect (format nil "/?name=~a&id=~a" (hunchentoot:post-parameter "filename") (hunchentoot:post-parameter "id"))))))
(defun read-template-arguments (template)
(cond
((not (str:blankp (template-data-url template)))
(multiple-value-bind (content status headers)
(drakma:http-request (template-data-url template) :want-stream t)
(cond
((>= status 400)
(error "Cannot access url: ~a" (template-data-url template)))
((str:containsp "lisp" (access:access headers :content-type))
(safe-read:safe-read content))
((str:containsp "json" (access:access headers :content-type))
(json:decode-json-from-source content))
(t
(error "Cannot resolve data-url content type")))))
((not (str:blankp (template-arguments template)))
(if (char= (aref (template-arguments template) 0) #\( )
;; we assume it is Lisp data if it starts with a parenthesis character
(read-from-string (template-arguments template))
;; otherwise, we assume it is json
(json:decode-json-from-string (template-arguments template))))))
(hunchentoot:define-easy-handler (render-template :uri "/render")
(id)
(let ((template (or (find-template id)
(error "Template not found: ~s" id))))
(handler-case
(let ((template-args (read-template-arguments template)))
(apply #'djula:render-template* (merge-pathnames (template-filename template) (templates-directory))
nil
(alexandria:alist-plist template-args)))
(error (e)
(cl-who:escape-string (write-to-string e :escape nil))))))
(defun render-settings-form (out)
(macrolet ((text-input (name label value &rest args)
`(with-html-output (out)
(:div :class "field is-small"
(:label :class "label is-small" (str ,label))
(:div :class "control"
(:input :name ,name
:class "input is-small"
:type "text"
:value ,value
,@args))))))
(with-html-output (out)
(:form :method "post"
(text-input "project-name" "Project name" *project-name* :readonly t)
(text-input "project-directory" "Project directory" (princ-to-string (project-directory)) :readonly t)
(text-input "templates-directory" "Templates directory" (princ-to-string (templates-directory)) :readonly t)
(text-input "assets-directory" "Assets directory" (assets-directory) :readonly t)
(text-input "config-directory" "Config directory" (princ-to-string (config-directory)) :readonly t)
(text-input "template-files-pattern" "Templates file pattern" *template-files-pattern*)
(:div :class "field"
(:label :class "checkbox"
(:input :name "debug-mode"
:type "checkbox"
:checked djula:*debug-mode*)
(str "Templates debug mode"))
(:p :class "help" (str "Display a panel with information about the rendered template.")))
(:div :class "field"
(:label :class "checkbox"
(:input :name "strict-mode"
:type "checkbox"
:checked djula:*strict-mode*)
(str "Templates strict mode"))
(:p :class "help" (str "Signal template errors when trying to access an unbound variable.")))
(:div :class "field is-grouped"
(:div :class "control"
(:button :class "button is-primary is-small"
:type "submit"
:name "update"
(str "Update"))))))))
(hunchentoot:define-easy-handler (settings-handler :uri "/settings")
()
(case (hunchentoot:request-method*)
(:get
(with-html-output-to-string (html)
(with-site-html html
(lambda ()
(render-navigation-bar html)
(htm (:div :class "container"
(render-settings-form html)))))))
(:post
(setf djula:*debug-mode* (and (hunchentoot:post-parameter "debug-mode") t))
(setf djula:*strict-mode* (and (hunchentoot:post-parameter "strict-mode") t))
(setf *template-files-pattern* (hunchentoot:post-parameter "template-files-pattern"))
(hunchentoot:redirect "/settings"))))
(defparameter *djula-docs-url* "http://mmontone.github.io/djula/djula/")
(hunchentoot:define-easy-handler (help-handler :uri "/help")
()
(with-html-output-to-string (html)
(with-site-html html
(lambda ()
(htm (:style
(str "#contents * {all:revert};")))
(render-navigation-bar html)
(htm (:div :class "container"
:id "contents"
(cl-markdown:markdown
(asdf:system-relative-pathname :template-designer "README.md")
:stream html)))))))
(defvar *acceptor*)
(defun start (project-name &key (port 0)
project-directory
config-directory
templates-directory
assets-directory
(open-browser t))
(setf *project-name* project-name)
(setf *project-directory* project-directory)
(setf *config-directory* config-directory)
(setf *templates-directory* templates-directory)
(setf *assets-directory* assets-directory)
(djula:add-template-directory (templates-directory))
(push (hunchentoot:create-folder-dispatcher-and-handler "/assets/" (assets-directory))
hunchentoot:*dispatch-table*)
(setf *acceptor*
(hunchentoot:start (make-instance 'hunchentoot:easy-acceptor :port port)))
(when open-browser
(trivial-open-browser:open-browser (format nil "http://localhost:~a" (hunchentoot:acceptor-port *acceptor*))))
*acceptor*)
(defun stop ()
(hunchentoot:stop *acceptor*)
(setf *acceptor* nil))
(defun open-browser ()
(trivial-open-browser:open-browser
(format nil "http://localhost:~a" (hunchentoot:acceptor-port *acceptor*))))
;; To start some project:
;; (start "my-project")
;; Demo using Djula demo files:
;; (start "djula-demo" :templates-directory (asdf:system-relative-pathname :djula "demo/templates/"))
;; Stop the template designer:
;; (stop)