diff --git a/project.clj b/project.clj index 70d36c4..6dd21bc 100644 --- a/project.clj +++ b/project.clj @@ -1,4 +1,4 @@ -(defproject cryogen-core "0.1.40" +(defproject cryogen-core "0.1.41" :description "Cryogen's compiler" :url "https://github.com/cryogen-project/cryogen-core" :license {:name "Eclipse Public License" @@ -12,6 +12,6 @@ [io.aviso/pretty "0.1.26"] [hiccup "1.0.5"] [selmer "1.0.4"] - [pandect "0.5.4"] + [pandect "0.6.0"] [hawk "0.2.10"] [clj-tagsoup "0.3.0" :exclusions [org.clojure/clojure]]]) diff --git a/resources/.gitkeep b/resources/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/src/cryogen_core/compiler.clj b/src/cryogen_core/compiler.clj index 3f4d139..dfbf151 100644 --- a/src/cryogen_core/compiler.clj +++ b/src/cryogen_core/compiler.clj @@ -31,15 +31,30 @@ [ext] (re-pattern (str (s/replace ext "." "\\.") "$"))) +(defn find-entries + "Returns a list of files under the templates directory according to the + implemented Markup protocol and specified root directory. It defaults to + looking under the implemented protocol's subdirectory, but fallsback to look + at the templates directory." + [root mu ignored-files] + (let [assets (find-assets (path "templates" (m/dir mu) root) + (m/ext mu) + ignored-files)] + (if (seq assets) + assets + (find-assets (path "templates" root) + (m/ext mu) + ignored-files)))) + (defn find-posts - "Returns a list of markdown files representing posts under the post root in templates/md" + "Returns a list of markdown files representing posts under the post root." [{:keys [post-root ignored-files]} mu] - (find-assets (path "templates" (m/dir mu) post-root) (m/ext mu) ignored-files)) + (find-entries post-root mu ignored-files)) (defn find-pages - "Returns a list of markdown files representing pages under the page root in templates/md" + "Returns a list of markdown files representing pages under the page root." [{:keys [page-root ignored-files]} mu] - (find-assets (path "templates" (m/dir mu) page-root) (m/ext mu) ignored-files)) + (find-entries page-root mu ignored-files)) (defn parse-post-date "Parses the post date from the post's file name and returns the corresponding java date object" @@ -381,14 +396,26 @@ [posts config] (map #(update-in % [:tags] (partial map (partial tag-info config))) posts)) +(defn- template-dir? + "Checks that the dir exists in the templates directory." + [dir] + (.isDirectory (file (str "resources/templates/" dir)))) + +(defn- markup-entries [post-root page-root] + (let [entries (for [mu (m/markups) + t (distinct [post-root page-root])] + [(str (m/dir mu) "/" t) t])] + (apply concat entries))) + (defn copy-resources-from-markup-folders - "Copy resources from markup folders" + "Copy resources from markup folders. This does not copy the markup entries." [{:keys [post-root page-root] :as config}] - (copy-resources - (merge config - {:resources (for [mu (m/markups) - t (distinct [post-root page-root])] (str (m/dir mu) "/" t)) - :ignored-files (map #(re-pattern-from-ext (m/ext %)) (m/markups))}))) + (let [folders (->> (markup-entries post-root page-root) + (filter template-dir?))] + (copy-resources + (merge config + {:resources folders + :ignored-files (map #(re-pattern-from-ext (m/ext %)) (m/markups))})))) (defn read-config "Reads the config file" diff --git a/src/cryogen_core/markup.clj b/src/cryogen_core/markup.clj index 69b671c..d9c7143 100644 --- a/src/cryogen_core/markup.clj +++ b/src/cryogen_core/markup.clj @@ -26,3 +26,13 @@ Markups." [] @markup-registry) + +(defn register-markup + "Add a Markup implementation to the registry." + [mu] + (swap! markup-registry conj mu)) + +(defn clear-registry + "Reset the Markup registry." + [] + (reset! markup-registry [])) diff --git a/test/cryogen_core/compiler_test.clj b/test/cryogen_core/compiler_test.clj index e7e77ca..7a79396 100644 --- a/test/cryogen_core/compiler_test.clj +++ b/test/cryogen_core/compiler_test.clj @@ -1,6 +1,10 @@ (ns cryogen-core.compiler-test (:require [clojure.test :refer :all] - [cryogen-core.compiler :refer :all])) + [cryogen-core.compiler :refer :all] + [cryogen-core.io :refer [path]] + [cryogen-core.markup :as m] + [me.raynes.fs :as fs]) + (:import [java.io File])) ; Test that the content-until-more-marker return nil or correct html text. (deftest test-content-until-more-marker @@ -21,3 +25,85 @@ and more content. "
this post has more marker
"))) + +(defn- markdown [] + (reify m/Markup + (dir [this] "md") + (ext [this] ".md"))) + +(defn- asciidoc [] + (reify m/Markup + (dir [this] "asc") + (ext [this] ".asc"))) + +(defn- create-entry [dir file] + (fs/mkdirs (File. dir)) + (fs/create (File. (str dir File/separator file)))) + +(defn- reset-resources [] + (fs/delete-dir "resources") + (create-entry "resources" ".gitkeep")) + +(defn- check-for-pages [mu] + (find-pages {:page-root "pages"} mu)) + +(defn- check-for-posts [mu] + (find-posts {:post-root "posts"} mu)) + +(deftest test-find-entries + (reset-resources) + (let [mu (markdown)] + (testing "Finds no files" + (is (empty? (check-for-posts mu)) + (is (empty? (check-for-pages mu)))) + + (let [dir->file + [[check-for-posts "resources/templates/md/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/pages" "page.md"]]] + (doseq [[check-fn dir file] dir->file] + (testing (str "Finds files in " dir) + (create-entry dir file) + (let [entries (check-fn mu)] + (is (= 1 (count entries))) + (is (= (.getAbsolutePath (File. (str dir File/separator file))) + (.getAbsolutePath (first entries))))) + (reset-resources))))))) + +(defmacro with-markup [mu & body] + `(do + (m/register-markup ~mu) + (try + ~@body + (finally + (m/clear-registry))))) + +(defn- copy-and-check-markup-folders + "Create entries in the markup folders. If `with-dir?` is set to true, include + the Markup implementation's `dir` in the path. Check that the folders exist + in the output folder." + [[pages-root posts-root :as dirs] mu with-dir?] + (doseq [dir dirs] + (let [path (if with-dir? + (str (m/dir mu) "/" dir) + dir)] + (create-entry (str "resources/templates/" path) + (str "entry" (m/ext mu))))) + (with-markup mu + (copy-resources-from-markup-folders + {:post-root posts-root + :page-root pages-root + :blog-prefix "/blog"})) + (doseq [dir dirs] + (is (.isDirectory (File. (str "resources/public/blog/" dir)))))) + +(deftest test-copy-resources-from-markup-folders + (reset-resources) + (doseq [mu [(markdown) (asciidoc)]] + (testing (str "Test copy from markup folders (" (m/dir mu) ")") + (let [dirs ["pages" "posts"]] + (copy-and-check-markup-folders dirs mu true) + (reset-resources) + (copy-and-check-markup-folders dirs mu false)))) + (reset-resources))