Skip to content

Commit

Permalink
[WIP] Markdown extensions + boilerplate for reference resolution
Browse files Browse the repository at this point in the history
To be completed at a later time.
  • Loading branch information
respatialized committed Dec 11, 2024
1 parent bcfac80 commit 86db165
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 11 deletions.
39 changes: 36 additions & 3 deletions dev/site/fabricate/dev/docstrings.clj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
"Tools for working with docstrings"
(:require [cybermonday.core :as md]
[cybermonday.ir :as md-ir]
[clojure.tools.reader :as reader]))
[cybermonday.parser :as parser]
[clojure.tools.reader :as reader]
[site.fabricate.dev.source.markdown :as markdown]
[site.fabricate.dev.references :as refs]))

(defn extract-bracket-text
[text]
Expand All @@ -23,11 +26,41 @@
:default (merge (extract-bracket-text bracket-text)
{:type :site.fabricate/term})))

#_(defn match-text
[text]
(cond (re-matches #"^'.*" text)
{:text text :type :symbol :symbol (reader/read-string contents)}
(re-matches #"^#'" text)
{:text text :type :var :var (reader/read-string contents)}
(re-matches #"^#:" text)
{:text text :type :keyword :keyword (reader/read-string contents)}
:default {:text text :type :term :term text}))

;; multimethod? dispatch on :type here?
#_(defn standardize
[term]
(let [term-data (match-text term)]
(cond (:term term-data) nil
(:var term-data) nil
(:symbol term-data) nil
(:keyword term-data) nil)))

#_(defn md-lower-fn
[[_kw {:keys [reference] :as m} text]]
(if-not (nil? reference)
[:a {:href reference} text]
(let [{:keys [link hiccup-data] :as std} (standardize text)]
[:a {:href link} hiccup-data])))

(comment
(resolve 'my-fake-ns/var)
(def example-docstring
"Return the data with ...something, after calling [[my-ns/fn]] on it")
(md/parse-body example-docstring {:lower-fns {:markdown/link-ref identity}})
(md-ir/md-to-ir example-docstring)
(def example-docstring-2
"Return the data with ...something, after calling [[my-ns/fn|`alias`]] on it")
(markdown/md->hiccup example-docstring)
(markdown/md->hiccup example-docstring-2)
(parser/to-hiccup (parser/parse example-docstring) example-docstring)
(println (md/parse-body
"Return the data with ...something, after calling [[some-fn]] on it"
{})))
25 changes: 24 additions & 1 deletion dev/site/fabricate/dev/elements.clj
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
(ns site.fabricate.dev.elements)
(ns site.fabricate.dev.elements
(:require [dev.onionpancakes.chassis.core :as c]))

(defn footer
([top-id]
Expand All @@ -24,3 +25,25 @@
[:img {:src "/media/fabricate-logo-v1.svg" :class "fabricate-header-logo"}]
[:h1 {:class "header-primary"} "Fabricate"]
[:h3 {:class "header-secondary"} "Form by art and labor"]])

(defn escape-css-string
"Escape the string so it can be used as a valid CSS identifier"
[str])

(defn anchor
"Generate a unique anchor from the given symbol."
[sym]
(let [resolved (resolve sym)]))

(comment
(c/escape-attribute-value (str (symbol (resolve 'anchor)))))

(defn function-doc
[fn-var]
(let [id (anchor fn-var)]
;; TODO: come up with canonical, or at least stable, way of converting
;; Clojure symbols and vars to and from properly escaped CSS
;; identifiers. the rest kind of flows from there, but each function
;; needs a UUID-like thing so that a URL can be generated from its
;; fully-qualified name
[:div {:class "fn-doc" :id id :data-clojure-var (str fn-var)}]))
4 changes: 2 additions & 2 deletions dev/site/fabricate/dev/references.clj
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
(if sym-ns
(str "/reference/namespaces/" sym-ns
".html#" (name term))
(str @glossary-path term)))}))
(str @glossary-path "#" term)))}))

(defmethod specify clojure.lang.Var
[term]
Expand All @@ -94,7 +94,7 @@
:link (if-let [kw-ns (namespace term)]
(str "/reference/namespaces/" kw-ns
".html#" (keyword (name term)))
(str @glossary-path term))})
(str @glossary-path "#" term))})

(comment
(specify #'site.fabricate.api/plan!)
Expand Down
16 changes: 15 additions & 1 deletion dev/site/fabricate/dev/source/markdown.clj
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,21 @@

(def node-tags "Extended mapping from Flexmark AST node to Hiccup tag" ())

;; TODO: figure out how to standardize and normalize this output
;; so it works with the rest of Cybermonday
(defn wikilink->map
"Convert the Wikilink object to a Clojure map"
[^WikiLink link]
{:link (str (.getLink link))
:page-ref (str (.getPageRef link))
;; the text field is non-nil only when there's a bracket alias
:text (str (.getText link))})

(extend-protocol md-parser/HiccupRepresentable
WikiLink
(to-hiccup [this source]
(cm-utils/make-hiccup-node :markdown/wikilink
{:reference source}
(wikilink->map this)
(md-parser/map-children-to-hiccup this
source))))

Expand All @@ -63,6 +73,10 @@
:members
(mapv #(dissoc % :exception-types :declaring-class))
clojure.pprint/print-table)
(->> (clojure.reflect/reflect WikiLink)
:members
(mapv #(dissoc % :exception-types :declaring-class))
clojure.pprint/print-table)
(.getDocument (flexmark-parse "[[wikilink]]"))
(.getReference (.getDocument (flexmark-parse "[[wikilink]]")))
((.getDocument (flexmark-parse
Expand Down
5 changes: 5 additions & 0 deletions test/site/fabricate/dev/references_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,8 @@
(t/testing (str term)
(t/is (map? (refs/specify term))
"Basic term types should be specifiable")))))

(comment
(mapv refs/specify
[#'site.fabricate.api/plan! 'site.fabricate.api/construct! :basic-kw
:site.fabricate.source/location]))
11 changes: 7 additions & 4 deletions test/site/fabricate/dev/source/markdown_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@


(t/deftest parsing-extensions
(let [test-txt "[[basic wikilink]]"
test-hiccup (md/md->hiccup test-txt)]
(let [test-txt "[[basic wikilink]]"
test-hiccup (md/md->hiccup test-txt)
test-docstring
"Return the data with ...something, after calling [[my-ns/fn]] on it"]
(t/testing "wikilinks"
(t/is (some? test-hiccup)
"Basic parsing should return values without errors")
(t/is (= "[[basic wikilink]]" (get-in test-hiccup [2 2 1 :reference]))
"Link text with brackets should be retained")
;; TODO: fix this property
#_(t/is (= "[[basic wikilink]]" (get-in test-hiccup [2 2 1 :reference]))
"Link text with brackets should be retained")
(t/is (= "basic wikilink" (get-in test-hiccup [2 2 2]))
"Link contents should be taken out of brackets in body text")
(t/testing "lowering fns"
Expand Down

0 comments on commit 86db165

Please sign in to comment.