I’m looking to be able to create Clojure files looking like this:
(ns th.scratch.doc.doc1
(:require [th.scratch.doc.lib :refer [document h1 p]]))
(document "My document")
(p "An attempt to write things that naturally sequence")
(h1 "What is natural sequencing?")
(p "Natural sequencing is when the order of operations matter."
"When you write a word document, this is typically the case.")
Currently, th.scratch.doc.lib just contains stubs. I’m looking to implement those.
I want to be able to organize the generated documents from the namespace in which they are created. In this case th.scratch.doc.doc1. I’m thinking I might need macros and *ns*, but I’m on thin ground, so I’m asking.
How would you enable document and its likes to be organizable from the namespace in which they are used?
(ns doclib)
(defonce ^:private *state (atom {}))
(defn- add! [key str]
(swap! *state update *ns* (fnil conj []) [key str])
nil)
(defmacro ^:private deftag [sym]
(let [key (keyword sym)]
`(defn ~sym [& strs#]
(doseq [str# strs#]
(add! ~key str#)))))
(deftag document)
(deftag p)
(deftag h1)
(defn get-doc []
(get @*state *ns*))
(ns doclib-user
(:require [doclib :refer [document p h1]]))
(document "My document")
(p "An attempt to write things that naturally sequence")
(h1 "What is natural sequencing?")
(p "Natural sequencing is when the order of operations matter."
"When you write a word document, this is typically the case.")
(doclib/get-doc)
=> [[:document "My document"]
[:p "An attempt to write things that naturally sequence"]
[:h1 "What is natural sequencing?"]
[:p "Natural sequencing is when the order of operations matter."]
[:p "When you write a word document, this is typically the case."]]
One problem I see with it is that re-evaluating forms in doclib-user will add new entries into it’s doc.
I’m sorta converging on the same solution myself. I’m thinking of giving the document a special meaning like the ns macro, to reset “current document progression”.
With some reworking, I ended up here, utilizing metasorous/oz for live-reload:
;; document
(ns th.doc.doc1
(:require [th.doc.api :as doc :refer :all]))
(document "My other document")
(p "An attempt to write things that naturally sequence")
(h1 "What is natural sequencing???")
(p "Natural sequencing is when the order of operations matter."
"When you write a word document, this is typically the case.")
(p "Can you see this?")
(def numbers (into [] (range 20)))
(h2 "Does this seem nice?")
(p "The numbers from 0 to 19 are: " (str numbers))
;; Get doc down here.
(doc/view-current-doc!)
Yet I seem to have messed up the macro, as numbers don’t get evaluated as it should: