From a5b56c1d637badb4afa0f05486643c62cdd60025 Mon Sep 17 00:00:00 2001 From: carbon-hvze <6391474+carbon-hvze@users.noreply.github.com> Date: Mon, 30 Oct 2023 21:22:54 +0300 Subject: [PATCH] filter document body in logger, impl templates in zd schema --- src/zd/core.clj | 22 ++++++++++++++++------ src/zd/schema.clj | 28 +++++++++++++++++++++++++++- src/zd/view/core.clj | 2 +- src/zd/view/editor.clj | 10 +++++++--- 4 files changed, 51 insertions(+), 11 deletions(-) diff --git a/src/zd/core.clj b/src/zd/core.clj index aed057e..5064716 100644 --- a/src/zd/core.clj +++ b/src/zd/core.clj @@ -89,10 +89,12 @@ (defmethod zen/op 'zd/new-doc [ztx _cfg {{docname :docname parent :parent} :params :as req} & opts] - {:status 200 - :body (hiccup.core/html (view/editor ztx req {:zd/docname (cond docname (symbol docname) - parent (symbol (str parent ".<>" )) - :else 'new)} ""))}) + (let [doc {:zd/parent (symbol parent) + :zd/docname (cond docname (symbol docname) + parent (symbol (str parent ".<>")) + :else 'new)}] + {:status 200 + :body (hiccup.core/html (view/editor ztx req doc ""))})) (defmethod zen/op 'zd/new-preview [ztx _cfg {body :body :as req} & opts] @@ -192,8 +194,14 @@ (swap! ztx dissoc :zdb :zd/backlinks)) (defmethod zen/op 'zd.events/logger - [ztx config {ev-name :ev :as ev} & opts] - (println (assoc ev ::ts (str (java.util.Date.))))) + [ztx config {ev-name :ev doc :params :as ev} & opts] + (let [ev (assoc ev ::ts (str (java.util.Date.)))] + (if (= ev-name 'zd.events/on-doc-load) + (println (assoc (dissoc ev :params) :docname (:zd/docname doc))) + (println ev)))) + +(defmethod zen-web.core/middleware-in 'zd/auth + [ztx config ev & opts]) (defn start [& [dir gitsync]] (let [ztx (zen/new-context {:zd/dir dir :zd/gitsync gitsync})] @@ -211,6 +219,8 @@ (stop ztx) + (:zd/props @ztx) + (:zd/backlinks @ztx) (git/exec {:dir (:zd/dir @ztx) :exec ["git" "log" "-1"]}) diff --git a/src/zd/schema.clj b/src/zd/schema.clj index 9cd979d..bae378b 100644 --- a/src/zd/schema.clj +++ b/src/zd/schema.clj @@ -1,10 +1,10 @@ (ns zd.schema (:require [clojure.string :as str])) - (defn to-keyname [docname] (let [parts (str/split (str docname) #"\.") ns (str/join "." (butlast parts))] + ;; TODO rename to _schema.zd ? (if (= ns "_") (keyword (last parts)) (keyword ns (last parts))))) @@ -18,6 +18,32 @@ (swap! ztx update :zd/classes dissoc docname) docname) +(defn get-class-props [ztx classname] + (into (filter (fn [[k v]] + ;; TODO think about different naming for root sch + (= (:zd/parent v) '_)) + (:zd/props @ztx)) + (filter (fn [[k v]] + (= (:zd/parent v) classname)) + (:zd/props @ztx)))) + +(defn get-class-template [ztx classname] + (->> (get-class-props ztx classname) + (map (fn [[k {df :zd/default dt :zd/data-type}]] + (cond + (and (= dt 'zd.string) (str/blank? df)) + (str k " \"\"") + + (= 'zd.string dt) + (format (str k " \"%s\"") df) + + (= 'zd.zentext dt) + (format (str k " /\n%s") df) + + :else + (format (str k " %s") df)))) + (str/join "\n"))) + (defn get-class [ztx docname] (or (get-in @ztx [:zd/classes docname]) (get-in @ztx [:zdb docname]))) diff --git a/src/zd/view/core.clj b/src/zd/view/core.clj index 2a726bc..12216e9 100644 --- a/src/zd/view/core.clj +++ b/src/zd/view/core.clj @@ -26,7 +26,7 @@ (layout/layout-with-menu ztx ctx doc (view ztx ctx doc))) (defn editor [ztx ctx doc content] - (layout/layout ztx ctx (zd.view.editor/editor ztx ctx doc content))) + (layout/layout ztx ctx (zd.view.editor/editor ztx ctx doc content))) (defn timeline [ztx ctx data] (layout/layout-with-menu ztx ctx {:zd/docname 'git} (timeline/view ztx data))) diff --git a/src/zd/view/editor.clj b/src/zd/view/editor.clj index 727b555..6bc6daa 100644 --- a/src/zd/view/editor.clj +++ b/src/zd/view/editor.clj @@ -1,18 +1,22 @@ (ns zd.view.editor (:require [cheshire.core :as json] + [clojure.string :as str] [hiccup.core] [hiccup.util] + [zd.schema :as sch] [zd.view.icons :as icons] [zd.store :as store])) - -;; TODO: templates (defn editor [ztx ctx {docname :zd/docname :as doc} content] (let [header (str ":zd/docname " (:zd/docname doc) "\n") text (str header content) + template (when (str/blank? content) + (sch/get-class-template ztx (:zd/parent doc))) symbols (store/symbols ztx) anns (store/annotations ztx) - zendoc {:text text + zendoc {:text (if (str/blank? content) + (str text "\n" template) + text) :symbols symbols :keys (store/props ztx) :icons icons/icons