generators-util.lisp (1550 bytes)
1 2 (in-package :cl-yag) 3 4 (defmacro generate(name &body data) 5 "Simplify file saving by using the layout system." 6 `(progn 7 (save-file ,name (generate-layout ,@data)))) 8 9 (defmacro template(before &body after) 10 "Simplify string replacement work in templates." 11 `(progn 12 (setf output (replace-all output ,before ,@after)))) 13 14 (defmacro with-converter(&body code) 15 "Get the converter object for an article and execute code in that context." 16 `(progn 17 (let ((converter-name (or (article-converter article) 18 (getf *config* :default-converter)))) 19 (let ((converter-object (getf *converters* converter-name))) 20 ,@code)))) 21 22 (defmacro prepare(template &body code) 23 "Simplify the declaration of a new page type by loading a template and executing code." 24 `(progn 25 (let ((output (load-file ,template))) 26 ,@code 27 output))) 28 29 30 (defun load-file(path) 31 "Load a file as a string. We escape ~ to avoid failures with format." 32 (if (probe-file path) 33 (handler-case (with-open-file (stream path :direction :input) 34 (let ((contents (make-string (file-length stream)))) 35 (read-sequence contents stream) 36 contents)) 37 (file-error (condition) 38 (cerror "ERROR : file ~a not found. Aborting~%" condition))) 39 )) 40 41 (defun save-file(path data) 42 "Save a string to a file." 43 (ensure-directories-exist path) 44 (with-open-file (stream path :direction :output :if-exists :supersede :if-does-not-exist :create) 45 (write-sequence data stream))) 46