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.
(ns cryogen-core.classpath-able-io
(:require [clojure.string :as st]
[cryogen-core.classpath-able-io.fs :as fs]
(:require [cryogen-core.classpath-able-io.fs :as fs]
[cryogen-core.classpath-able-io.cp :as cp]
[cryogen-core.classpath-able-io.this :as this]
[schema.core :as s])
@ -26,15 +25,6 @@
[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
[fs-prefix ;:- Prefix
base-path ;:- VirtualPath
@ -102,7 +92,7 @@
:from-fs from-fs)))
; TODO: Add files to keep
(s/defn delete-resource-recursive!
(s/defn delete-resources!
[virtual-path :- s/Str]
(let [resource-paths
(reverse

View file

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

View file

@ -1,18 +1,11 @@
(ns cryogen-core.io
(:require [clojure.java.io :as io]
[clojure.string :as s]
[cryogen-core.new-io :as new-io]
[me.raynes.fs :as fs]))
(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]
(let [res (conj other-res re)]
(reify java.io.FilenameFilter
@ -45,12 +38,12 @@
[]))
(defn create-folder [folder]
(let [loc (io/file (path public folder))]
(let [loc (io/file (new-io/path public folder))]
(when-not (.exists loc)
(.mkdirs loc))))
(defn create-file [file data]
(spit (path public file) data))
(spit (new-io/path public file) data))
(defn create-file-recursive [file data]
(create-folder (.getParent (io/file file)))
@ -74,7 +67,7 @@
(defn copy-resources [{:keys [blog-prefix resources ignored-files]}]
(doseq [resource resources]
(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
(not (.exists (io/file src)))
(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.
(ns cryogen-core.new-io
(:require
[clojure.java.io :as io]
[cryogen-core.classpath-able-io :as cp-io]))
(:require [clojure.string :as st]
[clojure.java.io :as 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")
@ -52,3 +52,11 @@
(doseq [resource resources]
(io/make-parents (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
(:require [clj-rss.core :as rss]
[cryogen-core.new-io :as new-io]
[text-decoration.core :refer :all]
[cryogen-core.io :as cryogen-io])
(:import java.util.Date))
@ -30,6 +31,6 @@
(defn make-filtered-channels [{:keys [rss-filters blog-prefix] :as config} posts-by-tag]
(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))
(cryogen-io/create-file uri (make-channel config (get posts-by-tag filter))))))

View file

@ -89,10 +89,10 @@
["file.js"]
(sut/filter-for-ignore-patterns #".*\.ignore" ["file.js" "file.ignore"]))))
(deftest test-delete-resource-recursive!
(deftest test-delete-resources!
(is
(let [target-tmp "target/tmp3"]
(.mkdir (io/file target-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)))))

View file

@ -33,7 +33,7 @@
(deftest test-create-dirs-from-markup-folders!
(is
(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!
"./not-existing-get-from-cp"
"test_posts"
@ -49,7 +49,7 @@
(deftest test-copy-resources-from-theme!
(is
(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 "")
(and (ftt/verify-dir-exists
(str target-tmp "/js"))