Fork me on GitHub
#cryogen
<
2021-08-30
>
NoahTheDuke14:08:34

now that i’ve gotten that set up, next question! and maybe this should go somewhere else, so please direct me if necessary. I’m looking to render all of my code snippets server-side using Pygments (with https://github.com/bfontaine/clygments), and can’t quite figure out how to hook into the rendering process to modify only the code blocks

Jakub Holý (HolyJak)15:08:37

You cannot, not via cryogen proper. Rendering markup to html is done by the markup plugin such as cryogen-asciidoctor. Look at what it allows (eg asciidoctor allows custom macros / extensions) or fork and adjust it.

Jakub Holý (HolyJak)15:08:08

Otherwise you'd need to do some post-processing of the html

NoahTheDuke21:08:58

well, i figured out how to do it, but it’s ugly lol

NoahTheDuke21:08:28

(defn transform-html
  "Creates an enlive-snippet from `html-string` then removes
  the newline nodes"
  [html-string]
  (->> (enlive/html-snippet html-string)
       (walk/postwalk
         (fn [node]
           (if (seq? node)
             (remove #(and (string? %) (re-matches #"\n\h*" %)) node)
             node)))
       (walk/postwalk
         (fn [node]
           (if (= "listingblock" (get-in node [:attrs :class]))
             (let [pre (-> node :content first :content first)
                   n (first (:content pre))
                   lang (keyword (get-in n [:attrs :data-lang]))
                   content (-> (:content n)
                               (first)
                               (clygments/highlight lang :html))
                   n (list (assoc n :content (enlive/html-snippet content)))]
               (assoc node :content (list (assoc pre :content n))))
             node)))))

(alter-var-root
  #'util/trimmed-html-snippet
  (fn [_f] transform-html))

👍 1
NoahTheDuke21:08:28

(defn transform-html
  "Creates an enlive-snippet from `html-string` then removes
  the newline nodes"
  [html-string]
  (->> (enlive/html-snippet html-string)
       (walk/postwalk
         (fn [node]
           (if (seq? node)
             (remove #(and (string? %) (re-matches #"\n\h*" %)) node)
             node)))
       (walk/postwalk
         (fn [node]
           (if (= "listingblock" (get-in node [:attrs :class]))
             (let [pre (-> node :content first :content first)
                   n (first (:content pre))
                   lang (keyword (get-in n [:attrs :data-lang]))
                   content (-> (:content n)
                               (first)
                               (clygments/highlight lang :html))
                   n (list (assoc n :content (enlive/html-snippet content)))]
               (assoc node :content (list (assoc pre :content n))))
             node)))))

(alter-var-root
  #'util/trimmed-html-snippet
  (fn [_f] transform-html))

👍 1