Skip to content

Instantly share code, notes, and snippets.

@martinklepsch
Last active November 30, 2023 02:42
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save martinklepsch/cdd68dba96799dba0266b8842ae00ce3 to your computer and use it in GitHub Desktop.
Save martinklepsch/cdd68dba96799dba0266b8842ae00ce3 to your computer and use it in GitHub Desktop.
A small babashka script to convert HTML from stdin to hiccup and pretty print it.

A small babashka script to convert HTML from stdin to hiccup and pretty print it.

I use this inside NeoVim with this config line

command PasteAsHiccup r !pbpaste | html2hiccup

And then whenever I want to paste some HTML as Hiccup I'll do :PasteAsHiccup.

This requires

  1. bb (Babashka)
  2. zprint
  3. The script below in your $PATH
#!/usr/bin/env bb
;; vim: ft=clojure
(require '[babashka.pods :as pods]
'[babashka.process :as p]
'[clojure.string :as string]
'[clojure.walk :as walk])
(pods/load-pod 'retrogradeorbit/bootleg "0.1.9")
(require '[pod.retrogradeorbit.bootleg.utils :as utils])
(def html
(apply str (map string/trim (string/split (slurp *in*) #"\\n" ))))
(def hiccup
(utils/convert-to html :hiccup))
(def cleaned-hiccup
(walk/postwalk
(fn [x]
(cond
(and (string? x) (string/blank? (string/trim x)))
nil
(string? x)
(string/trim x)
(and (coll? x) (not (map-entry? x)))
(into (empty x) (remove nil? x))
:else
x))
hiccup))
@(p/process {:in (pr-str cleaned-hiccup)
:out :inherit} "zprint")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment