From e6500a6117bdd35c3bfe1c1cf42f32243403051a Mon Sep 17 00:00:00 2001 From: Erik Schierboom Date: Sun, 19 Jan 2025 16:06:30 +0100 Subject: [PATCH] Move formatting code to formatting namespace --- generators/src/formatting.clj | 24 +++++++++++++++++++++++- generators/src/templates.clj | 28 ++++------------------------ 2 files changed, 27 insertions(+), 25 deletions(-) diff --git a/generators/src/formatting.clj b/generators/src/formatting.clj index c9982bea..3c8db6f8 100644 --- a/generators/src/formatting.clj +++ b/generators/src/formatting.clj @@ -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))))) diff --git a/generators/src/templates.clj b/generators/src/templates.clj index df443b85..fd9830b7 100644 --- a/generators/src/templates.clj +++ b/generators/src/templates.clj @@ -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]