create dirs from md works

This commit is contained in:
jem 2019-12-20 15:55:53 +01:00
parent 399ed2d8ec
commit 8ecdb8c3c6
14 changed files with 338 additions and 306 deletions

View file

@ -23,12 +23,12 @@
(def File s/Any) ; java.io.File (def File s/Any) ; java.io.File
(def Resource (def Resource
{:path Path {:path Path
:uri Uri :uri Uri
:file File :file File
:source-type SourceType :source-type SourceType
:resource-type ResourceType }) :resource-type ResourceType})
(def public "resources/public") (def public "resources/public")
@ -62,7 +62,7 @@
:uri (.toURI file) :uri (.toURI file)
:file file :file file
:source-type source-type :source-type source-type
:resource-type (cond :resource-type (cond
(.isDirectory file) :dir (.isDirectory file) :dir
(.isFile file) :file (.isFile file) :file
:else :unknown)})) :else :unknown)}))
@ -106,7 +106,7 @@
file-from-cp (if from-cp file-from-cp (if from-cp
(file-from-cp full-path) (file-from-cp full-path)
nil)] nil)]
(cond (cond
(some? file-from-fs) (some? file-from-fs)
(create-resource resource-path file-from-fs :filesystem) (create-resource resource-path file-from-fs :filesystem)
(some? file-from-cp) (some? file-from-cp)
@ -145,7 +145,7 @@
path-to-work-with path-to-work-with
:from-cp from-cp :from-cp from-cp
:from-fs from-fs) :from-fs from-fs)
result (into result result (into result
[resource-to-work-with])] [resource-to-work-with])]
(cond (cond
(nil? resource-to-work-with) [] (nil? resource-to-work-with) []
@ -165,18 +165,17 @@
& {:keys [from-cp from-fs] & {:keys [from-cp from-fs]
:or {from-cp true :or {from-cp true
from-fs true}}] from-fs true}}]
(map #(:path %) (map #(:path %)
(get-resources-recursive (get-resources-recursive
fs-prefix base-path paths fs-prefix base-path paths
:from-cp from-cp :from-cp from-cp
:from-fs from-fs)) :from-fs from-fs)))
)
; TODO: Add files to keep ; TODO: Add files to keep
(s/defn delete-resource-recursive! (s/defn delete-resource-recursive!
[path :- s/Str] [path :- s/Str]
(let [resource-paths (let [resource-paths
(reverse (get-resource-paths-recursive (reverse (get-resource-paths-recursive
"" path [""] :from-cp false))] "" path [""] :from-cp false))]
(doseq [resource-path resource-paths] (doseq [resource-path resource-paths]
(io/delete-file (str path resource-path))))) (io/delete-file (str path resource-path)))))
@ -191,18 +190,18 @@
] ]
(let [resource-paths (let [resource-paths
(get-resource-paths-recursive fs-prefix base-path source-paths)] (get-resource-paths-recursive fs-prefix base-path source-paths)]
(if (empty? resource-paths) (if (empty? resource-paths)
(throw (IllegalArgumentException. (str "resource " resource-paths ", " (throw (IllegalArgumentException. (str "resource " resource-paths ", "
source-paths " not found"))) source-paths " not found")))
(doseq [resource-path resource-paths] (doseq [resource-path resource-paths]
(let [target-file (io/file target-path resource-path) (let [target-file (io/file target-path resource-path)
source-file (io/file (file-from-cp-or-fs source-file (io/file (file-from-cp-or-fs
fs-prefix fs-prefix
base-path base-path
resource-path))] resource-path))]
(io/make-parents target-file) (io/make-parents target-file)
(when (.isFile source-file) (when (.isFile source-file)
(io/copy source-file target-file))))))) (io/copy source-file target-file)))))))
(defn copy-resources-from-user! (defn copy-resources-from-user!
[fs-prefix resources target-path ignore-patterns] [fs-prefix resources target-path ignore-patterns]
@ -210,10 +209,39 @@
(copy-resources! fs-prefix resource-path resources (copy-resources! fs-prefix resource-path resources
target-path ignore-patterns))) target-path ignore-patterns)))
(defn copy-resources-from-theme! (defn copy-resources-from-theme!
[fs-prefix theme target-path ignore-patterns] [fs-prefix theme target-path ignore-patterns]
(let [theme-path (str "templates/themes/" theme)] (let [theme-path (str "templates/themes/" theme)]
(copy-resources! fs-prefix theme-path ["css" "js"] (copy-resources! fs-prefix theme-path ["css" "js"]
target-path ignore-patterns) target-path ignore-patterns)
(copy-resources! fs-prefix (str theme-path "/html") ["404.html"] (copy-resources! fs-prefix (str theme-path "/html") ["404.html"]
target-path ignore-patterns))) target-path ignore-patterns)))
(defn distinct-resources-by-path
[resources]
(loop [paths (set (map :path resources))
resources resources
acc []]
(cond (empty? resources) acc
(contains? paths (:path (first resources))) (recur (disj paths (:path (first resources)))
(rest resources)
(conj acc (first resources)))
:else (recur paths (rest resources) acc))))
(defn get-distinct-markup-dirs
[fs-prefix posts pages ignore-patterns]
(let [base-path "templates/md"
resources (get-resources-recursive
fs-prefix base-path [pages posts])
filtered-resources (->> (filter #(= (:resource-type %) :dir) resources)
(distinct-resources-by-path))]
filtered-resources))
(defn create-dirs-from-markup-folders!
"Copy resources from markup folders. This does not copy the markup entries."
[fs-prefix posts pages target-path ignore-patterns]
(let [resources (get-distinct-markup-dirs fs-prefix posts pages
ignore-patterns)]
(doseq [resource resources]
(io/make-parents (io/file (str target-path "/" (:path resource))))
(.mkdir (io/file (str target-path "/" (:path resource)))))))

View file

@ -494,37 +494,37 @@
author-root-uri theme debug? page-model author-root-uri theme debug? page-model
page-root-uri resources] page-root-uri resources]
:as config} (read-config) :as config} (read-config)
posts (map klipsify (add-prev-next (read-posts config))) posts (map klipsify (add-prev-next (read-posts config)))
posts-by-tag (group-by-tags posts) posts-by-tag (group-by-tags posts)
posts (tag-posts posts config) posts (tag-posts posts config)
latest-posts (->> posts (take recent-posts) vec) latest-posts (->> posts (take recent-posts) vec)
klipsified-pages (map klipsify (read-pages config)) klipsified-pages (map klipsify (read-pages config))
modelled-pages (cond modelled-pages (cond
(= page-model :flat) klipsified-pages (= page-model :flat) klipsified-pages
(= page-model :hierarchic) (hierarchic/build-hierarchic-map page-root-uri klipsified-pages)) (= page-model :hierarchic) (hierarchic/build-hierarchic-map page-root-uri klipsified-pages))
home-page (->> modelled-pages home-page (->> modelled-pages
(filter #(boolean (:home? %))) (filter #(boolean (:home? %)))
(first)) (first))
other-pages (->> modelled-pages other-pages (->> modelled-pages
(remove #{home-page}) (remove #{home-page})
(add-prev-next)) (add-prev-next))
params (merge config params (merge config
{:today (java.util.Date.) {:today (java.util.Date.)
:title (:site-title config) :title (:site-title config)
:active-page "home" :active-page "home"
:tags (map (partial tag-info config) (keys posts-by-tag)) :tags (map (partial tag-info config) (keys posts-by-tag))
:latest-posts latest-posts :latest-posts latest-posts
:pages other-pages :pages other-pages
:home-page (if home-page :home-page (if home-page
home-page home-page
(assoc (first latest-posts) :layout "home.html")) (assoc (first latest-posts) :layout "home.html"))
:archives-uri (page-uri "archives.html" config) :archives-uri (page-uri "archives.html" config)
:index-uri (page-uri "index.html" config) :index-uri (page-uri "index.html" config)
:tags-uri (page-uri "tags.html" config) :tags-uri (page-uri "tags.html" config)
:rss-uri (cryogen-io/path "/" blog-prefix rss-name) :rss-uri (cryogen-io/path "/" blog-prefix rss-name)
:site-url (if (.endsWith site-url "/") (.substring site-url 0 (dec (count site-url))) site-url)}) :site-url (if (.endsWith site-url "/") (.substring site-url 0 (dec (count site-url))) site-url)})
file-resource-path (str "file:resources/templates/themes/" theme) file-resource-path (str "file:resources/templates/themes/" theme)
classpath-resource-path (str "templates/themes/" theme)] classpath-resource-path (str "templates/themes/" theme)]
(when debug? (when debug?
(println (blue "debug: page-model:")) (println (blue "debug: page-model:"))
(println "\t-->" (cyan page-model)) (println "\t-->" (cyan page-model))
@ -548,7 +548,12 @@
ignored-files) ignored-files)
;TODO: replace this ;TODO: replace this
; Nur directories kopieren ; Nur directories kopieren
(copy-resources-from-markup-folders config) ;(copy-resources-from-markup-folders config)
(cp-io/create-dirs-from-markup-folders! "resources/"
(:posts config)
(:pages config)
(cp-io/path "resources/public" blog-prefix)
ignored-files)
(compile-pages params modelled-pages) (compile-pages params modelled-pages)
(compile-posts params posts) (compile-posts params posts)
(compile-tags params posts-by-tag) (compile-tags params posts-by-tag)

