If you would like to add some logic around which viewer to use by default in
Portal, the easiest way is to provide a :portal.viewer/default
key as part of
a value's metadata. Unfortunately, not all values support metadata, such as
strings. They way around this limitation is to wrap the value in a container
that does support metadata, such as vectors.
Here is on such default viewer selector:
(def defaults
{string? :portal.viewer/text
bytes? :portal.viewer/bin})
(defn- get-viewer [value]
(or (some (fn [[predicate viewer]]
(when (predicate value)
viewer))
defaults)
:portal.viewer/tree))
With a corresponding submit function:
(require '[portal.api :as p])
(defn submit [value]
(p/submit
(with-meta
[(get-viewer value) value]
{:portal.viewer/default :portal.viewer/hiccup})))
(add-tap #'submit)
Now by tapping the following values you can see the default viewer selector in action:
(tap> "hello, world")
(tap> (byte-array [0 1 2 3]))
(tap> (range 10))
Can you improve this documentation?Edit on GitHub
cljdoc is a website building & hosting documentation for Clojure/Script libraries
× close