Skip to content

Commit

Permalink
Move formatting code to formatting namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
ErikSchierboom committed Jan 19, 2025
1 parent ad4007a commit e6500a6
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 25 deletions.
24 changes: 23 additions & 1 deletion generators/src/formatting.clj
Original file line number Diff line number Diff line change
@@ -1,5 +1,27 @@
(ns formatting
(:require [cljfmt.core :refer [reformat-string]]))
(:require [cljfmt.core :refer [reformat-string]]
[clojure.string :as str])
(:import [com.github.jknack.handlebars Formatter]))

(defn format-code [code]
(reformat-string code))

(defn format-string [s _next]
(str "\"" (str/escape s char-escape-string) "\""))

(defn format-collection [coll next open close]
(let [formatted-elements (str/join " " (map #(. next format %) coll))]
(str open formatted-elements close)))

(defn format-list [coll next]
(format-collection coll next "'(" ")"))

(defn format-set [coll next]
(format-collection coll next "#{" "}"))

(defn formatter [test conv]
(proxy [Formatter] []
(format [value next]
(if (test value)
(conv value next)
(. next format value)))))
28 changes: 4 additions & 24 deletions generators/src/templates.clj
Original file line number Diff line number Diff line change
Expand Up @@ -6,33 +6,13 @@
[log]
[paths]
[formatting])
(:import [com.github.jknack.handlebars Formatter EscapingStrategy]))

(defn format-string [s _next]
(str "\"" (str/escape s char-escape-string) "\""))

(defn format-collection [coll next open close]
(let [formatted-elements (str/join " " (map #(. next format %) coll))]
(str open formatted-elements close)))

(defn format-list [coll next]
(format-collection coll next "'(" ")"))

(defn format-set [coll next]
(format-collection coll next "#{" "}"))

(defn formatter [test conv]
(proxy [Formatter] []
(format [value next]
(if (test value)
(conv value next)
(. next format value)))))
(:import [com.github.jknack.handlebars EscapingStrategy]))

(def reg
(-> *hbs*
(. with (formatter set? format-set))
(. with (formatter list? format-list))
(. with (formatter string? format-string))
(. with (formatting/formatter set? formatting/format-set))
(. with (formatting/formatter list? formatting/format-list))
(. with (formatting/formatter string? formatting/format-string))
(. with EscapingStrategy/NOOP)))

(defhelper ifzero [ctx options]
Expand Down

0 comments on commit e6500a6

Please sign in to comment.