Fork me on GitHub
#clerk
<
2024-03-19
>
Ruben Sevaldson14:03:35

Hi 🙂 I am a beginner with Clojure and clerk and have a couple of questions: • Is there an easy way to pretty print xml and/or html in clerk? • Is there a way to tell clerk to print a string in its entirety, without having to click "... more"?

teodorlu21:03:31

Hi 🙂 > Is there an easy way to pretty print xml and/or html in clerk? For both HTML and XML I’d probably convert to Clojure data structures first, then use the built-in viewers. If you really want to view XML/HTML as text, it appears that xml is supported in fenced code blocks, and those can be produced programmatically. Something like this:

(defn view-xml [xml-str]
  (clerk/md (str "
xml\n" xml-str "\n
\n")))

(view-xml "Ruben")

teodorlu21:03:49

reading the book https://book.clerk.vision/#code hinted towards using clerk/code with options. That didn’t seem to give me syntax highlighting, so I may be doing something wrong. (`io.github.nextjournal/clerk {:mvn/version "0.15.957"}` )

teodorlu21:03:12

> • Is there a way to tell clerk to print a string in its entirety, without having to click “... more”? I’ve used this to view big tables:

(require '[nextjournal.clerk :as clerk]
         '[nextjournal.clerk.viewer :as v])

(defn big-table [x]
  (v/with-viewer (dissoc v/table-viewer :page-size)
    x))

(big-table (repeatedly 200 (fn [] {:x (rand)})))
I’m guessing that something similar can be done for a string viewer, eg
(defn big-string [x]
  (v/with-viewer (dissoc v/string-viewer :page-size)
    x))
, but I haven’t tried!

teodorlu21:03:12

People who know Clerk better than I may be able to give you better answers!

Ruben Sevaldson17:03:53

Thanks a lot! Will try this out

Ruben Sevaldson17:03:54

> For both HTML and XML I’d probably convert to Clojure data structures first, then use the built-in viewers. Agreed, but I find the representation is a bit less concise than the raw XML? For example:

(xml/parse (java.io.ByteArrayInputStream. (.getBytes "<person><name>Ruben</name></person>"))) 
Becomes
{:tag :person,
 :attrs nil,
 :content [{:tag :name, :attrs nil, :content ["Ruben"]}]}
I think it can become a bit much when dealing with large XML documents

teodorlu18:03:44

Yeah, good point. Hiccup is probably a bit more compact.