Skip to content

Commit

Permalink
Update status + leave aside map dispatch for now
Browse files Browse the repository at this point in the history
This will require more work.
  • Loading branch information
respatialized committed Jul 14, 2024
1 parent 73c118c commit 6a9a879
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 5 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,13 +109,15 @@ span[data-clojure-symbol^="def"] {

Now everything that begins with `def` is covered - even if you forgot about a different one, like `clojure.test/deftest` - you'll be covered.

I use a similar rule to highlight instances of the most important function in this intro: <code><span data-clojure-symbol="clj->hiccup">clj->hiccup</span></code>. I hope Adorn gives you the data you need to highlight what makes the most sense for the code you're displaying, rather than forcing you into a


## Other libraries with overlapping aims
- [Glow](https://github.com/venantius/glow) is another server-side syntax highlighting library for Clojure. It only runs on the JVM because it uses [ANTLR](https://www.antlr.org/) to parse Clojure. It also uses [Enlive](https://github.com/cgrand/enlive) instead of Hiccup for its intermediate representation of parsed Clojure code.
- [Clygments](https://github.com/bfontaine/clygments) wraps the [Pygments](https://pygments.org/) Python library, which obviously means this library introduces a dependency on Python.

## Status
Pre-alpha. Moving towards a stable API, but does not yet have clearly defined contracts.
Pre-alpha. Moving towards a stable API, but does not yet have clearly defined contracts. Not yet implemented across all target platforms, does not yet have a fully robust extension mechanism, and still requires a unified data model for "forms."

The `site.fabricate.adorn.forms` namespace has a fairly complete set of functions that are used as building blocks.

Expand All @@ -127,6 +129,7 @@ The `site.fabricate.adorn.forms` namespace has a fairly complete set of function
- [ ] provide sensible defaults and an example of styling using plain CSS
- [ ] including at least one useful Flexbox example
- [x] provide an override mechanism for users who want to display particular forms in special ways
- [ ] provide a nested, map-based override mechanism for subforms in a Clojure form
- [x] provide an extension mechanism for special symbols (e.g. `def`, `defn`, `def-my-custom-def`)
- [ ] compatibility across Hiccup implementations

Expand Down
2 changes: 1 addition & 1 deletion dev/html/main.css
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ img {
max-width: 100%;
}

article p {
article p,ul,ol {
max-width: 58ch;
}

Expand Down
16 changes: 14 additions & 2 deletions src/site/fabricate/adorn.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
(defn form-type
"Get the type of the node for display, defaulting to the tag returned by `forms/node-type`.
If `{:display-type :custom-type}` data has been added to the form, return the type. `:display-type` passed as an option in the options map takes precedence over existing node data. `:display-type` can also be a map indicating how child nodes should be handled, in which case the `:self*` entry is used for the top-level node."
If `{:display-type :custom-type}` data has been added to the form, return the type. `:display-type` passed as an option in the options map takes precedence over existing node data. `:display-type` can also be a map indicating how child nodes should be handled, in which case the `:self*` entry is used for the top-level node."
([node opts]
(let [display-type (or (get opts :display-type)
(get node :display-type)
Expand Down Expand Up @@ -39,6 +39,18 @@ Falls back to defaults defined in `site.fabricate.adorn.forms` namespace for uni
(let [display-fn (get opts :display-type)]
(display-fn (forms/->node node) opts)))

(defmethod form->hiccup :display/map
([node {:keys [attrs display-type] :or {attrs {}} :as opts}]
#_(let [node (forms/->node node)
;; fall back to default if no self-display type is set
self-display-fn (:self* display-type)
converted (if self-display-fn (self-display-fn node opts))]
;; this is tricky; how does conversion work for the child nodes?
;; does the self display type override this or combine them somehow?
;;
(if (node/children node) '...))
(throw (ex-info "Display map not implemented" {:status "pre-alpha"}))))

(defmethod form->hiccup :fn
([node {:keys [attrs] :or {attrs {}} :as opts}]
(forms/fn->span (forms/->node node) attrs form->hiccup))
Expand Down Expand Up @@ -156,7 +168,7 @@ Falls back to defaults defined in `site.fabricate.adorn.forms` namespace for uni
(defn clj->hiccup
"Convert the given Clojure string, expression, or rewrite-clj node to a Hiccup data structure.
Uses the multimethod `site.fabricate.adorn/form->hiccup` for dispatch."
Uses the multimethod `site.fabricate.adorn/form->hiccup` for dispatch."
([src opts]
(form->hiccup (forms/->node src (select-keys opts [:lang :update-subnodes?]))
opts))
Expand Down
26 changes: 25 additions & 1 deletion test/site/fabricate/adorn_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,31 @@
(t/is (re-find #"custom" (get-in expr-hiccup [1 :class]))
"Dispatch based on :type metadata should work")
(t/is (re-find #"custom" (get-in expr-hiccup-2 [1 :class]))
"Dispatch based on option passed in should work"))))
"Dispatch based on option passed in should work"))
#_(t/testing "with map"
(let [map->dl (fn map->dl [m-node _opts]
(into [:dl]
(mapcat (fn [[k v]] [[:dt k] [:dd v]])
(node/sexpr m-node))))
test-map {:term "definition" :term2 "definition"}
expected-dl [:dl [:dt :term] [:dd "definition"] [:dt :term2]
[:dd "definition"]]]
(t/is (= expected-dl
(adorn/clj->hiccup test-map {:display-type map->dl}))
"passing a function should change the display type")
(t/is (= expected-dl
(adorn/clj->hiccup test-map {:display-type {:map map->dl}}))
"passing a map should change the display type")
(t/is (= expected-dl
(adorn/clj->hiccup test-map
{:display-type {:self* map->dl}}))
"passing a map should change the display type")))))

(comment
(adorn/clj->hiccup {:a 1 :b 2}
{:display-type (fn ident [i & _args] (node/sexpr i))})
(adorn/form-type (forms/->node {:a 1 :b 2})
{:display-type {:map (fn ident [i & _args] i)}}))

(t/deftest src-files
(let [forms-parsed (parse-file "src/site/fabricate/adorn/forms.cljc")
Expand Down

0 comments on commit 6a9a879

Please sign in to comment.