Fallback to resources/templates/{pages,posts}

By default, when using markdown files, Cryogen will look for
`resources/templates/md/pages/*.md`. This commits allows Cryogen to fall back to
`resources/templates/pages/*.md` in the case that it can't find any files in the
former directory. It works similarly for posts and Asciidoc files.

Since we're looking specifically for `*.md` for Markdown files and `*.asc` for
Ascii files, this is an opportunity to eliminate redundancy.
This commit is contained in:
Arsene Rei 2016-05-23 22:20:36 -07:00 committed by Carmen La
parent 3e7d43b212
commit f383661e5e
5 changed files with 136 additions and 13 deletions

View file

@ -1,4 +1,4 @@
(defproject cryogen-core "0.1.40" (defproject cryogen-core "0.1.41"
:description "Cryogen's compiler" :description "Cryogen's compiler"
:url "https://github.com/cryogen-project/cryogen-core" :url "https://github.com/cryogen-project/cryogen-core"
:license {:name "Eclipse Public License" :license {:name "Eclipse Public License"
@ -12,6 +12,6 @@
[io.aviso/pretty "0.1.26"] [io.aviso/pretty "0.1.26"]
[hiccup "1.0.5"] [hiccup "1.0.5"]
[selmer "1.0.4"] [selmer "1.0.4"]
[pandect "0.5.4"] [pandect "0.6.0"]
[hawk "0.2.10"] [hawk "0.2.10"]
[clj-tagsoup "0.3.0" :exclusions [org.clojure/clojure]]]) [clj-tagsoup "0.3.0" :exclusions [org.clojure/clojure]]])

0
resources/.gitkeep Normal file
View file

View file

@ -31,15 +31,30 @@
[ext] [ext]
(re-pattern (str (s/replace 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 (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] [{: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 (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] [{: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 (defn parse-post-date
"Parses the post date from the post's file name and returns the corresponding java date object" "Parses the post date from the post's file name and returns the corresponding java date object"
@ -381,14 +396,26 @@
[posts config] [posts config]
(map #(update-in % [:tags] (partial map (partial tag-info config))) posts)) (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 (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}] [{:keys [post-root page-root] :as config}]
(let [folders (->> (markup-entries post-root page-root)
(filter template-dir?))]
(copy-resources (copy-resources
(merge config (merge config
{:resources (for [mu (m/markups) {:resources folders
t (distinct [post-root page-root])] (str (m/dir mu) "/" t)) :ignored-files (map #(re-pattern-from-ext (m/ext %)) (m/markups))}))))
:ignored-files (map #(re-pattern-from-ext (m/ext %)) (m/markups))})))
(defn read-config (defn read-config
"Reads the config file" "Reads the config file"

View file

@ -26,3 +26,13 @@
Markups." Markups."
[] []
@markup-registry) @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 []))

View file

@ -1,6 +1,10 @@
(ns cryogen-core.compiler-test (ns cryogen-core.compiler-test
(:require [clojure.test :refer :all] (: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. ; Test that the content-until-more-marker return nil or correct html text.
(deftest test-content-until-more-marker (deftest test-content-until-more-marker
@ -21,3 +25,85 @@ and more content.
"<div id=\"post\"><div class=\"post-content\"> "<div id=\"post\"><div class=\"post-content\">
this post has more marker this post has more marker
</div></div>"))) </div></div>")))
(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))