From efbe8312617ecb82e7f8ee52d161d0a2445e08cf Mon Sep 17 00:00:00 2001 From: J Irving Date: Sat, 27 Dec 2014 02:38:25 -0500 Subject: [PATCH] Support :ignored-file config key Prevent the compiler attempting to process files defined by a list of regexps. By default ignore emacs and vi backup files. --- project.clj | 2 +- src/cryogen_core/compiler.clj | 19 ++++++++++--------- src/cryogen_core/io.clj | 9 ++++++++- src/cryogen_core/sitemap.clj | 4 ++-- src/cryogen_core/watcher.clj | 25 +++++++++++++++---------- 5 files changed, 36 insertions(+), 23 deletions(-) diff --git a/project.clj b/project.clj index 558f725..14ec51c 100644 --- a/project.clj +++ b/project.clj @@ -1,4 +1,4 @@ -(defproject cryogen-core "0.1.8" +(defproject cryogen-core "0.1.9-SNAPSHOT" :description "Cryogen's compiler" :url "https://github.com/lacarmen/cryogen-core" :license {:name "Eclipse Public License" diff --git a/src/cryogen_core/compiler.clj b/src/cryogen_core/compiler.clj index 76bd1ba..b089e29 100644 --- a/src/cryogen_core/compiler.clj +++ b/src/cryogen_core/compiler.clj @@ -26,18 +26,18 @@ (defn find-md-assets "Returns a list of files ending with .md under templates" - [] - (find-assets "templates" ".md")) + [ignored-files] + (find-assets "templates" ".md" ignored-files)) (defn find-posts "Returns a list of markdown files representing posts under the post root in templates/md" - [{:keys [post-root]}] - (find-assets (str "templates/md" post-root) ".md")) + [{:keys [post-root ignored-files]}] + (find-assets (str "templates/md" post-root) ".md" ignored-files)) (defn find-pages "Returns a list of markdown files representing pages under the page root in templates/md" - [{:keys [page-root]}] - (find-assets (str "templates/md" page-root) ".md")) + [{:keys [page-root ignored-files]}] + (find-assets (str "templates/md" page-root) ".md" ignored-files)) (defn parse-post-date "Parses the post date from the post's file name and returns the corresponding java date object" @@ -257,7 +257,8 @@ (update-in [:sass-src] (fnil str "css")) (update-in [:sass-dest] (fnil str "css")) (update-in [:post-date-format] (fnil str "yyyy-MM-dd")) - (update-in [:keep-files] (fnil seq []))) + (update-in [:keep-files] (fnil seq [])) + (update-in [:ignored-files] (fnil seq [#"^\.#.*" #".*\.swp$"]))) site-url (:site-url config) blog-prefix (:blog-prefix config)] (merge @@ -272,7 +273,7 @@ "Generates all the html and copies over resources specified in the config" [] (println (green "compiling assets...")) - (let [{:keys [site-url blog-prefix rss-name recent-posts sass-src sass-dest keep-files] :as config} (read-config) + (let [{:keys [site-url blog-prefix rss-name recent-posts sass-src sass-dest keep-files ignored-files] :as config} (read-config) posts (add-prev-next (read-posts config)) pages (add-prev-next (read-pages config)) [navbar-pages sidebar-pages] (group-pages pages) @@ -297,7 +298,7 @@ (compile-index default-params config) (compile-archives default-params posts config) (println (blue "generating site map")) - (spit (str public blog-prefix "/sitemap.xml") (sitemap/generate site-url)) + (spit (str public blog-prefix "/sitemap.xml") (sitemap/generate site-url ignored-files)) (println (blue "generating rss")) (spit (str public blog-prefix "/" rss-name) (rss/make-channel config posts)) (println (blue "compiling sass")) diff --git a/src/cryogen_core/io.clj b/src/cryogen_core/io.clj index 25b3e1a..691003e 100644 --- a/src/cryogen_core/io.clj +++ b/src/cryogen_core/io.clj @@ -11,9 +11,16 @@ (.toURI) (io/file))) -(defn find-assets [f ext] +(defn ignore [ignored-files] + (fn [file] + (let [name (.getName file) + matches (map #(re-find % name) ignored-files)] + (not (some seq matches))))) + +(defn find-assets [f ext ignored-files] (->> (get-resource f) file-seq + (filter (ignore ignored-files)) (filter (fn [file] (-> file .getName (.endsWith ext)))))) (defn create-folder [folder] diff --git a/src/cryogen_core/sitemap.clj b/src/cryogen_core/sitemap.clj index 0cf4736..8641094 100644 --- a/src/cryogen_core/sitemap.clj +++ b/src/cryogen_core/sitemap.clj @@ -13,13 +13,13 @@ (defn loc [f] (-> f (.getAbsolutePath) (.split "resources/public/") second)) -(defn generate [site-url] +(defn generate [site-url ignored-files] (with-out-str (emit {:tag :urlset :attrs {:xmlns "http://www.sitemaps.org/schemas/sitemap/0.9"} :content - (for [f (find-assets "public" ".html")] + (for [f (find-assets "public" ".html" ignored-files)] {:tag :url :content [{:tag :loc diff --git a/src/cryogen_core/watcher.clj b/src/cryogen_core/watcher.clj index e1edccd..43d478f 100644 --- a/src/cryogen_core/watcher.clj +++ b/src/cryogen_core/watcher.clj @@ -1,21 +1,26 @@ (ns cryogen-core.watcher - (:require [clojure.java.io :refer [file]])) + (:require [clojure.java.io :refer [file]] + [cryogen-core.io :refer [ignore]])) -(defn get-assets [root] - (file-seq (file root))) +(defn get-assets [root ignored-files] + (->> root + file + file-seq + (filter #(not (.isDirectory %))) + (filter (ignore ignored-files)))) -(defn sum-times [path] - (->> (get-assets path) (map #(.lastModified %)) (reduce +))) +(defn sum-times [path ignored-files] + (->> (get-assets path ignored-files) (map #(.lastModified %)) (reduce +))) -(defn watch-assets [root action] - (loop [times (sum-times root)] +(defn watch-assets [root ignored-files action] + (loop [times (sum-times root ignored-files)] (Thread/sleep 300) - (let [new-times (sum-times root)] + (let [new-times (sum-times root ignored-files)] (when-not (= times new-times) (action)) (recur new-times)))) -(defn start-watcher! [root action] - (doto (Thread. #(watch-assets root action)) +(defn start-watcher! [root ignored-files action] + (doto (Thread. #(watch-assets root ignored-files action)) (.setDaemon true) (.start)))