ethica

Spinozas ethics and various translations, edited for easy post processing.
git clone https://git.repetitions.de/ethica
commit 76bad781b8a085b33695a3c20ac5e836e4e417d0
parent 560e424661273522a6bbbc8f6d943b1603e53b84
Author: lou woell <lou@repetitions.de>
Date:   Fri,  6 Nov 2026 19:48:55 +0100

refactor

Diffstat:
Mhtml/ethics.chtml | 4++--
Mhtml/node.chtml | 4++--
Mlisp/ethics.lisp | 191+++++++++++++++++++++++++++++++++++++++++++++++++++++++------------------------
3 files changed, 137 insertions(+), 62 deletions(-)

diff --git a/html/ethics.chtml b/html/ethics.chtml @@ -2,7 +2,7 @@ <html lquery="(attr :lang language)"> <head> <title> - <c:s>(gethash "ethica" types)</c:s>: <c:s>name</c:s> + <c:s>(node-type ethica version)</c:s>: <c:s>(name version)</c:s> </title> <meta charset="utf-8"> <link rel="stylesheet" href="assets/style.css"> @@ -15,7 +15,7 @@ <hr> </nav> <div class="container"> - <c:node node="tree"/> + <c:node node="ethica"/> </div> </body> </html> diff --git a/html/node.chtml b/html/node.chtml @@ -1,7 +1,7 @@ <div class="node" lquery="(attr :id id) (add-class type)"> <c:expand class="headline" fill="as *" as="h{level}"> - <c:s>(gethash type types)</c:s> <c:s>(gethash "number" node "")</c:s> - <a class="anchor" href="#{id}" fill="href *">#<c:s>id</c:s></a> + <c:s>(node-type node version)</c:s> <c:s>(or (num node) "")</c:s> + <a class="anchor" fill="href *" href="#{id}">#<c:s>id</c:s></a> </c:expand> <c:when test="backlinks"> <div class="backlinks"> diff --git a/lisp/ethics.lisp b/lisp/ethics.lisp @@ -20,10 +20,61 @@ "fr_saisset" "fr_appuhn"))) -(defparameter *template-cache* (make-hash-table)) -(defvar *vkey* nil) +(defparameter *template-cache* nil) (defvar *level* nil) -(defvar *types* nil) +(defvar *version* nil) + +(defclass version () + ((key :reader key + :initarg :key) + (path :reader path + :initarg :path) + (name :reader name + :initarg :name) + (translator :reader translator + :initarg :translator) + (language :reader language + :initarg :language) + (types :reader types + :initarg :types) + (meta :reader meta + :initarg :meta) + (file :reader file + :initarg :file))) + +(defclass node () + ((id :reader id + :initarg :id) + (type :reader ntype + :initarg :type) + (refs :reader ref-keys + :initarg :ref-keys) + (number :initarg :number + :reader num) + (backlinks :accessor backlinks + :initform (make-array 10 :adjustable t :fill-pointer 0)) + (child-keys :reader child-keys + :initarg :child-keys) + (children :accessor children + :initarg :children) + (text :initform (make-hash-table :test #'equal)))) + +(defmethod node-type ((node node) (version version)) + (gethash (ntype node) (types version))) + +(defmethod vtext ((node node) (version version)) + (with-slots (key) version + (with-slots (text) node + (gethash key text)))) + +(defmethod (setf vtext) (value (node node) (version version)) + (with-slots (key) version + (with-slots (text) node + (setf (gethash key text) value)))) + +(defmethod (setf vtext) (value (node node) (key string)) + (with-slots (text) node + (setf (gethash key text) value))) (defun get-template (template config) (declare (type pathname template)) @@ -34,18 +85,59 @@ (let ((path (merge-pathnames template (getf config :template-dir)))) (plump:parse path)))))) -(defun read-version (key dir tbl) +(defun read-meta (json) + (jzon:with-parser (parser json) + (let (top stack key) + (labels ((finish-value (value) + (typecase stack + (null (setf top value)) + ((cons list) (push value (car stack))) + ((cons hash-table) + (setf (gethash (pop key) (car stack)) + value)))) + (finish-object (value) + (finish-value + (if stack (make-instance 'node + :id (gethash "id" value) + :type (gethash "type" value) + :ref-keys (gethash "ref" value) + :number (gethash "number" value) + :child-keys (gethash "children" value)) + value)))) + + (loop (multiple-value-bind (event value) (jzon:parse-next parser) + (case event + ((nil) (return top)) + (:value (finish-value value)) + (:begin-array (push (list) stack)) + (:end-array (finish-value (coerce (the list (nreverse (pop stack))) + 'simple-vector))) + (:begin-object (push (make-hash-table :test 'equal) stack)) + (:object-key (push value key)) + (:end-object (finish-object (pop stack)))))))))) + +(defun read-version (key dir meta) (format t "Reading: ~a~&" key) - (loop :with meta := nil + (loop :with version := nil :for path :in (uiop:directory-files dir) :as ftype := (pathname-type path) :as fname := (pathname-name path) :when (string= ftype "md") - :do (setf (gethash key (gethash fname tbl)) + :do (setf (vtext (gethash fname meta) key) (uiop:read-file-string path)) :when (string= ftype "json") - :do (setf meta (jzon:parse path)) - :finally (return meta))) + :do (setf version (jzon:parse path)) + :finally (return + (make-instance + 'version + :key key + :path dir + :name (gethash "name" version) + :translator (gethash "translator" version) + :language (gethash "language" version) + :types (gethash "types" version) + :meta version + :file (make-pathname :name key :type "html"))))) (defun import-versions (config tbl) (loop :for path :in (loop :with data-dir := (getf config :data-dir) @@ -55,20 +147,7 @@ (append (pathname-directory data-dir) (list v)))) :as fname := (car (last (pathname-directory path))) - :as data := (read-version fname path tbl) - :collect - (let ((v (list :key fname - :path path - :name (gethash "name" data) - :translator (gethash "translator" data) - :language (gethash "language" data) - :types (gethash "types" data) - :meta data - :file (make-pathname - :name fname - :type "html")))) - (format t "Version: ~S~&" v) v) - :into versions + :collect (read-version fname path tbl) :into versions :finally (return (cons tbl versions)))) (defun get-nodes (tbl nodes) @@ -82,19 +161,19 @@ (defun make-links (tbl id nodes) (when nodes (loop :for node :across nodes - :do (push id (gethash :links (gethash node tbl)))))) + :do (vector-push id (backlinks (gethash node tbl)))))) -(defun build-tree (tbl) - (loop :for node :being :each :hash-value :in tbl - :as children := (gethash "children" node) - :do (make-links tbl (gethash "id" node) (gethash "ref" node)) - (setf (gethash :child-nodes node) - (get-nodes tbl children)) - :finally (return (gethash "ethica" tbl)))) +(defun build-tree (meta) + (loop :for node :being :each :hash-value :in meta + :do (make-links meta (id node) (ref-keys node)) + (setf (children node) + (get-nodes meta (child-keys node))) + + :finally (return (gethash "ethica" meta)))) (defun import-ethics (config) - (let ((tbl (jzon:parse (merge-pathnames - #p"meta.json" (getf config :data-dir))))) + (let ((tbl (read-meta (merge-pathnames #p"meta.json" + (getf config :data-dir))))) (build-tree tbl) (import-versions config tbl))) @@ -118,32 +197,28 @@ (get-template #p"node.chtml" config) :node node :level *level* - :types *types* - :id (gethash "id" node) - :type (gethash "type" node) - :refs (gethash "ref" node) - :backlinks (gethash :links node) - :children (gethash :child-nodes node)))) + :children (children node) + :type (ntype node) + :version *version*))) (defun write-ethics (config) - (destructuring-bind (ethica &rest versions) - (import-ethics config) - (process-to-file - (get-template #p"index.chtml" config) - (merge-pathnames #p"index.html" (getf config :build-dir)) - :versions versions) - (dolist (version versions) - (let ((*vkey* (getf version :key)) - (*types* (getf version :types))) - (format t "Writing: ~a~&" *vkey*) - (process-to-file - (get-template #p "ethics.chtml" config) - (merge-pathnames (getf version :file) - (getf config :build-dir)) - :tree (gethash "ethica" ethica) - :name (getf version :name) - :types *types* - :language (getf version :language)))))) + (let ((*template-cache* (make-hash-table))) + (destructuring-bind (ethica &rest versions) + (import-ethics config) + (process-to-file + (get-template #p"index.chtml" config) + (merge-pathnames #p"index.html" (getf config :build-dir)) + :versions versions) + (dolist (version versions) + (let ((*version* version) + (*package* (find-package :ethics))) + (format t "Writing: ~a~&" (key version)) + (process-to-file + (get-template #p "ethics.chtml" config) + (merge-pathnames (file version) + (getf config :build-dir)) + :ethica (gethash "ethica" ethica) + :version *version*)))))) ;; --- tag defs @@ -151,11 +226,11 @@ (clip:process-attributes xml-node) (setf (plump:tag-name xml-node) "div") (let ((node (clip:resolve-attribute xml-node "node"))) - (lquery:$1 xml-node (html (markdown:parse (gethash *vkey* node))))) + (lquery:$1 xml-node (html (markdown:parse (vtext node *version*))))) (plump:remove-attribute xml-node "node")) (clip:define-tag-processor node (xml-node) - ;; (clip:process-attributes xml-node) + (clip:process-attributes xml-node) (let ((result (clip:resolve-attribute xml-node "node"))) (plump:remove-attribute xml-node "node") (lquery:$1 xml-node (html (make-node result)))