refactor path

This commit is contained in:
jem 2020-02-18 19:58:55 +01:00
parent f72d97e7d1
commit a25a04092e
7 changed files with 52 additions and 60 deletions

View file

@ -7,8 +7,7 @@
; You must not remove this notice, or any other, from this software. ; You must not remove this notice, or any other, from this software.
(ns cryogen-core.classpath-able-io (ns cryogen-core.classpath-able-io
(:require [clojure.string :as st] (:require [cryogen-core.classpath-able-io.fs :as fs]
[cryogen-core.classpath-able-io.fs :as fs]
[cryogen-core.classpath-able-io.cp :as cp] [cryogen-core.classpath-able-io.cp :as cp]
[cryogen-core.classpath-able-io.this :as this] [cryogen-core.classpath-able-io.this :as this]
[schema.core :as s]) [schema.core :as s])
@ -26,15 +25,6 @@
[ignore-patterns source-list] [ignore-patterns source-list]
(filter #(not (re-matches ignore-patterns %)) source-list)) (filter #(not (re-matches ignore-patterns %)) source-list))
; TODO replace this fn ?
(defn path
"Creates path from given parts, ignore empty elements"
[& path-parts]
(->> path-parts
(remove st/blank?)
(st/join "/")
(#(st/replace % #"/+" "/"))))
(defn resource-from-cp-or-fs ;:- Resource (defn resource-from-cp-or-fs ;:- Resource
[fs-prefix ;:- Prefix [fs-prefix ;:- Prefix
base-path ;:- VirtualPath base-path ;:- VirtualPath
@ -102,7 +92,7 @@
: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-resources!
[virtual-path :- s/Str] [virtual-path :- s/Str]
(let [resource-paths (let [resource-paths
(reverse (reverse

View file

@ -45,13 +45,13 @@
at the templates directory." at the templates directory."
[root mu ignored-files] [root mu ignored-files]
(let [assets (cryogen-io/find-assets (let [assets (cryogen-io/find-assets
(cryogen-io/path "templates" (m/dir mu) root) (new-io/path "templates" (m/dir mu) root)
(m/ext mu) (m/ext mu)
ignored-files)] ignored-files)]
(if (seq assets) (if (seq assets)
assets assets
(cryogen-io/find-assets (cryogen-io/find-assets
(cryogen-io/path "templates" root) (new-io/path "templates" root)
(m/ext mu) (m/ext mu)
ignored-files)))) ignored-files))))
@ -79,7 +79,7 @@
:as params}] :as params}]
(let [page-uri (get params uri-type) (let [page-uri (get params uri-type)
uri-end (if clean-urls? (s/replace file-name #"(index)?\.html" "/") file-name)] uri-end (if clean-urls? (s/replace file-name #"(index)?\.html" "/") file-name)]
(cryogen-io/path "/" blog-prefix page-uri uri-end)))) (new-io/path "/" blog-prefix page-uri uri-end))))
(defn read-page-meta (defn read-page-meta
"Returns the clojure map from the top of a markdown page/post" "Returns the clojure map from the top of a markdown page/post"
@ -233,7 +233,7 @@
"When `clean-urls?` is set, appends `/index.html` before spit; otherwise just spits." "When `clean-urls?` is set, appends `/index.html` before spit; otherwise just spits."
[file-uri {:keys [clean-urls?]} data] [file-uri {:keys [clean-urls?]} data]
(if clean-urls? (if clean-urls?
(cryogen-io/create-file-recursive (cryogen-io/path file-uri "index.html") data) (cryogen-io/create-file-recursive (new-io/path file-uri "index.html") data)
(cryogen-io/create-file file-uri data))) (cryogen-io/create-file file-uri data)))
(defn- print-debug-info [data] (defn- print-debug-info [data]
@ -246,7 +246,7 @@
:as params} pages] :as params} pages]
(when-not (empty? pages) (when-not (empty? pages)
(println (blue "compiling pages")) (println (blue "compiling pages"))
;(cryogen-io/create-folder (cryogen-io/path "/" blog-prefix page-root-uri)) ;(cryogen-io/create-folder (new-io/path "/" blog-prefix page-root-uri))
(doseq [{:keys [uri] (doseq [{:keys [uri]
:as page} pages] :as page} pages]
(println "-->" (cyan uri)) (println "-->" (cyan uri))
@ -258,7 +258,7 @@
(merge params (merge params
{:active-page "pages" {:active-page "pages"
:home false :home false
:servlet-context (cryogen-io/path "/" blog-prefix "/") :servlet-context (new-io/path "/" blog-prefix "/")
:page page :page page
:uri uri}))) :uri uri})))
(compile-pages params (:children page))))) (compile-pages params (:children page)))))
@ -269,7 +269,7 @@
:as params} posts] :as params} posts]
(when-not (empty? posts) (when-not (empty? posts)
(println (blue "compiling posts")) (println (blue "compiling posts"))
(cryogen-io/create-folder (cryogen-io/path "/" blog-prefix post-root-uri)) (cryogen-io/create-folder (new-io/path "/" blog-prefix post-root-uri))
(doseq [{:keys [uri] (doseq [{:keys [uri]
:as post} posts] :as post} posts]
(println "-->" (cyan uri)) (println "-->" (cyan uri))
@ -280,7 +280,7 @@
(render-file (str "/html/" (:layout post)) (render-file (str "/html/" (:layout post))
(merge params (merge params
{:active-page "posts" {:active-page "posts"
:servlet-context (cryogen-io/path "/" blog-prefix "/") :servlet-context (new-io/path "/" blog-prefix "/")
:post post :post post
:disqus-shortname disqus-shortname :disqus-shortname disqus-shortname
:uri uri})))))) :uri uri}))))))
@ -291,7 +291,7 @@
:as params} posts-by-tag] :as params} posts-by-tag]
(when-not (empty? posts-by-tag) (when-not (empty? posts-by-tag)
(println (blue "compiling tags")) (println (blue "compiling tags"))
(cryogen-io/create-folder (cryogen-io/path "/" blog-prefix tag-root-uri)) (cryogen-io/create-folder (new-io/path "/" blog-prefix tag-root-uri))
(doseq [[tag posts] posts-by-tag] (doseq [[tag posts] posts-by-tag]
(let [{:keys [name uri]} (tag-info params tag)] (let [{:keys [name uri]} (tag-info params tag)]
(println "-->" (cyan uri)) (println "-->" (cyan uri))
@ -300,7 +300,7 @@
(render-file "/html/tag.html" (render-file "/html/tag.html"
(merge params (merge params
{:active-page "tags" {:active-page "tags"
:servlet-context (cryogen-io/path "/" blog-prefix "/") :servlet-context (new-io/path "/" blog-prefix "/")
:name name :name name
:posts posts :posts posts
:uri uri}))))))) :uri uri})))))))
@ -315,7 +315,7 @@
(render-file "/html/tags.html" (render-file "/html/tags.html"
(merge params (merge params
{:active-page "tags" {:active-page "tags"
:servlet-context (cryogen-io/path "/" blog-prefix "/") :servlet-context (new-io/path "/" blog-prefix "/")
:uri uri}))))) :uri uri})))))
(defn content-until-more-marker (defn content-until-more-marker
@ -353,8 +353,8 @@
[previews params] [previews params]
(mapv (fn [[prev target next]] (mapv (fn [[prev target next]]
(merge target (merge target
{:prev (if prev (page-uri (cryogen-io/path "p" (str (:index prev) ".html")) params) nil) {:prev (if prev (page-uri (new-io/path "p" (str (:index prev) ".html")) params) nil)
:next (if next (page-uri (cryogen-io/path "p" (str (:index next) ".html")) params) nil)})) :next (if next (page-uri (new-io/path "p" (str (:index next) ".html")) params) nil)}))
(partition 3 1 (flatten [nil previews nil])))) (partition 3 1 (flatten [nil previews nil]))))
(defn compile-preview-pages (defn compile-preview-pages
@ -368,18 +368,18 @@
previews (if (> (count previews) 1) previews (if (> (count previews) 1)
(assoc-in previews [1 :prev] (page-uri "index.html" params)) (assoc-in previews [1 :prev] (page-uri "index.html" params))
previews)] previews)]
(cryogen-io/create-folder (cryogen-io/path "/" blog-prefix "p")) (cryogen-io/create-folder (new-io/path "/" blog-prefix "p"))
(doseq [{:keys [index posts prev next]} previews (doseq [{:keys [index posts prev next]} previews
:let [index-page? (= 1 index)]] :let [index-page? (= 1 index)]]
(write-html (write-html
(if index-page? (page-uri "index.html" params) (if index-page? (page-uri "index.html" params)
(page-uri (cryogen-io/path "p" (str index ".html")) params)) (page-uri (new-io/path "p" (str index ".html")) params))
params params
(render-file "/html/previews.html" (render-file "/html/previews.html"
(merge params (merge params
{:active-page "preview" {:active-page "preview"
:home (when index-page? true) :home (when index-page? true)
:servlet-context (cryogen-io/path "/" blog-prefix "/") :servlet-context (new-io/path "/" blog-prefix "/")
:posts posts :posts posts
:prev-uri prev :prev-uri prev
:next-uri next}))))))) :next-uri next})))))))
@ -416,7 +416,7 @@
{:active-page "archives" {:active-page "archives"
:archives true :archives true
:groups (group-for-archive posts) :groups (group-for-archive posts)
:servlet-context (cryogen-io/path "/" blog-prefix "/") :servlet-context (new-io/path "/" blog-prefix "/")
:uri uri}))))) :uri uri})))))
(defn compile-authors (defn compile-authors
@ -424,7 +424,7 @@
[{:keys [blog-prefix author-root-uri author] [{:keys [blog-prefix author-root-uri author]
:as params} posts] :as params} posts]
(println (blue "compiling authors")) (println (blue "compiling authors"))
(cryogen-io/create-folder (cryogen-io/path "/" blog-prefix author-root-uri)) (cryogen-io/create-folder (new-io/path "/" blog-prefix author-root-uri))
;; if the post author is empty defaults to config's :author ;; if the post author is empty defaults to config's :author
(doseq [{:keys [author posts]} (group-for-author posts author)] (doseq [{:keys [author posts]} (group-for-author posts author)]
(let [uri (page-uri (str author ".html") :author-root-uri params)] (let [uri (page-uri (str author ".html") :author-root-uri params)]
@ -435,7 +435,7 @@
(merge params (merge params
{:author author {:author author
:groups (group-for-archive posts) :groups (group-for-archive posts)
:servlet-context (cryogen-io/path "/" blog-prefix "/") :servlet-context (new-io/path "/" blog-prefix "/")
:uri uri})))))) :uri uri}))))))
(defn tag-posts (defn tag-posts
@ -542,7 +542,7 @@
: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 (new-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-prefix "resources/" file-resource-prefix "resources/"
file-target-prefix "target/" file-target-prefix "target/"
@ -561,8 +561,8 @@
(println "\t-->" (cyan target-file-uri)) (println "\t-->" (cyan target-file-uri))
(println (blue "debug: home-page:")) (println (blue "debug: home-page:"))
(println "\t-->" (cyan (-> params :home-page)))) (println "\t-->" (cyan (-> params :home-page))))
(new-io/delete-resource-recursive! (.getPath target-file-uri)) (new-io/delete-resources! (.getPath target-file-uri))
(new-io/delete-resource-recursive! (cp-io/path "resources/public" blog-prefix)) (new-io/delete-resources! (new-io/path "resources/public" blog-prefix))
(println (blue "preparing theme resources")) (println (blue "preparing theme resources"))
(new-io/copy-html-from-theme! "content/" (new-io/copy-html-from-theme! "content/"
theme theme
@ -571,18 +571,18 @@
(println (blue "copying theme resources")) (println (blue "copying theme resources"))
(new-io/copy-resources-from-theme! "content/" (new-io/copy-resources-from-theme! "content/"
theme theme
(cp-io/path "resources/public" blog-prefix) (new-io/path "resources/public" blog-prefix)
ignored-files) ignored-files)
(println (blue "copying markup resources")) (println (blue "copying markup resources"))
(new-io/copy-resources-from-templates! "content/" (new-io/copy-resources-from-templates! "content/"
resources resources
(cp-io/path "resources/public" blog-prefix) (new-io/path "resources/public" blog-prefix)
ignored-files) ignored-files)
(println (blue "prepare folders based on markup")) (println (blue "prepare folders based on markup"))
(new-io/create-dirs-from-markup-folders! "content/" (new-io/create-dirs-from-markup-folders! "content/"
(:posts config) (:posts config)
(:pages config) (:pages config)
(cp-io/path "resources/public" blog-prefix) (new-io/path "resources/public" blog-prefix)
ignored-files) ignored-files)
(set-custom-resource-path! (.toString target-file-uri)) (set-custom-resource-path! (.toString target-file-uri))
(compile-pages params modelled-pages) (compile-pages params modelled-pages)
@ -598,16 +598,16 @@
(compile-authors params posts)) (compile-authors params posts))
(println (blue "generating site map")) (println (blue "generating site map"))
(->> (sitemap/generate site-url ignored-files) (->> (sitemap/generate site-url ignored-files)
(cryogen-io/create-file (cryogen-io/path "/" blog-prefix "sitemap.xml"))) (cryogen-io/create-file (new-io/path "/" blog-prefix "sitemap.xml")))
(println (blue "generating main rss")) (println (blue "generating main rss"))
(->> (rss/make-channel config posts) (->> (rss/make-channel config posts)
(cryogen-io/create-file (cryogen-io/path "/" blog-prefix rss-name))) (cryogen-io/create-file (new-io/path "/" blog-prefix rss-name)))
(println (blue "generating filtered rss")) (println (blue "generating filtered rss"))
(rss/make-filtered-channels config posts-by-tag) (rss/make-filtered-channels config posts-by-tag)
(println (blue "compiling sass")) (println (blue "compiling sass"))
(sass/compile-sass->css! (sass/compile-sass->css!
(merge (select-keys config [:sass-path :compass-path :sass-src :ignored-files]) (merge (select-keys config [:sass-path :compass-path :sass-src :ignored-files])
{:sass-dest (cryogen-io/path ".." "public" blog-prefix sass-dest) {:sass-dest (new-io/path ".." "public" blog-prefix sass-dest)
:base-dir "resources/templates/"})))) :base-dir "resources/templates/"}))))
(defn compile-assets-timed [] (defn compile-assets-timed []

View file

@ -1,18 +1,11 @@
(ns cryogen-core.io (ns cryogen-core.io
(:require [clojure.java.io :as io] (:require [clojure.java.io :as io]
[clojure.string :as s] [clojure.string :as s]
[cryogen-core.new-io :as new-io]
[me.raynes.fs :as fs])) [me.raynes.fs :as fs]))
(def public "resources/public") (def public "resources/public")
(defn path
"Creates path from given parts, ignore empty elements"
[& path-parts]
(->> path-parts
(remove s/blank?)
(s/join "/")
(#(s/replace % #"/+" "/"))))
(defn re-filter [bool-fn re & other-res] (defn re-filter [bool-fn re & other-res]
(let [res (conj other-res re)] (let [res (conj other-res re)]
(reify java.io.FilenameFilter (reify java.io.FilenameFilter
@ -45,12 +38,12 @@
[])) []))
(defn create-folder [folder] (defn create-folder [folder]
(let [loc (io/file (path public folder))] (let [loc (io/file (new-io/path public folder))]
(when-not (.exists loc) (when-not (.exists loc)
(.mkdirs loc)))) (.mkdirs loc))))
(defn create-file [file data] (defn create-file [file data]
(spit (path public file) data)) (spit (new-io/path public file) data))
(defn create-file-recursive [file data] (defn create-file-recursive [file data]
(create-folder (.getParent (io/file file))) (create-folder (.getParent (io/file file)))
@ -74,7 +67,7 @@
(defn copy-resources [{:keys [blog-prefix resources ignored-files]}] (defn copy-resources [{:keys [blog-prefix resources ignored-files]}]
(doseq [resource resources] (doseq [resource resources]
(let [src (str "resources/templates/" resource) (let [src (str "resources/templates/" resource)
target (path public blog-prefix (fs/base-name resource))] target (new-io/path public blog-prefix (fs/base-name resource))]
(cond (cond
(not (.exists (io/file src))) (not (.exists (io/file src)))
(throw (IllegalArgumentException. (str "resource " src " not found"))) (throw (IllegalArgumentException. (str "resource " src " not found")))

View file

@ -7,11 +7,11 @@
; You must not remove this notice, or any other, from this software. ; You must not remove this notice, or any other, from this software.
(ns cryogen-core.new-io (ns cryogen-core.new-io
(:require (:require [clojure.string :as st]
[clojure.java.io :as io] [clojure.java.io :as io]
[cryogen-core.classpath-able-io :as cp-io])) [cryogen-core.classpath-able-io :as cp-io]))
(def delete-resource-recursive! cp-io/delete-resource-recursive!) (def delete-resources! cp-io/delete-resources!)
(def public "resources/public") (def public "resources/public")
@ -52,3 +52,11 @@
(doseq [resource resources] (doseq [resource resources]
(io/make-parents (io/file (str target-path "/" (:virtual-path resource)))) (io/make-parents (io/file (str target-path "/" (:virtual-path resource))))
(.mkdir (io/file (str target-path "/" (:virtual-path resource))))))) (.mkdir (io/file (str target-path "/" (:virtual-path resource)))))))
(defn path
"Creates path from given parts, ignore empty elements"
[& path-parts]
(->> path-parts
(remove st/blank?)
(st/join "/")
(#(st/replace % #"/+" "/"))))

View file

@ -1,5 +1,6 @@
(ns cryogen-core.rss (ns cryogen-core.rss
(:require [clj-rss.core :as rss] (:require [clj-rss.core :as rss]
[cryogen-core.new-io :as new-io]
[text-decoration.core :refer :all] [text-decoration.core :refer :all]
[cryogen-core.io :as cryogen-io]) [cryogen-core.io :as cryogen-io])
(:import java.util.Date)) (:import java.util.Date))
@ -30,6 +31,6 @@
(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]
(let [uri (cryogen-io/path "/" blog-prefix (str (name filter) ".xml"))] (let [uri (new-io/path "/" blog-prefix (str (name filter) ".xml"))]
(println "\t-->" (cyan uri)) (println "\t-->" (cyan uri))
(cryogen-io/create-file uri (make-channel config (get posts-by-tag filter)))))) (cryogen-io/create-file uri (make-channel config (get posts-by-tag filter))))))

View file

@ -89,10 +89,10 @@
["file.js"] ["file.js"]
(sut/filter-for-ignore-patterns #".*\.ignore" ["file.js" "file.ignore"])))) (sut/filter-for-ignore-patterns #".*\.ignore" ["file.js" "file.ignore"]))))
(deftest test-delete-resource-recursive! (deftest test-delete-resources!
(is (is
(let [target-tmp "target/tmp3"] (let [target-tmp "target/tmp3"]
(.mkdir (io/file target-tmp)) (.mkdir (io/file target-tmp))
(.createNewFile (io/file (str target-tmp "/file-tmp"))) (.createNewFile (io/file (str target-tmp "/file-tmp")))
(sut/delete-resource-recursive! target-tmp) (sut/delete-resources! target-tmp)
(not (ftt/verify-dir-exists target-tmp))))) (not (ftt/verify-dir-exists target-tmp)))))

View file

@ -33,7 +33,7 @@
(deftest test-create-dirs-from-markup-folders! (deftest test-create-dirs-from-markup-folders!
(is (is
(let [target-tmp "target/tmp-test-create-dirs-from-markup-folders"] (let [target-tmp "target/tmp-test-create-dirs-from-markup-folders"]
(sut/delete-resource-recursive! target-tmp) (sut/delete-resources! target-tmp)
(sut/create-dirs-from-markup-folders! (sut/create-dirs-from-markup-folders!
"./not-existing-get-from-cp" "./not-existing-get-from-cp"
"test_posts" "test_posts"
@ -49,7 +49,7 @@
(deftest test-copy-resources-from-theme! (deftest test-copy-resources-from-theme!
(is (is
(let [target-tmp "target/tmp-test-copy-resources-from-theme"] (let [target-tmp "target/tmp-test-copy-resources-from-theme"]
(sut/delete-resource-recursive! target-tmp) (sut/delete-resources! target-tmp)
(sut/copy-resources-from-theme! "./" theme target-tmp "") (sut/copy-resources-from-theme! "./" theme target-tmp "")
(and (ftt/verify-dir-exists (and (ftt/verify-dir-exists
(str target-tmp "/js")) (str target-tmp "/js"))