View file

@ -1,39 +1,36 @@
(ns cryogen-core.hierarchic (ns cryogen-core.hierarchic
(:require (:require
[clojure.string :as s])) [clojure.string :as s]))
(defn normalized-page-root-uri [page-root-uri] (defn normalized-page-root-uri [page-root-uri]
(cond (cond
(= "" page-root-uri) "" (= "" page-root-uri) ""
(and (and
(s/starts-with? page-root-uri "/") (s/starts-with? page-root-uri "/")
(s/ends-with? page-root-uri "/")) page-root-uri (s/ends-with? page-root-uri "/")) page-root-uri
(and (and
(s/starts-with? page-root-uri "/") (s/starts-with? page-root-uri "/")
(not (s/ends-with? page-root-uri "/"))) (str page-root-uri "/") (not (s/ends-with? page-root-uri "/"))) (str page-root-uri "/")
(and (and
(not (s/starts-with? page-root-uri "/")) (not (s/starts-with? page-root-uri "/"))
(s/ends-with? page-root-uri "/")) (str "/" page-root-uri) (s/ends-with? page-root-uri "/")) (str "/" page-root-uri)
(and (and
(not (s/starts-with? page-root-uri "/")) (not (s/starts-with? page-root-uri "/"))
(not (s/ends-with? page-root-uri "/"))) (str "/" page-root-uri "/")) (not (s/ends-with? page-root-uri "/"))) (str "/" page-root-uri "/")))
)
(defn uri-level [uri] (defn uri-level [uri]
(- (count (- (count
(s/split uri #"/")) (s/split uri #"/"))
1) 1))
)
(defn filter-pages-for-uri [uri pages] (defn filter-pages-for-uri [uri pages]
(let [html? (s/ends-with? uri ".html") (let [html? (s/ends-with? uri ".html")
clean? (s/ends-with? uri "/") clean? (s/ends-with? uri "/")
clean-uri (cond clean-uri (cond
html? (subs uri 0 (- (count uri) 5)) html? (subs uri 0 (- (count uri) 5))
clean? (subs uri 0 (- (count uri) 1)) clean? (subs uri 0 (- (count uri) 1))
:default uri)] :default uri)]
(filter #(s/starts-with? (:uri %) clean-uri) pages)) (filter #(s/starts-with? (:uri %) clean-uri) pages)))
)
(defn build-hierarchic-level (defn build-hierarchic-level
"builds one level of hierarchic tree recurs to next level." "builds one level of hierarchic tree recurs to next level."
@ -41,20 +38,17 @@
(let [current-level (+ 1 (uri-level parent-uri)) (let [current-level (+ 1 (uri-level parent-uri))
pages-of-parent (filter-pages-for-uri parent-uri pages) pages-of-parent (filter-pages-for-uri parent-uri pages)
pages-on-level (filter #(= current-level (uri-level (:uri %))) pages-of-parent) pages-on-level (filter #(= current-level (uri-level (:uri %))) pages-of-parent)
pages-on-child-level (filter #(< current-level (uri-level (:uri %))) pages-of-parent) pages-on-child-level (filter #(< current-level (uri-level (:uri %))) pages-of-parent)]
]
(sort-by :page-index (sort-by :page-index
(map #(let [page-on-level % (map #(let [page-on-level %
child-pages (filter-pages-for-uri (:uri page-on-level) pages-on-child-level)] child-pages (filter-pages-for-uri (:uri page-on-level) pages-on-child-level)]
(if (empty? child-pages) (if (empty? child-pages)
page-on-level page-on-level
(merge page-on-level (merge page-on-level
{:children (build-hierarchic-level (:uri page-on-level) child-pages)}))) pages-on-level)) {:children (build-hierarchic-level (:uri page-on-level) child-pages)}))) pages-on-level))))
))
(defn build-hierarchic-map (defn build-hierarchic-map
"builds a hierarchic tree from pages" "builds a hierarchic tree from pages"
[page-root-uri pages] [page-root-uri pages]
(let [sorted-pages (sort-by :uri pages)] (let [sorted-pages (sort-by :uri pages)]
(build-hierarchic-level (normalized-page-root-uri page-root-uri) sorted-pages) (build-hierarchic-level (normalized-page-root-uri page-root-uri) sorted-pages)))
))

View file

@ -6,7 +6,7 @@
(def public "resources/public") (def public "resources/public")
(defn path (defn path
"Creates path from given parts, ignore empty elements" "Creates path from given parts, ignore empty elements"
[& path-parts] [& path-parts]
(->> path-parts (->> path-parts
(remove s/blank?) (remove s/blank?)
@ -88,7 +88,7 @@
[config] [config]
(let [theme-path (str "themes/" (:theme config))] (let [theme-path (str "themes/" (:theme config))]
(copy-resources (copy-resources
(merge config (merge config
{:resources [(str theme-path "/css") {:resources [(str theme-path "/css")
(str theme-path "/js") (str theme-path "/js")
(str theme-path "/html/404.html")]})))) (str theme-path "/html/404.html")]}))))

View file

@ -62,9 +62,9 @@
[html settings] [html settings]
(letfn [(tag [h clas] (letfn [(tag [h clas]
(enlive/sniptest h (enlive/sniptest h
[(keyword (str "code" clas))] [(keyword (str "code" clas))]
(fn [x] (fn [x]
(update-in x [:attrs :class] #(str % " nohighlight")))))] (update-in x [:attrs :class] #(str % " nohighlight")))))]
(reduce tag html (eval-classes settings)))) (reduce tag html (eval-classes settings))))
(def defaults (def defaults

View file

@ -4,30 +4,29 @@
[cryogen-core.io :as cryogen-io]) [cryogen-core.io :as cryogen-io])
(:import java.util.Date)) (:import java.util.Date))
(defn posts-to-items [^String site-url posts] (defn posts-to-items [^String site-url posts]
(map (map
(fn [{:keys [uri title content date enclosure author description]}] (fn [{:keys [uri title content date enclosure author description]}]
(let [link (str (if (.endsWith site-url "/") (apply str (butlast site-url)) site-url) uri) (let [link (str (if (.endsWith site-url "/") (apply str (butlast site-url)) site-url) uri)
enclosure (if (nil? enclosure) "" enclosure)] enclosure (if (nil? enclosure) "" enclosure)]
{:guid link {:guid link
:link link :link link
:title title :title title
:description (or description content) :description (or description content)
:author author :author author
:enclosure enclosure :enclosure enclosure
:pubDate date})) :pubDate date}))
posts)) posts))
(defn make-channel [config posts] (defn make-channel [config posts]
(apply (apply
(partial rss/channel-xml (partial rss/channel-xml
false false
{:title (:site-title config) {:title (:site-title config)
:link (:site-url config) :link (:site-url config)
:description (:description config) :description (:description config)
:lastBuildDate (Date.)}) :lastBuildDate (Date.)})
(posts-to-items (:site-url config) posts))) (posts-to-items (:site-url config) posts)))
(defn make-filtered-channels [{:keys [rss-filters blog-prefix] :as config} posts-by-tag] (defn make-filtered-channels [{:keys [rss-filters blog-prefix] :as config} posts-by-tag]
(doseq [filter rss-filters] (doseq [filter rss-filters]

View file

@ -16,13 +16,13 @@
(defn generate [site-url ignored-files] (defn generate [site-url ignored-files]
(with-out-str (with-out-str
(emit (emit
{:tag :urlset {:tag :urlset
:attrs {:xmlns "http://www.sitemaps.org/schemas/sitemap/0.9"} :attrs {:xmlns "http://www.sitemaps.org/schemas/sitemap/0.9"}
:content :content
(for [^java.io.File f (cryogen-io/find-assets "public" ".html" ignored-files)] (for [^java.io.File f (cryogen-io/find-assets "public" ".html" ignored-files)]
{:tag :url {:tag :url
:content :content
[{:tag :loc [{:tag :loc
:content [(str site-url (loc f))]} :content [(str site-url (loc f))]}
{:tag :lastmod {:tag :lastmod
:content [(-> f (.lastModified) (Date.) format-date)]}]})}))) :content [(-> f (.lastModified) (Date.) format-date)]}]})})))

View file

@ -12,13 +12,13 @@
headings." headings."
[content] [content]
(reduce (reduce
(fn [headings {:keys [tag attrs content] :as elm}] (fn [headings {:keys [tag attrs content] :as elm}]
(if (some #{tag} _h) (if (some #{tag} _h)
(conj headings elm) (conj headings elm)
(if-let [more-headings (get-headings content)] (if-let [more-headings (get-headings content)]
(into headings more-headings) (into headings more-headings)
headings))) headings)))
[] content)) [] content))
(defn- zip-toc-tree-to-insertion-point (defn- zip-toc-tree-to-insertion-point
"Given a toc-tree zipper and a header level, navigate "Given a toc-tree zipper and a header level, navigate
@ -46,25 +46,24 @@
and return it." and return it."
[headings] [headings]
(loop [zp (z/zipper (loop [zp (z/zipper
map? map?
:children :children
(fn [node children] (assoc node :children (apply vector children))) (fn [node children] (assoc node :children (apply vector children)))
{:value :root :children []}) {:value :root :children []})
items headings] items headings]
(if-let [{tag :tag {id :id} :attrs [{{name :name} :attrs} title :as htext] :content} (first items)] (if-let [{tag :tag {id :id} :attrs [{{name :name} :attrs} title :as htext] :content} (first items)]
(let [anchor (or id name)] (let [anchor (or id name)]
(if (nil? anchor) (if (nil? anchor)
(recur zp (rest items)) (recur zp (rest items))
(recur (insert-toc-tree-entry zp (recur (insert-toc-tree-entry zp
{:tag tag {:tag tag
:anchor anchor :anchor anchor
:text (or :text (or
(if (string? title) title (-> title :content first)) (if (string? title) title (-> title :content first))
(first htext))}) (first htext))})
(rest items)))) (rest items))))
(z/root zp)))) (z/root zp))))
(defn- make-toc-entry (defn- make-toc-entry
"Given an anchor link and some text, construct a toc entry "Given an anchor link and some text, construct a toc entry
consisting of link to the anchor using the given text, wrapped consisting of link to the anchor using the given text, wrapped
@ -73,7 +72,6 @@
(when (and anchor text) (when (and anchor text)
[:li [:a {:href (str "#" anchor)} text]])) [:li [:a {:href (str "#" anchor)} text]]))
(defn- build-toc (defn- build-toc
"Given the root of a toc tree and either :ol or :ul, "Given the root of a toc tree and either :ol or :ul,
generate the table of contents and return it as a hiccup tree." generate the table of contents and return it as a hiccup tree."
@ -86,9 +84,9 @@
; Create hiccup sequence of :ol/:ul tag and sequence of :li tags ; Create hiccup sequence of :ol/:ul tag and sequence of :li tags
(if (seq children) (if (seq children)
(let [sublist [first-list-open (map build-toc children (let [sublist [first-list-open (map build-toc children
(repeat list-open) (repeat list-open)
(repeat :outer-list?) (repeat :outer-list?)
(repeat false))]] (repeat false))]]
(if-let [li li] ; The root element has nothing so ignore it (if-let [li li] ; The root element has nothing so ignore it
(seq [li sublist]) ; Use seq to lazily concat the li with the sublists (seq [li sublist]) ; Use seq to lazily concat the li with the sublists
sublist)) sublist))
@ -105,8 +103,8 @@
[html & {:keys [list-type] :or {list-type :ol}}] [html & {:keys [list-type] :or {list-type :ol}}]
(let [list-type (if (true? list-type) :ol list-type)] (let [list-type (if (true? list-type) :ol list-type)]
(-> html (-> html
(enlive/html-snippet) (enlive/html-snippet)
(get-headings) (get-headings)
(build-toc-tree) (build-toc-tree)
(build-toc list-type) (build-toc list-type)
(hiccup/html)))) (hiccup/html))))

View file

@ -37,15 +37,15 @@
(deftest test-resource-from-cp-or-fs (deftest test-resource-from-cp-or-fs
(is (is
(.exists (.exists
(:file (:file
(sut/resource-from-cp-or-fs (sut/resource-from-cp-or-fs
"./test-resources/" "./test-resources/"
"templates/themes/bootstrap4-test" "templates/themes/bootstrap4-test"
"js")))) "js"))))
(is (is
(.exists (.exists
(:file (:file
(sut/resource-from-cp-or-fs (sut/resource-from-cp-or-fs
"./" "" ".gitkeep")))) "./" "" ".gitkeep"))))
(is (is
@ -60,7 +60,7 @@
{:path "js/subdir" {:path "js/subdir"
:source-type :classpath :source-type :classpath
:resource-type :dir} :resource-type :dir}
(filter-object (filter-object
(sut/resource-from-cp-or-fs (sut/resource-from-cp-or-fs
"./not-existing-so-load-from-cp" "./not-existing-so-load-from-cp"
"templates/themes/bootstrap4-test" "templates/themes/bootstrap4-test"
@ -77,54 +77,73 @@
(map filter-object (map filter-object
(sut/get-resources-recursive (sut/get-resources-recursive
"" "templates/themes/bootstrap4-test" ["js/dummy.js"])))) "" "templates/themes/bootstrap4-test" ["js/dummy.js"]))))
(is (=
[]
(sut/get-resources-recursive
"" "templates/themes/bootstrap4-test" ["js/dummy.js"] :from-cp false)))
(is (= (is (=
["js/subdir" ["js/subdir"
"js/subdir/test.js" "js/subdir/subdummy.js"
"js/subdir/subdummy.js"] "js/subdir/test.js"]
(map #(:path %) (sort (map :path
(sut/get-resources-recursive (sut/get-resources-recursive
"" "templates/themes/bootstrap4-test" ["js/subdir"]))))) "" "templates/themes/bootstrap4-test" ["js/subdir"])))))
(deftest test-get-resource-paths-recursive
(is (=
[]
(sut/get-resource-paths-recursive "" "templates/themes/bootstrap4-test" ["not-existing"])))
(is (=
["js/dummy.js"]
(sut/get-resource-paths-recursive
"" "templates/themes/bootstrap4-test" ["js/dummy.js"])))
(is (=
[]
(sut/get-resource-paths-recursive
"" "templates/themes/bootstrap4-test" ["js/dummy.js"]
:from-cp false)))
(is (=
["js/subdir"
"js/subdir/test.js"
"js/subdir/subdummy.js"]
(sut/get-resource-paths-recursive
"" "templates/themes/bootstrap4-test" ["js/subdir"])))
(is (= (is (=
["." ["."
"./css" "./css"
"./css/dummy.css" "./css/dummy.css"
"./js"
"./js/subdir"
"./js/subdir/test.js"
"./js/subdir/subdummy.js"
"./js/dummy.js"
"./html" "./html"
"./html/403.html" "./html/403.html"
"./html/404.html"] "./html/404.html"
(sut/get-resource-paths-recursive "" "templates/themes/bootstrap4-test" ["."]))) "./js"
) "./js/dummy.js"
"./js/subdir"
"./js/subdir/subdummy.js"
"./js/subdir/test.js"]
(sort (map :path
(sut/get-resources-recursive
"" "templates/themes/bootstrap4-test" ["."]))))))
(deftest test-delete-resource-recursive (deftest test-get-distinct-markup-dirs
(is (=
["test_pages"
"test_pages/home"
"test_posts"
"test_posts/home"]
(sort (map :path
(sut/get-distinct-markup-dirs
"not-existing-get-from-cp"
"test_posts" "test_pages"
""))))))
(deftest test-distinct-resources-by-path
(is (= [{:path "pages/test"}
{:path "pages/test1"}
{:path "pages/test2"}]
(sut/distinct-resources-by-path [{:path "pages/test"}
{:path "pages/test1"}
{:path "pages/test2"}
{:path "pages/test1"}]))))
(deftest test-create-dirs-from-markup-folders!
(is (do
(sut/delete-resource-recursive! (str target "2"))
(sut/create-dirs-from-markup-folders!
"not-existing-get-from-cp" "test_posts" "test_pages"
(str target "2") "")
(and (verify-dir-exists
(str (str target "2") "/test_pages"))
(verify-dir-exists
(str (str target "2") "/test_posts"))
(verify-dir-exists
(str (str target "2") "/test_pages/home"))))))
(deftest test-delete-resource-recursive!
(is (is
(do (do
(.mkdir (io/file (str "target/tmp" target))) (.mkdir (io/file target))
(sut/delete-resource-recursive! (str "target/tmp" target)) (sut/delete-resource-recursive! target)
(not (verify-dir-exists (str "target/tmp" target)))))) (not (verify-dir-exists target)))))
(deftest test-filter-for-ignore-patterns (deftest test-filter-for-ignore-patterns
(is (= (is (=
@ -132,18 +151,17 @@
(sut/filter-for-ignore-patterns #".*\.ignore" ["file.js" "file.ignore"])))) (sut/filter-for-ignore-patterns #".*\.ignore" ["file.js" "file.ignore"]))))
(deftest test-copy-resources-from-theme! (is (do (deftest test-copy-resources-from-theme! (is (do
(sut/delete-resource-recursive! (str "target/tmp" target)) (sut/delete-resource-recursive! target)
(sut/copy-resources-from-theme! "./" theme target "") (sut/copy-resources-from-theme! "./" theme target "")
(and (verify-dir-exists (and (verify-dir-exists
(str target "/js")) (str target "/js"))
(verify-file-exists (verify-file-exists
(str target "/js/dummy.js")) (str target "/js/dummy.js"))
(verify-dir-exists (verify-dir-exists
(str target "/js/subdir")) (str target "/js/subdir"))
(verify-file-exists (verify-file-exists
(str target "/js/subdir/subdummy.js")) (str target "/js/subdir/subdummy.js"))
(verify-file-exists (verify-file-exists
(str target "/css/dummy.css")) (str target "/css/dummy.css"))
(verify-file-exists (verify-file-exists
(str target "/404.html")) (str target "/404.html"))))))
))))

View file

@ -55,21 +55,21 @@ and more content.
(let [mu (markdown)] (let [mu (markdown)]
(testing "Finds no files" (testing "Finds no files"
(is (empty? (check-for-posts mu)) (is (empty? (check-for-posts mu))
(is (empty? (check-for-pages mu)))) (is (empty? (check-for-pages mu))))
(let [dir->file (let [dir->file
[[check-for-posts "resources/templates/md/posts" "post.md"] [[check-for-posts "resources/templates/md/posts" "post.md"]
[check-for-posts "resources/templates/posts" "post.md"] [check-for-posts "resources/templates/posts" "post.md"]
[check-for-pages "resources/templates/md/pages" "page.md"] [check-for-pages "resources/templates/md/pages" "page.md"]
[check-for-pages "resources/templates/pages" "page.md"]]] [check-for-pages "resources/templates/pages" "page.md"]]]
(doseq [[check-fn dir file] dir->file] (doseq [[check-fn dir file] dir->file]
(testing (str "Finds files in " dir) (testing (str "Finds files in " dir)
(create-entry dir file) (create-entry dir file)
(let [entries (check-fn mu)] (let [entries (check-fn mu)]
(is (= 1 (count entries))) (is (= 1 (count entries)))
(is (= (.getAbsolutePath (File. (str dir File/separator file))) (is (= (.getAbsolutePath (File. (str dir File/separator file)))
(.getAbsolutePath (first entries))))) (.getAbsolutePath (first entries)))))
(reset-resources))))))) (reset-resources)))))))
(defmacro with-markup [mu & body] (defmacro with-markup [mu & body]
`(do `(do
@ -92,9 +92,9 @@ and more content.
(str "entry" (m/ext mu))))) (str "entry" (m/ext mu)))))
(with-markup mu (with-markup mu
(copy-resources-from-markup-folders (copy-resources-from-markup-folders
{:post-root posts-root {:post-root posts-root
:page-root pages-root :page-root pages-root
:blog-prefix "/blog"})) :blog-prefix "/blog"}))
(doseq [dir dirs] (doseq [dir dirs]
(is (.isDirectory (File. (str "resources/public/blog/" dir)))))) (is (.isDirectory (File. (str "resources/public/blog/" dir))))))
@ -102,9 +102,9 @@ and more content.
(reset-resources) (reset-resources)
(testing "No pages or posts nothing to copy" (testing "No pages or posts nothing to copy"
(copy-resources-from-markup-folders (copy-resources-from-markup-folders
{:post-root "pages" {:post-root "pages"
:page-root "posts" :page-root "posts"
:blog-prefix "/blog"}) :blog-prefix "/blog"})
(is (not (.isDirectory (File. (str "resources/public/blog/pages"))))) (is (not (.isDirectory (File. (str "resources/public/blog/pages")))))
(is (not (.isDirectory (File. (str "resources/public/blog/posts")))))) (is (not (.isDirectory (File. (str "resources/public/blog/posts"))))))

View file

@ -1,104 +1,95 @@
(ns cryogen-core.hierarchic-test (ns cryogen-core.hierarchic-test
(:require (:require
[clojure.test :refer :all] [clojure.test :refer :all]
[cryogen-core.hierarchic :as sut])) [cryogen-core.hierarchic :as sut]))
(defn- page [uri page-index] (defn- page [uri page-index]
{:uri uri {:uri uri
:content uri :content uri
:page-index page-index}) :page-index page-index})
(defn- enhanced-page [uri page-index children] (defn- enhanced-page [uri page-index children]
{:uri uri {:uri uri
:content uri :content uri
:page-index page-index :page-index page-index
:children children}) :children children})
(deftest test-normalized-page-root-uri (deftest test-normalized-page-root-uri
(testing (testing
(is (= "" (sut/normalized-page-root-uri ""))) (is (= "" (sut/normalized-page-root-uri "")))
(is (= "/root/" (sut/normalized-page-root-uri "root"))) (is (= "/root/" (sut/normalized-page-root-uri "root")))
(is (= "/root/" (sut/normalized-page-root-uri "/root"))) (is (= "/root/" (sut/normalized-page-root-uri "/root")))
(is (= "/root/" (sut/normalized-page-root-uri "root/"))) (is (= "/root/" (sut/normalized-page-root-uri "root/")))
(is (= "/root/" (sut/normalized-page-root-uri "/root/"))) (is (= "/root/" (sut/normalized-page-root-uri "/root/")))))
))
(deftest test-uri-level (deftest test-uri-level
(testing (testing
(is (= 2 (sut/uri-level "/pages/nav1/"))) (is (= 2 (sut/uri-level "/pages/nav1/")))
(is (= 2 (sut/uri-level "/pages/nav1.html"))) (is (= 2 (sut/uri-level "/pages/nav1.html")))))
))
(def pages-clean-1 [(page "/pages/nav1/" 0) (def pages-clean-1 [(page "/pages/nav1/" 0)
(page "/pages/nav1/nav11/" 1) (page "/pages/nav1/nav11/" 1)
(page "/pages/nav1/nav13/" 3) (page "/pages/nav1/nav13/" 3)
(page "/pages/nav1/nav11/nav112/" 2) (page "/pages/nav1/nav11/nav112/" 2)
(page "/pages/nav1/nav12/" 2) (page "/pages/nav1/nav12/" 2)
(page "/pages/nav1/nav11/xnav111/" 1) (page "/pages/nav1/nav11/xnav111/" 1)])
])
(def expected-clean-1 [(enhanced-page (def expected-clean-1 [(enhanced-page
"/pages/nav1/" 0 "/pages/nav1/" 0
[(enhanced-page [(enhanced-page
"/pages/nav1/nav11/" 1 "/pages/nav1/nav11/" 1
[(page "/pages/nav1/nav11/xnav111/" 1) [(page "/pages/nav1/nav11/xnav111/" 1)
(page "/pages/nav1/nav11/nav112/" 2)]) (page "/pages/nav1/nav11/nav112/" 2)])
(page "/pages/nav1/nav12/" 2) (page "/pages/nav1/nav12/" 2)
(page "/pages/nav1/nav13/" 3)] (page "/pages/nav1/nav13/" 3)])])
)])
(def pages-dirty [(page "/pages/nav1.html" 0) (def pages-dirty [(page "/pages/nav1.html" 0)
(page "/pages/nav1/nav11.html" 1) (page "/pages/nav1/nav11.html" 1)
(page "/pages/nav1/nav13.html" 3) (page "/pages/nav1/nav13.html" 3)
(page "/pages/nav1/nav11/nav112.html" 2) (page "/pages/nav1/nav11/nav112.html" 2)
(page "/pages/nav1/nav12.html" 2) (page "/pages/nav1/nav12.html" 2)
(page "/pages/nav1/nav11/xnav111.html" 1) (page "/pages/nav1/nav11/xnav111.html" 1)])
])
(def expected-dirty [(enhanced-page (def expected-dirty [(enhanced-page
"/pages/nav1.html" 0 "/pages/nav1.html" 0
[(enhanced-page [(enhanced-page
"/pages/nav1/nav11.html" 1 "/pages/nav1/nav11.html" 1
[(page "/pages/nav1/nav11/xnav111.html" 1) [(page "/pages/nav1/nav11/xnav111.html" 1)
(page "/pages/nav1/nav11/nav112.html" 2)]) (page "/pages/nav1/nav11/nav112.html" 2)])
(page "/pages/nav1/nav12.html" 2) (page "/pages/nav1/nav12.html" 2)
(page "/pages/nav1/nav13.html" 3)] (page "/pages/nav1/nav13.html" 3)])])
)])
(def pages-clean-2 [(page "/pages/1/" 0) (def pages-clean-2 [(page "/pages/1/" 0)
(page "/pages/2/" 1) (page "/pages/2/" 1)
(page "/pages/2/22/" 0)]) (page "/pages/2/22/" 0)])
(def expected-clean-2 [(page "/pages/1/" 0) (def expected-clean-2 [(page "/pages/1/" 0)
(enhanced-page (enhanced-page
"/pages/2/" 1 "/pages/2/" 1
[(page "/pages/2/22/" 0)])]) [(page "/pages/2/22/" 0)])])
(def pages-clean-3 [(page "/1/" 0) (def pages-clean-3 [(page "/1/" 0)
(page "/2/" 1) (page "/2/" 1)
(page "/2/22/" 0)]) (page "/2/22/" 0)])
(def expected-clean-3 [(page "/1/" 0) (def expected-clean-3 [(page "/1/" 0)
(enhanced-page (enhanced-page
"/2/" 1 "/2/" 1
[(page "/2/22/" 0)])]) [(page "/2/22/" 0)])])
(deftest test-hierarchic-pages (deftest test-hierarchic-pages
(testing (testing
"hierarchic expectations" "hierarchic expectations"
(is (= expected-clean-1 (is (= expected-clean-1
(sut/build-hierarchic-map "pages" pages-clean-1))) (sut/build-hierarchic-map "pages" pages-clean-1)))
(is (= expected-dirty (is (= expected-dirty
(sut/build-hierarchic-map "pages" pages-dirty))) (sut/build-hierarchic-map "pages" pages-dirty)))
(is (= expected-clean-2 (is (= expected-clean-2
(sut/build-hierarchic-map "pages" pages-clean-2))) (sut/build-hierarchic-map "pages" pages-clean-2)))
(is (= expected-clean-3 (is (= expected-clean-3
(sut/build-hierarchic-map "" pages-clean-3))) (sut/build-hierarchic-map "" pages-clean-3)))))
)
)
(deftest test-filter-pages-for-uri (deftest test-filter-pages-for-uri
(testing (testing
(is (= 6 (count (sut/filter-pages-for-uri "/pages/nav1/" pages-clean-1)))) (is (= 6 (count (sut/filter-pages-for-uri "/pages/nav1/" pages-clean-1))))
(is (= 6 (count (sut/filter-pages-for-uri "/pages/nav1.html" pages-dirty)))) (is (= 6 (count (sut/filter-pages-for-uri "/pages/nav1.html" pages-dirty))))))
))

View file

@ -27,9 +27,9 @@
; Test that the make-toc-entry ignores invalid input ; Test that the make-toc-entry ignores invalid input
(deftest test-make-toc-entry (deftest test-make-toc-entry
(is (nil? (is (nil?
(make-toc-entry nil "Text"))) (make-toc-entry nil "Text")))
(is (nil? (is (nil?
(make-toc-entry "anchor" nil))) (make-toc-entry "anchor" nil)))
(is (= [:li [:a {:href "#anchor"} "Text"]] (is (= [:li [:a {:href "#anchor"} "Text"]]
(make-toc-entry "anchor" "Text")))) (make-toc-entry "anchor" "Text"))))
@ -45,20 +45,20 @@
no-headers [:div [:p "This is not a header"]] no-headers [:div [:p "This is not a header"]]
closing-header-larger-than-opening-1 closing-header-larger-than-opening-1
[:div [:h2 [:a {:name "starting_low"}] [:div [:h2 [:a {:name "starting_low"}]
"Starting Low"] "Starting Low"]
[:h1 [:a {:name "finishing_high"}] [:h1 [:a {:name "finishing_high"}]
"Finishing High"]] "Finishing High"]]
closing-header-larger-than-opening-2 closing-header-larger-than-opening-2
[:div [:h2 [:a {:name "starting_low"}] [:div [:h2 [:a {:name "starting_low"}]
"Starting Low"] "Starting Low"]
[:h4 [:a {:name "jumping_in"}] [:h4 [:a {:name "jumping_in"}]
"Jumping Right In"] "Jumping Right In"]
[:h3 [:a {:name "pulling_back"}] [:h3 [:a {:name "pulling_back"}]
"But then pull back"] "But then pull back"]
[:h2 [:a {:name "to_the_top"}] [:h2 [:a {:name "to_the_top"}]
"To the top"]]] "To the top"]]]
(is (= [:ol.content (seq [[:li [:a {:href "#test"} "Test"]]])] (is (= [:ol.content (seq [[:li [:a {:href "#test"} "Test"]]])]
(-> simplest-header (-> simplest-header
(parse-to-headings) (parse-to-headings)
@ -90,7 +90,6 @@
"but outer headers cannot be less indented " "but outer headers cannot be less indented "
"than the original header."])))) "than the original header."]))))
(deftest test-generate-toc (deftest test-generate-toc
(let [htmlString "<div><h2><a name=\"test\"></a>Test</h2></div>"] (let [htmlString "<div><h2><a name=\"test\"></a>Test</h2></div>"]
(is (= "<ol class=\"content\"><li><a href=\"#test\">Test</a></li></ol>" (is (= "<ol class=\"content\"><li><a href=\"#test\">Test</a></li></ol>"