2022-10-27

Published: Nov 08, 2022Last modified: Apr 05, 2023
Word count: 242

org-blog failed better static clerk diffs attempt

I tried to write a function here that pretty-printed the raw state blob that is in-lined in the ->html static app build helper....

It's a mess, but I thought I'd leave it here....

(defn pp-js-str
  "Converts the passed state to a js expression
  that creates the stringified state.
  Supports better git-diffs for updated post content."
  [state]
  (-> state
      (->>
        (walk/postwalk (fn [form]
                         (if (and
                               (vector? form)
                               (= (count form) 2)
                               (= (first form) :nextjournal/edn))
                           (do
                             (println "stringifying substr")
                             [(first form)
                              (-> form second
                                  (zp/zprint-str
                                    {:parse-string? true
                                     :output        {:real-le? true}})
                                  pr-str
                                  ;; (string/split-lines)
                                  ;; (->> (map (fn [s]
                                  ;;             (str "\"" s "\" + \\")))
                                  ;;      ;; (string/replace "\n" "\n\" ++ \"")
                                  ;;      ;; (#(str " + " %  " + "))
                                  ;;      (string/join "\n"))
                                  )])
                           form))))
      (zp/zprint-str {:output {:real-le? true}})
      (string/split-lines)
      (->>
        (map (fn [s] (str "\"" s "\" + \\\n")))
        (map (fn [s]
               (if (re-seq #"\\\" \+ \\$" s)
                 (do
                   (println "replacing tail" s)
                   (def s s)
                   (string/replace s #"\\\" \+ \\\\" "\" + \\"))
                 s)))
        (map (fn [s]
               ;; (println s)
               (cond
                 ;; :nextjournal/expanded at line
                 (re-seq #"^\" +\"\\\"\{:n" s)
                 (do
                   (println "line to edit" s)
                   (string/replace-first s #"\"\\\"\{" "\" + \"\\\\\"{ "))

                 ;; end of nj/edn str
                 (re-seq #"\}\\\"\",\" \+ \\" s)
                 (do
                   (println "line to edit" s)
                   s
                   (string/replace-first s #"\}\\\"\",\"" "}\""))
                 :else s)
               )))
      ;; pr-str
      ;; (string/replace "\n" "\n\" + \"")
      ))