Skip to content

Commit

Permalink
Update core.clj
Browse files Browse the repository at this point in the history
I like more spacing what can I say
  • Loading branch information
Asko Nõmm committed Apr 17, 2022
1 parent ab92eef commit 8c8ea6e
Showing 1 changed file with 28 additions and 2 deletions.
30 changes: 28 additions & 2 deletions src/bruno/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,25 @@
(def ^:dynamic *target-directory* nil)
(declare sci-opts)


(defn triml
"Trims the given `trim-char` from the left of `input`."
[input trim-char]
(if (string/starts-with? input trim-char)
(recur (subs input 1)
(triml (subs input 1)
trim-char)
input))


(defn trimr
"Trims away the given `trim-char` from the right of `input`."
[input trim-char]
(if (string/ends-with? input trim-char)
(recur (subs input 0 (- (count input) 1))
(trimr (subs input 0 (- (count input) 1))
trim-char)
input))


(defn scan
"Scans a given `directory` for any and all files (recursively)
and returns a list of maps containing the path of a file and
Expand All @@ -55,6 +58,7 @@
flatten
vec))


(defn- parse-md-metadata-line
"Parses each YAML metadata line into a map of k:v."
[line]
Expand All @@ -68,6 +72,7 @@
{(keyword meta-key)
meta-value}))


(defn parse-md-metadata
"Takes in a given `content` as the entirety of a Markdown
content file, and then parses YAML metadata from it."
Expand All @@ -77,6 +82,7 @@
(into {} (map #(parse-md-metadata-line %) lines)))
{}))


(defn parse-md-entry
"Takes in a given `content` as the entirety of a Markdown
content file, and then parses the Markdown into HTML from it."
Expand All @@ -86,6 +92,7 @@
(string/trim)
(clarktown/render)))


(defn slug-from-path
"Takes in a full `path` to a file and returns the relative URL slug
from it."
Expand All @@ -96,6 +103,7 @@
(string/split #"\.")
first)))


(defn get-content-items
"Gets a collection of content items."
([]
Expand All @@ -109,6 +117,7 @@
(->> (scan directory)
(filter #(string/ends-with? (:path %) ".md"))))))


(defn get-pages
"Gets a collection of pages."
[]
Expand All @@ -121,6 +130,7 @@
(filter #(or (= (:file-ext %) "html.clj")
(= (:file-ext %) "xml.clj"))))))


(defn get-layouts
"Gets a collection of layouts."
[]
Expand All @@ -132,13 +142,15 @@
(->> (scan (str *src-directory* File/separatorChar "_layouts"))
(filter #(string/ends-with? (:path %) ".clj")))))


(defn- get-current-dir
"Returns the current working directory."
[]
(-> (:out (sh/sh "pwd"))
(string/replace "\n" "")
string/trim))


(defn- get-src-dir
"Gets the source directory."
[]
Expand All @@ -147,12 +159,14 @@
(str current-dir File/separatorChar "src")
current-dir)))


(defn- get-target-dir
"Gets teh target directory."
[]
(let [current-dir (get-current-dir)]
(str current-dir File/separatorChar "public")))


(defn load-partial
"Renders a template partial."
([name]
Expand All @@ -166,13 +180,15 @@
(h/html (sci/eval-string partial (merge-with into sci-opts
{:bindings local-bindings}))))))


(defn document
"Wraps `contents` within a valid HTML document."
[opts & contents]
(if-not (map? opts)
(hpage/html5 {} opts contents)
(hpage/html5 opts contents)))


(defn content-composer
"Composes data sets from available Markdown files."
[opts]
Expand All @@ -186,6 +202,7 @@
; group by
(:group-by opts) (group-by (:group-by opts))))


(defn format-date
"Format given `date` string according to `format`.
Optionally takes in `timezone` as the third argument,
Expand All @@ -209,6 +226,7 @@
(println (.getMessage e))
""))))


(def sci-opts
{:bindings {'document document
'include-js hpage/include-js
Expand All @@ -218,6 +236,7 @@
'format-date format-date}
:namespaces {'clojure.string {'split string/split}}})


(defn build-content-items!
"Builds all the content items with the layout specified
in the individual content items, or with the \"default\"
Expand All @@ -243,6 +262,7 @@
(io/make-parents write-path)
(spit write-path html)))))


(defn build-pages!
"Builds all the pages."
[]
Expand All @@ -261,12 +281,14 @@
(io/make-parents write-path)
(spit write-path html))))


(defn empty-public-dir!
"Deletes all files and folders from the `*target-directory*`."
[]
(doseq [{:keys [path]} (scan *target-directory*)]
(io/delete-file path)))


(defn copy-assets!
"Copies all assets to the `*target-directory*`."
[]
Expand All @@ -283,6 +305,7 @@
(io/make-parents to-path)
(io/copy from to))))


(defn build!
"Builds the static site in `*src-directory*`."
[]
Expand All @@ -291,6 +314,7 @@
(build-content-items!)
(build-pages!))


(defn watch!
"Runs an infinite loop that checks every 1s for any changes
to files, upon which it will call `(build!)`."
Expand All @@ -304,6 +328,7 @@
(recur new-watch-list
(scan *src-directory*))))


(defn argument
"Parses a given list of `args` for a `command` and returns
`true` if the command was found. If the command has a
Expand All @@ -317,6 +342,7 @@
true)
nil))))


(defn -main [& args]
(println "Thinking ...")
(alter-var-root #'*src-directory* (constantly (get-src-dir)))
Expand Down

0 comments on commit 8c8ea6e

Please sign in to comment.