Skip to content

Commit

Permalink
substitute into the template any matching yaml front matter entries
Browse files Browse the repository at this point in the history
  • Loading branch information
grahamcarlyle committed Nov 28, 2024
1 parent 6e91d1c commit 00165ad
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
17 changes: 13 additions & 4 deletions src/main/com/grahamcarlyle/blog/build.clj
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
(ns com.grahamcarlyle.blog.build
(:require
[babashka.fs :as fs]
[com.grahamcarlyle.blog :as-alias blog]
[com.grahamcarlyle.blog.page :as-alias page]
[com.grahamcarlyle.blog.posts :as posts]
[clojure.java.io :as io]
[clojure.walk :as walk]
[hiccup2.core :as h]))

(def post-template
[:html {:lang "en"}
[:head
[:title ::page/title]]
[:body
:blog.page/content]])
::page/content]])

(defn substitute [template mapping]
(walk/postwalk
Expand All @@ -20,14 +22,21 @@
x))
template))

(defn qualify-plain-keyword-keys [mapping ns-str]
(update-keys mapping
#(cond-> %
(and (keyword? %) (nil? (namespace %)))
(->> name (keyword ns-str)))))

(defn generate [{:keys [output-dir]}]
(fs/delete-tree output-dir)
(fs/create-dirs output-dir)
(let [result (posts/parse (slurp (io/file "posts" "first-scrappy.md")))]
(let [result (posts/parse (slurp (io/file "posts" "first-scrappy.md")))
mapping (merge {::page/content (:hiccup result)}
(qualify-plain-keyword-keys (:meta result) (namespace ::page/placeholder)))]
(spit (io/file output-dir "index.html")
(h/html (h/raw "<!DOCTYPE html>")
(substitute post-template {:blog.page/content (:hiccup result)})))))
(substitute post-template mapping)))))

(comment
(def output-dir "generated-output")
Expand Down
11 changes: 10 additions & 1 deletion src/test/com/grahamcarlyle/blog/build_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,13 @@

(deftest substitute-test
(is (= [:a "hello"]
(build/substitute [:a :foo] {:foo "hello"}))))
(build/substitute [:a :foo] {:foo "hello"}))))

(deftest qualify-plain-keyword-keys-test
(is (= {:e/a :b
:foo/c :d
"b" "a"}
(build/qualify-plain-keyword-keys {:a :b
:foo/c :d
"b" "a"}
"e"))))

0 comments on commit 00165ad

Please sign in to comment.