Compare commits
No commits in common. "6ef987639d1e1981ec5ba841b5fe0e6603097d42" and "ba95015e26de4388a6263e1ffac9e25e1b2108c8" have entirely different histories.
6ef987639d
...
ba95015e26
|
@ -11,8 +11,3 @@ pom.xml.asc
|
||||||
/.prepl-port
|
/.prepl-port
|
||||||
.hgignore
|
.hgignore
|
||||||
.hg/
|
.hg/
|
||||||
.clj-kondo/*
|
|
||||||
.lsp/*
|
|
||||||
|
|
||||||
# Project specific ignores
|
|
||||||
out/
|
|
||||||
|
|
|
@ -7,5 +7,4 @@
|
||||||
[hiccup "2.0.0-RC3"]]
|
[hiccup "2.0.0-RC3"]]
|
||||||
:repl-options {:init-ns website.core}
|
:repl-options {:init-ns website.core}
|
||||||
:main website.main
|
:main website.main
|
||||||
:plugins [[dev.weavejester/lein-cljfmt "0.13.0"]]
|
|
||||||
)
|
)
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
<!DOCTYPE html>
|
<!DOCTYPE html>
|
||||||
<html lang="en">
|
<html lang="en">
|
||||||
<head>
|
<head>
|
||||||
<meta charset="UTF-8">
|
<meta charset="UTF-8">
|
||||||
|
@ -11,6 +11,6 @@
|
||||||
<p>This is a paragraph.</p>
|
<p>This is a paragraph.</p>
|
||||||
<p>This is another paragraph.</p>
|
<p>This is another paragraph.</p>
|
||||||
</div>
|
</div>
|
||||||
#{{footer.template.html}}
|
#{{footer}}
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
|
@ -1,68 +1,19 @@
|
||||||
(ns website.core
|
(ns website.core
|
||||||
(:require
|
(:require [clojure.java.io :as io]))
|
||||||
[clojure.java.io :as io]
|
|
||||||
[clojure.string :as string]))
|
|
||||||
|
|
||||||
(def template-reg #"#\{\{(.*)\}\}")
|
|
||||||
(def template-dir "./site/templates")
|
(def template-dir "./site/templates")
|
||||||
(def html-dir "./site/html")
|
|
||||||
(def output-dir "./out")
|
|
||||||
|
|
||||||
;; Thank you https://clojure.org/api/cheatsheet
|
;; Thank you https://clojure.org/api/cheatsheet
|
||||||
; Generate a keyword-string map of the templates folder
|
(def template-map
|
||||||
(defn generate-template-map
|
|
||||||
"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"
|
||||||
[template-dir]
|
(let [files (filter #(.isFile %) (file-seq (io/file template-dir)))
|
||||||
(let [files (filter #(.isFile %) (file-seq (io/file template-dir)))]
|
] (mapcat #(assoc {} (keyword (.getName %)) (slurp %)) files)))
|
||||||
(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 put_file
|
(defn foo
|
||||||
"Output a file to a location while ensuring the required parent directories exist."
|
"I don't do a whole lot."
|
||||||
[file_map_entry]
|
[x]
|
||||||
(io/make-parents (first file_map_entry))
|
(println x "Hello, World!"))
|
||||||
(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)
|
|
||||||
)
|
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
(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)
|
||||||
|
)
|
||||||
|
|
Loading…
Reference in New Issue