-
Notifications
You must be signed in to change notification settings - Fork 61
/
config.clj
78 lines (71 loc) · 3.23 KB
/
config.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
(ns cryogen-core.config
(:require [clojure.string :as string]
[schema.core :as s]
[cryogen-core.schemas :as schemas]
[cryogen-core.io :as cryogen-io]))
(defn subpath?
"Checks if either path is a subpath of the other"
[p1 p2]
(let [parts #(string/split % #"/")]
(every? #{true} (map #(= %1 %2) (parts p1) (parts p2)))))
(defn root-uri
"Creates the uri for posts and pages. Returns root-path by default"
[k config]
(if-let [uri (k config)]
uri
(config (-> k (name) (string/replace #"-uri$" "") (keyword)))))
(defn process-config
"Reads the config file"
[config]
(try
(s/validate schemas/Config config)
(let [config (-> config
(update-in [:tag-root-uri] (fnil identity ""))
(update-in [:public-dest] (fnil identity "public"))
(update-in [:recent-posts] (fnil identity 3))
(update-in [:archive-group-format] (fnil identity "yyyy MMMM"))
(update-in [:sass-src] (fnil identity ["css"]))
(update-in [:sass-path] (fnil identity "sass"))
(update-in [:posts-per-page] (fnil identity 5))
(update-in [:blocks-per-preview] (fnil identity 2))
(assoc :page-root-uri (root-uri :page-root-uri config)
:post-root-uri (root-uri :post-root-uri config)))
check-overlap (fn [dirs]
(some #(subpath? % (:public-dest config)) dirs))]
(if (or (= (string/trim (:public-dest config)) "")
(string/starts-with? (:public-dest config) ".")
(check-overlap ["content" "themes" "src" "target"]))
(throw (new Exception "Dangerous :public-dest value. The folder will be deleted each time the content is rendered. Specify a sub-folder that doesn't overlap with the default folders or your resource folders."))
config))
(catch Exception e (throw e))))
(defn deep-merge
"Recursively merges maps. When override is true, for scalars and vectors,
the last value wins. When override is false, vectors are merged, but for
scalars, the last value still wins."
[override & vs]
(cond
(= (count vs) 1) vs
(every? map? vs) (apply merge-with (partial deep-merge override) vs)
(and (not override) (every? sequential? vs)) (apply into vs)
:else (last vs)))
(defn read-config []
(let [config (-> "content/config.edn"
cryogen-io/get-resource
cryogen-io/read-edn-resource)
theme-config-resource (-> config
:theme
(#(cryogen-io/path "themes" % "config.edn"))
cryogen-io/get-resource)
theme-config (if theme-config-resource
(cryogen-io/read-edn-resource theme-config-resource))]
(assoc config
:theme-resources
(or (:resources theme-config) [])
:theme-sass-src
(or (:sass-src theme-config) []))))
(defn resolve-config
"Loads the config file, merging in the overrides and, and filling in missing defaults"
([]
(resolve-config {}))
([overrides]
(process-config (deep-merge true (read-config) overrides))))