Skip to content

Commit

Permalink
use a template to render a post
Browse files Browse the repository at this point in the history
  • Loading branch information
grahamcarlyle committed Nov 28, 2024
1 parent 1fddb75 commit 6e91d1c
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
20 changes: 19 additions & 1 deletion src/main/com/grahamcarlyle/blog/build.clj
Original file line number Diff line number Diff line change
@@ -1,15 +1,33 @@
(ns com.grahamcarlyle.blog.build
(:require
[babashka.fs :as fs]
[com.grahamcarlyle.blog :as-alias blog]
[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"}
[:body
:blog.page/content]])

(defn substitute [template mapping]
(walk/postwalk
(fn [x]
(if (keyword? x)
(get mapping x x)
x))
template))


(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")))]
(spit (io/file output-dir "index.html") (h/html (:hiccup result)))))
(spit (io/file output-dir "index.html")
(h/html (h/raw "<!DOCTYPE html>")
(substitute post-template {:blog.page/content (:hiccup result)})))))

(comment
(def output-dir "generated-output")
Expand Down
9 changes: 9 additions & 0 deletions src/test/com/grahamcarlyle/blog/build_test.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
(ns com.grahamcarlyle.blog.build-test
(:require
[clojure.test :refer [deftest is]]
[com.grahamcarlyle.blog.build :as build]))


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

0 comments on commit 6e91d1c

Please sign in to comment.