Skip to content

Commit

Permalink
filter document body in logger, impl templates in zd schema
Browse files Browse the repository at this point in the history
  • Loading branch information
carbon-hvze committed Oct 30, 2023
1 parent d7835a1 commit a5b56c1
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 11 deletions.
22 changes: 16 additions & 6 deletions src/zd/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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})]
Expand All @@ -211,6 +219,8 @@

(stop ztx)

(:zd/props @ztx)

(:zd/backlinks @ztx)

(git/exec {:dir (:zd/dir @ztx) :exec ["git" "log" "-1"]})
Expand Down
28 changes: 27 additions & 1 deletion src/zd/schema.clj
Original file line number Diff line number Diff line change
@@ -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)))))
Expand All @@ -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])))
Expand Down
2 changes: 1 addition & 1 deletion src/zd/view/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
Expand Down
10 changes: 7 additions & 3 deletions src/zd/view/editor.clj
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit a5b56c1

Please sign in to comment.