Add initial working version with simple templating

This version supports copying over HTML files and including simple
static template files.
This commit is contained in:
Archie Hilton 2024-11-03 21:54:54 +00:00
parent 60bff98ac2
commit a7c9b1feff
2 changed files with 16 additions and 28 deletions

View File

@ -1,8 +1,7 @@
(ns website.core (ns website.core
(:require (:require
[clojure.java.io :as io] [clojure.java.io :as io]
[clojure.string :as string] [clojure.string :as string]))
))
(def template-reg #"#\{\{(.*)\}\}") (def template-reg #"#\{\{(.*)\}\}")
(def template-dir "./site/templates") (def template-dir "./site/templates")
@ -14,11 +13,8 @@
(defn generate-template-map [template-dir] (defn generate-template-map [template-dir]
"Grab all files in the template directory and insert them into a map with "Grab all files in the template directory and insert them into a map with
the filenames as keyword keys" the filenames as keyword keys"
(let [files (filter #(.isFile %) (file-seq (io/file template-dir))) (let [files (filter #(.isFile %) (file-seq (io/file template-dir)))]
] (reduce #(assoc %1 (keyword (.getName %2)) (slurp %2)) {} files)))
(reduce #(assoc %1 (keyword (.getName %2)) (slurp %2)) {} files)
)
)
; Generate a string-string map of the new filepaths and the unprocessed contents ; Generate a string-string map of the new filepaths and the unprocessed contents
(defn generate-output-file-map [html-dir, output-dir] (defn generate-output-file-map [html-dir, output-dir]
@ -32,17 +28,13 @@ the filenames as keyword keys"
contents (map slurp file-listing) contents (map slurp file-listing)
old-paths (map #(.getPath %) file-listing) old-paths (map #(.getPath %) file-listing)
rootless-paths (map #(subs % (count html-dir)) old-paths) rootless-paths (map #(subs % (count html-dir)) old-paths)
new-paths (map #(string/join [output-dir %]) rootless-paths) new-paths (map #(string/join [output-dir %]) rootless-paths)]
] (zipmap new-paths contents)))
(zipmap new-paths contents)
)
)
(defn put_file [file_map_entry] (defn put_file [file_map_entry]
"Output a file to a location while ensuring the required parent directories exist." "Output a file to a location while ensuring the required parent directories exist."
(io/make-parents (first file_map_entry)) (io/make-parents (first file_map_entry))
(spit (first file_map_entry) (second file_map_entry)) (spit (first file_map_entry) (second file_map_entry)))
)
(defn insert_templates [file-map-entry template-map] (defn insert_templates [file-map-entry template-map]
; Will go through and check all the template names in the file then ; Will go through and check all the template names in the file then
@ -53,10 +45,8 @@ the filenames as keyword keys"
file (first file-map-entry) file (first file-map-entry)
contents (second file-map-entry) contents (second file-map-entry)
templates-used (re-seq template-reg contents) templates-used (re-seq template-reg contents)
new-contents (reduce #(string/replace %1 (first %2) ((keyword (second %2)) template-map)) contents templates-used) new-contents (reduce #(string/replace %1 (first %2) ((keyword (second %2)) template-map)) contents templates-used)]
] [file new-contents]))
[file new-contents]
))
; Main routine ; Main routine
; Read all the templates ; Read all the templates

View File

@ -1,8 +1,6 @@
(ns website.main (ns website.main
(:require [website.core :as core]) (:require [website.core :as core])
(:gen-class) (:gen-class))
)
(defn -main [&args] (defn -main [&args]
(core/foo 1) (core/foo 1))
)