Compare commits

..

7 Commits

Author SHA1 Message Date
Archie Hilton 6ef987639d Fix docstrings in core.clj 2024-11-03 22:38:22 +00:00
Archie Hilton 8daae7ae22 Update .gitignore 2024-11-03 22:37:51 +00:00
Archie Hilton a7c9b1feff Add initial working version with simple templating
This version supports copying over HTML files and including simple
static template files.
2024-11-03 21:54:54 +00:00
Archie Hilton 60bff98ac2 Add output directory to .gitignore 2024-11-03 21:54:37 +00:00
Archie Hilton 98f10abaaf Add cljfmt to format code 2024-11-03 21:50:07 +00:00
Archie Hilton c4b85808b9 Fix up template naming in index.html; remove space 2024-11-03 21:47:42 +00:00
Archie Hilton 27e023fb73 Rename index.template.html to index.html
Think this makes more sense for pages which are actually pages.
2024-10-15 00:01:31 +01:00
5 changed files with 68 additions and 15 deletions

5
.gitignore vendored
View File

@ -11,3 +11,8 @@ pom.xml.asc
/.prepl-port
.hgignore
.hg/
.clj-kondo/*
.lsp/*
# Project specific ignores
out/

View File

@ -7,4 +7,5 @@
[hiccup "2.0.0-RC3"]]
:repl-options {:init-ns website.core}
:main website.main
:plugins [[dev.weavejester/lein-cljfmt "0.13.0"]]
)

View File

@ -11,6 +11,6 @@
<p>This is a paragraph.</p>
<p>This is another paragraph.</p>
</div>
#{{footer}}
#{{footer.template.html}}
</body>
</html>

View File

@ -1,19 +1,68 @@
(ns website.core
(:require [clojure.java.io :as io]))
(:require
[clojure.java.io :as io]
[clojure.string :as string]))
(def template-reg #"#\{\{(.*)\}\}")
(def template-dir "./site/templates")
(def html-dir "./site/html")
(def output-dir "./out")
;; Thank you https://clojure.org/api/cheatsheet
(def template-map
; Generate a keyword-string map of the templates folder
(defn generate-template-map
"Grab all files in the template directory and insert them into a map with
the filenames as keyword keys"
(let [files (filter #(.isFile %) (file-seq (io/file template-dir)))
] (mapcat #(assoc {} (keyword (.getName %)) (slurp %)) files)))
[template-dir]
(let [files (filter #(.isFile %) (file-seq (io/file template-dir)))]
(reduce #(assoc %1 (keyword (.getName %2)) (slurp %2)) {} files)))
; Generate a string-string map of the new filepaths and the unprocessed contents
(defn generate-output-file-map
"Take the input html files, read them, and create a mapping of their paths to
their contents.
This would probably be better as a list of maps each containing a :contents
and :path keyword.
"
[html-dir, output-dir]
(let [file-listing (filter #(.isFile %) (file-seq (io/file html-dir)))
contents (map slurp file-listing)
old-paths (map #(.getPath %) file-listing)
rootless-paths (map #(subs % (count html-dir)) old-paths)
new-paths (map #(string/join [output-dir %]) rootless-paths)]
(zipmap new-paths contents)))
(defn foo
"I don't do a whole lot."
[x]
(println x "Hello, World!"))
(defn put_file
"Output a file to a location while ensuring the required parent directories exist."
[file_map_entry]
(io/make-parents (first file_map_entry))
(spit (first file_map_entry) (second file_map_entry)))
(defn insert_templates
"Go through and check all the template names in the file then
mark which templates exist and which dont
Using this we can report an error for any templates which do not exist"
[file-map-entry template-map]
(let [file (first file-map-entry)
contents (second file-map-entry)
templates-used (re-seq template-reg contents)
new-contents (reduce #(string/replace %1 (first %2) ((keyword (second %2)) template-map)) contents templates-used)]
[file new-contents]))
; Main routine
; Read all the templates
(def template-map (generate-template-map template-dir))
; Read all the html files
(def output-file-map (generate-output-file-map html-dir output-dir))
; For each html file:
; * Insert any matching templates
; * Stick it in the out directory
(for [of output-file-map]
; of is a key-value pair
(put_file (insert_templates of template-map))
;(put_file of)
)

View File

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