ethics.lisp (9334B)
1 (defpackage :ethics 2 (:use :cl) 3 (:export #:write-ethics) 4 (:local-nicknames 5 (:md :markdown.cl) 6 (:jzon :com.inuoe.jzon))) 7 8 (in-package :ethics) 9 10 ;; --- Importing Data 11 12 (defparameter config 13 '(:build-dir #p"../build/" 14 :template-dir #p"../html/" 15 :data-dir #p"../data/" 16 :versions ("en_elwes" 17 "la_spinoza" 18 "de_stern" 19 "de_auerbach" 20 "fr_saisset" 21 "fr_appuhn"))) 22 23 (defparameter *template-cache* nil) 24 (defvar *level* nil) 25 (defvar *version* nil) 26 27 (defclass version () 28 ((key :reader key 29 :initarg :key) 30 (path :reader path 31 :initarg :path) 32 (name :reader name 33 :initarg :name) 34 (translator :reader translator 35 :initarg :translator) 36 (language :reader language 37 :initarg :language) 38 (types :reader types 39 :initarg :types) 40 (meta :reader meta 41 :initarg :meta) 42 (file :reader file 43 :initarg :file))) 44 45 (defclass node () 46 ((id :reader id 47 :initarg :id) 48 (type :reader ntype 49 :initarg :type) 50 (refs :reader ref-keys 51 :initarg :ref-keys) 52 (number :initarg :number 53 :reader num) 54 (backlinks :accessor backlinks 55 :initform '()) 56 (child-keys :reader child-keys 57 :initarg :child-keys) 58 (children :accessor children 59 :initarg :children) 60 (text :initform (make-hash-table :test #'equal)))) 61 62 (defmethod node-type ((node node) (version version)) 63 (gethash (ntype node) (types version))) 64 65 (defmethod vtext ((node node) (version version)) 66 (with-slots (key) version 67 (with-slots (text) node 68 (gethash key text)))) 69 70 (defmethod (setf vtext) (value (node node) (version version)) 71 (with-slots (key) version 72 (with-slots (text) node 73 (setf (gethash key text) value)))) 74 75 (defmethod (setf vtext) (value (node node) (key string)) 76 (with-slots (text) node 77 (setf (gethash key text) value))) 78 79 (defun get-template (template config) 80 (declare (type pathname template)) 81 (plump:clone-node 82 (uiop:ensure-gethash 83 template *template-cache* 84 (lambda () 85 (let ((path (merge-pathnames template (getf config :template-dir)))) 86 (plump:parse path)))))) 87 88 (defmethod clip:clip ((value string) field) 89 value) 90 91 (defun read-meta (json) 92 (jzon:with-parser (parser json) 93 (let (top stack key) 94 (labels ((finish-value (value) 95 (typecase stack 96 (null (setf top value)) 97 ((cons list) (push value (car stack))) 98 ((cons hash-table) 99 (setf (gethash (pop key) (car stack)) 100 value)))) 101 (finish-object (value) 102 (finish-value 103 (if stack (make-instance 104 'node 105 :id (gethash "id" value) 106 :type (gethash "type" value) 107 :ref-keys (gethash "ref" value) 108 :number (gethash "number" value) 109 :child-keys (gethash "children" value)) 110 value)))) 111 112 (loop (multiple-value-bind (event value) (jzon:parse-next parser) 113 (case event 114 ((nil) (return top)) 115 (:value (finish-value value)) 116 (:begin-array (push (list) stack)) 117 (:end-array (finish-value 118 (coerce (the list (nreverse (pop stack))) 119 'simple-vector))) 120 (:begin-object (push (make-hash-table :test 'equal) stack)) 121 (:object-key (push value key)) 122 (:end-object (finish-object (pop stack)))))))))) 123 124 (defun read-version (key dir meta) 125 (format t "Reading: ~a~&" key) 126 (loop :with version := nil 127 :for path :in (uiop:directory-files dir) 128 :as ftype := (pathname-type path) 129 :as fname := (pathname-name path) 130 :when (string= ftype "md") 131 :do (setf (vtext (gethash fname meta) key) 132 (uiop:read-file-string path)) 133 :when (string= ftype "json") 134 :do (setf version (jzon:parse path)) 135 :finally (return 136 (make-instance 137 'version 138 :key key 139 :path dir 140 :name (gethash "name" version) 141 :translator (gethash "translator" version) 142 :language (gethash "language" version) 143 :types (gethash "types" version) 144 :meta version 145 :file (make-pathname :name key :type "html"))))) 146 147 (defun import-versions (config tbl) 148 (loop :for path :in (loop :with data-dir := (getf config :data-dir) 149 :for v :in (getf config :versions) 150 :collect (make-pathname 151 :directory 152 (append (pathname-directory data-dir) 153 (list v)))) 154 :as fname := (car (last (pathname-directory path))) 155 :collect (read-version fname path tbl) :into versions 156 :finally (return (cons tbl versions)))) 157 158 (defun get-nodes (tbl nodes) 159 (when nodes 160 (loop :with res := (make-array (length nodes) :fill-pointer 0) 161 :for key :across nodes 162 :as node := (gethash key tbl) 163 :do (vector-push node res) 164 :finally (return res)))) 165 166 (defun make-links (tbl id nodes) 167 (when nodes 168 (loop :for node :across nodes 169 :do (push id (backlinks (gethash node tbl)))))) 170 171 (defun build-tree (meta) 172 (loop :for node :being :each :hash-value :in meta 173 :do (make-links meta (id node) (ref-keys node)) 174 (setf (children node) 175 (get-nodes meta (child-keys node))) 176 177 :finally (return (gethash "ethica" meta)))) 178 179 (defun import-ethics (config) 180 (let ((tbl (read-meta (merge-pathnames #p"meta.json" 181 (getf config :data-dir))))) 182 (build-tree tbl) 183 (import-versions config tbl))) 184 185 ;; --- generate html 186 187 (defun process-to-file (template outpath &rest bindings) 188 (with-open-file (out outpath 189 :direction :output 190 :external-format :utf8 191 :if-exists :supersede 192 :if-does-not-exist :create) 193 (plump:serialize 194 (apply #'clip:process 195 template 196 bindings) 197 out))) 198 199 (defmethod make-node ((node node)) 200 (let ((*level* (1+ (or *level* 0)))) 201 (clip:process 202 (get-template #p"node.chtml" config) 203 :node node 204 :level *level* 205 :version *version*))) 206 207 (defun write-ethics (config) 208 (let ((*template-cache* (make-hash-table))) 209 (destructuring-bind (ethica &rest versions) 210 (import-ethics config) 211 (process-to-file 212 (get-template #p"index.chtml" config) 213 (merge-pathnames #p"index.html" (getf config :build-dir)) 214 :versions versions) 215 (dolist (version versions) 216 (let ((*version* version) 217 (*package* (find-package :ethics))) 218 (format t "Writing: ~a~&" (key version)) 219 (process-to-file 220 (get-template #p "ethics.chtml" config) 221 (merge-pathnames (file version) 222 (getf config :build-dir)) 223 :ethica (gethash "ethica" ethica) 224 :version *version*)))))) 225 226 ;; --- tag defs 227 228 (clip:define-tag-processor md (xml-node) 229 (clip:process-attributes xml-node) 230 (setf (plump:tag-name xml-node) "div") 231 (let ((node (clip:resolve-attribute xml-node "node"))) 232 (lquery:$1 xml-node (html (markdown:parse (vtext node *version*))))) 233 (plump:remove-attribute xml-node "node")) 234 235 (clip:define-tag-processor node (xml-node) 236 (clip:process-attributes xml-node) 237 (let ((result (clip:resolve-attribute xml-node "node"))) 238 (plump:remove-attribute xml-node "node") 239 (lquery:$1 xml-node (html (make-node result))) 240 (clip:process-tag "splice" xml-node))) 241 242 (clip:define-attribute-processor text (node value) 243 (lquery:$1 node (text value)) 244 (plump:remove-attribute node "text")) 245 246 ;; --- plumbing 247 248 (defmacro parse-args (&rest args) 249 `(loop :with versions := nil 250 :for arg :on (uiop:command-line-arguments) :by #'cddr 251 :as flag := (car arg) 252 :as value := (cadr arg) 253 :if (string= flag "--") 254 :do (setf versions (cdr arg)) 255 (loop-finish) 256 :else 257 :nconc (parse-arg flag value ,@args) :into pairs 258 :finally (return (append pairs (list :versions versions))))) 259 260 (defun parse-arg (flag value &rest args) 261 (let ((key (and (uiop:string-prefix-p "--" flag) 262 (intern (string-upcase (subseq flag 2)) 263 :keyword)))) 264 (if (member key args) 265 (list key value) 266 (error "Unrecognized command line argument: ~a ~a" flag value)))) 267 268 (defun main () 269 (let ((config (parse-args :build-dir :template-dir :data-dir))) 270 (format t "Arguments: ~a~&" config) 271 (write-ethics config))) 272