separate new -io from cp-io

This commit is contained in:
jem 2020-01-07 13:47:34 +01:00
parent bbae94fa8c
commit 72072f2ace
3 changed files with 128 additions and 101 deletions

View file

@ -203,20 +203,6 @@
(when (.isFile source-file) (when (.isFile source-file)
(io/copy source-file target-file))))))) (io/copy source-file target-file)))))))
(defn copy-resources-from-user!
[fs-prefix resources target-path ignore-patterns]
(let [resource-path "templates"]
(copy-resources! fs-prefix resource-path resources
target-path ignore-patterns)))
(defn copy-resources-from-theme!
[fs-prefix theme target-path ignore-patterns]
(let [theme-path (str "templates/themes/" theme)]
(copy-resources! fs-prefix theme-path ["css" "js"]
target-path ignore-patterns)
(copy-resources! fs-prefix (str theme-path "/html") ["404.html"]
target-path ignore-patterns)))
(defn distinct-resources-by-path (defn distinct-resources-by-path
[resources] [resources]
(loop [paths (set (map :path resources)) (loop [paths (set (map :path resources))
@ -227,21 +213,3 @@
(rest resources) (rest resources)
(conj acc (first resources))) (conj acc (first resources)))
:else (recur paths (rest resources) acc)))) :else (recur paths (rest resources) acc))))
(defn get-distinct-markup-dirs
[fs-prefix posts pages ignore-patterns]
(let [base-path "templates/md"
resources (get-resources-recursive
fs-prefix base-path [pages posts])
filtered-resources (->> (filter #(= (:resource-type %) :dir) resources)
(distinct-resources-by-path))]
filtered-resources))
(defn create-dirs-from-markup-folders!
"Copy resources from markup folders. This does not copy the markup entries."
[fs-prefix posts pages target-path ignore-patterns]
(let [resources (get-distinct-markup-dirs fs-prefix posts pages
ignore-patterns)]
(doseq [resource resources]
(io/make-parents (io/file (str target-path "/" (:path resource))))
(.mkdir (io/file (str target-path "/" (:path resource)))))))

View file

@ -8,7 +8,7 @@
[selmer.util :refer [set-custom-resource-path!]] [selmer.util :refer [set-custom-resource-path!]]
[text-decoration.core :refer :all] [text-decoration.core :refer :all]
[cryogen-core.io :as cryogen-io] [cryogen-core.io :as cryogen-io]
[cryogen-core.classpath-able-io :as cp-io] [cryogen-core.new-io :as new-io]
[cryogen-core.klipse :as klipse] [cryogen-core.klipse :as klipse]
[cryogen-core.markup :as m] [cryogen-core.markup :as m]
[cryogen-core.rss :as rss] [cryogen-core.rss :as rss]
@ -70,7 +70,8 @@
"Creates a URI from file name. `uri-type` is any of the uri types specified in config, e.g., `:post-root-uri`." "Creates a URI from file name. `uri-type` is any of the uri types specified in config, e.g., `:post-root-uri`."
([file-name params] ([file-name params]
(page-uri file-name nil params)) (page-uri file-name nil params))
([file-name uri-type {:keys [blog-prefix clean-urls?] :as params}] ([file-name uri-type {:keys [blog-prefix clean-urls?]
: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)))) (cryogen-io/path "/" blog-prefix page-uri uri-end))))
@ -236,11 +237,13 @@
(defn compile-pages (defn compile-pages
"Compiles all the pages into html and spits them out into the public folder" "Compiles all the pages into html and spits them out into the public folder"
[{:keys [blog-prefix page-root-uri debug?] :as params} pages] [{:keys [blog-prefix page-root-uri debug?]
: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 (cryogen-io/path "/" blog-prefix page-root-uri))
(doseq [{:keys [uri] :as page} pages] (doseq [{:keys [uri]
:as page} pages]
(println "-->" (cyan uri)) (println "-->" (cyan uri))
(when debug? (when debug?
(print-debug-info page)) (print-debug-info page))
@ -257,11 +260,13 @@
(defn compile-posts (defn compile-posts
"Compiles all the posts into html and spits them out into the public folder" "Compiles all the posts into html and spits them out into the public folder"
[{:keys [blog-prefix post-root-uri disqus-shortname debug?] :as params} posts] [{:keys [blog-prefix post-root-uri disqus-shortname debug?]
: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 (cryogen-io/path "/" blog-prefix post-root-uri))
(doseq [{:keys [uri] :as post} posts] (doseq [{:keys [uri]
:as post} posts]
(println "-->" (cyan uri)) (println "-->" (cyan uri))
(when debug? (when debug?
(print-debug-info post)) (print-debug-info post))
@ -277,7 +282,8 @@
(defn compile-tags (defn compile-tags
"Compiles all the tag pages into html and spits them out into the public folder" "Compiles all the tag pages into html and spits them out into the public folder"
[{:keys [blog-prefix tag-root-uri] :as params} posts-by-tag] [{:keys [blog-prefix tag-root-uri]
: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 (cryogen-io/path "/" blog-prefix tag-root-uri))
@ -294,7 +300,8 @@
:posts posts :posts posts
:uri uri}))))))) :uri uri})))))))
(defn compile-tags-page [{:keys [blog-prefix] :as params}] (defn compile-tags-page [{:keys [blog-prefix]
:as params}]
"Compiles a page with links to each tag page. Spits the page into the public folder" "Compiles a page with links to each tag page. Spits the page into the public folder"
(println (blue "compiling tags page")) (println (blue "compiling tags page"))
(let [uri (page-uri "tags.html" params)] (let [uri (page-uri "tags.html" params)]
@ -332,7 +339,8 @@
(->> posts (->> posts
(map #(create-preview blocks-per-preview %)) (map #(create-preview blocks-per-preview %))
(partition-all posts-per-page) (partition-all posts-per-page)
(map-indexed (fn [i v] {:index (inc i) :posts v})))) (map-indexed (fn [i v] {:index (inc i)
:posts v}))))
(defn create-preview-links (defn create-preview-links
"Turn each vector of previews into a map with :prev and :next keys that contain the uri of the "Turn each vector of previews into a map with :prev and :next keys that contain the uri of the
@ -346,7 +354,8 @@
(defn compile-preview-pages (defn compile-preview-pages
"Compiles a series of pages containing 'previews' from each post" "Compiles a series of pages containing 'previews' from each post"
[{:keys [blog-prefix posts-per-page blocks-per-preview] :as params} posts] [{:keys [blog-prefix posts-per-page blocks-per-preview]
:as params} posts]
(when-not (empty? posts) (when-not (empty? posts)
(let [previews (-> posts (let [previews (-> posts
(create-previews posts-per-page blocks-per-preview) (create-previews posts-per-page blocks-per-preview)
@ -356,7 +365,7 @@
previews)] previews)]
(cryogen-io/create-folder (cryogen-io/path "/" blog-prefix "p")) (cryogen-io/create-folder (cryogen-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 (cryogen-io/path "p" (str index ".html")) params))
@ -372,7 +381,8 @@
(defn compile-index (defn compile-index
"Compiles the index page into html and spits it out into the public folder" "Compiles the index page into html and spits it out into the public folder"
[{:keys [disqus? debug? home-page] :as params}] [{:keys [disqus? debug? home-page]
:as params}]
(println (blue "compiling index")) (println (blue "compiling index"))
(let [uri (page-uri "index.html" params)] (let [uri (page-uri "index.html" params)]
(when debug? (when debug?
@ -390,7 +400,8 @@
(defn compile-archives (defn compile-archives
"Compiles the archives page into html and spits it out into the public folder" "Compiles the archives page into html and spits it out into the public folder"
[{:keys [blog-prefix] :as params} posts] [{:keys [blog-prefix]
:as params} posts]
(println (blue "compiling archives")) (println (blue "compiling archives"))
(let [uri (page-uri "archives.html" params)] (let [uri (page-uri "archives.html" params)]
(write-html uri (write-html uri
@ -405,7 +416,8 @@
(defn compile-authors (defn compile-authors
"For each author, creates a page with filtered posts." "For each author, creates a page with filtered posts."
[{:keys [blog-prefix author-root-uri author] :as params} posts] [{:keys [blog-prefix author-root-uri author]
: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 (cryogen-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
@ -439,7 +451,8 @@
(defn copy-resources-from-markup-folders (defn copy-resources-from-markup-folders
"Copy resources from markup folders. This does not copy the markup entries." "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) (let [folders (->> (markup-entries post-root page-root)
(filter template-dir?))] (filter template-dir?))]
(cryogen-io/copy-resources (cryogen-io/copy-resources
@ -480,7 +493,8 @@
"Add the klipse html under the :klipse key and adds nohighlight "Add the klipse html under the :klipse key and adds nohighlight
classes to any code blocks that are to be klipsified. Expects classes to any code blocks that are to be klipsified. Expects
configuration to be under :klipse, if there's none it does nothing." configuration to be under :klipse, if there's none it does nothing."
[{:keys [klipse content] :as post-or-page}] [{:keys [klipse content]
:as post-or-page}]
(-> post-or-page (-> post-or-page
(update :klipse klipse/emit content) (update :klipse klipse/emit content)
(update :content klipse/tag-nohighlight (:settings klipse)))) (update :content klipse/tag-nohighlight (:settings klipse))))
@ -489,46 +503,47 @@
"Generates all the html and copies over resources specified in the config" "Generates all the html and copies over resources specified in the config"
[] []
(println (green "compiling assets...")) (println (green "compiling assets..."))
(let [{:keys [^String site-url blog-prefix rss-name recent-posts (let [{:keys [^String site-url blog-prefix
sass-dest keep-files ignored-files previews? rss-name recent-posts sass-dest
author-root-uri theme debug? page-model keep-files ignored-files previews?
page-root-uri resources] author-root-uri theme debug?
page-model page-root-uri resources]
:as config} (read-config) :as config} (read-config)
posts (map klipsify (add-prev-next (read-posts config))) posts (map klipsify (add-prev-next (read-posts config)))
posts-by-tag (group-by-tags posts) posts-by-tag (group-by-tags posts)
posts (tag-posts posts config) posts (tag-posts posts config)
latest-posts (->> posts (take recent-posts) vec) latest-posts (->> posts (take recent-posts) vec)
klipsified-pages (map klipsify (read-pages config)) klipsified-pages (map klipsify (read-pages config))
modelled-pages (cond modelled-pages (cond
(= page-model :flat) klipsified-pages (= page-model :flat) klipsified-pages
(= page-model :hierarchic) (hierarchic/build-hierarchic-map page-root-uri klipsified-pages)) (= page-model :hierarchic) (hierarchic/build-hierarchic-map page-root-uri klipsified-pages))
home-page (->> modelled-pages home-page (->> modelled-pages
(filter #(boolean (:home? %))) (filter #(boolean (:home? %)))
(first)) (first))
other-pages (->> modelled-pages other-pages (->> modelled-pages
(remove #{home-page}) (remove #{home-page})
(add-prev-next)) (add-prev-next))
params (merge config params (merge config
{:today (java.util.Date.) {:today (java.util.Date.)
:title (:site-title config) :title (:site-title config)
:active-page "home" :active-page "home"
:tags (map (partial tag-info config) (keys posts-by-tag)) :tags (map (partial tag-info config) (keys posts-by-tag))
:latest-posts latest-posts :latest-posts latest-posts
:pages other-pages :pages other-pages
:home-page (if home-page :home-page (if home-page
home-page home-page
(assoc (first latest-posts) :layout "home.html")) (assoc (first latest-posts) :layout "home.html"))
: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 (cryogen-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/"
resource-prefix (str "templates/themes/" theme) resource-prefix (str "templates/themes/" theme)
file-uri (:uri file-uri (:uri
(cp-io/resource-from-cp-or-fs (new-io/resource-from-cp-or-fs
file-resource-prefix resource-prefix "" file-resource-prefix resource-prefix ""
:from-cp false))] :from-cp false))]
(when debug? (when debug?
(println (blue "debug: page-model:")) (println (blue "debug: page-model:"))
(println "\t-->" (cyan page-model)) (println "\t-->" (cyan page-model))
@ -540,25 +555,25 @@
;; TODO: 2. use target/theme as custome-resource-path ;; TODO: 2. use target/theme as custome-resource-path
(set-custom-resource-path! (.toString file-uri)) (set-custom-resource-path! (.toString file-uri))
;(cryogen-io/wipe-public-folder keep-files) ;(cryogen-io/wipe-public-folder keep-files)
(cp-io/delete-resource-recursive! (cp-io/path "resources/public" blog-prefix)) (new-io/delete-resource-recursive! (new-io/path "resources/public" blog-prefix))
(println (blue "copying theme resources")) (println (blue "copying theme resources"))
;(cryogen-io/copy-resources-from-theme config) ;(cryogen-io/copy-resources-from-theme config)
(cp-io/copy-resources-from-theme! "resources/" (new-io/copy-resources-from-theme! "resources/"
theme theme
(cp-io/path "resources/public" blog-prefix) (new-io/path "resources/public" blog-prefix)
ignored-files) ignored-files)
(println (blue "copying resources")) (println (blue "copying resources"))
;(cryogen-io/copy-resources config) ;(cryogen-io/copy-resources config)
(cp-io/copy-resources-from-user! "resources/" (new-io/copy-resources-from-user! "resources/"
resources resources
(cp-io/path "resources/public" blog-prefix) (new-io/path "resources/public" blog-prefix)
ignored-files) ignored-files)
;(copy-resources-from-markup-folders config) ;(copy-resources-from-markup-folders config)
(cp-io/create-dirs-from-markup-folders! "resources/" (new-io/create-dirs-from-markup-folders! "resources/"
(: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)
; TODO: Hier weitermachen ; TODO: Hier weitermachen
(compile-pages params modelled-pages) (compile-pages params modelled-pages)
(compile-posts params posts) (compile-posts params posts)

View file

@ -0,0 +1,44 @@
; Copyright (c) meissa. All rights reserved.
; The use and distribution terms for this software are covered by the
; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
; which can be found in the file epl-v10.html at the root of this distribution.
; By using this software in any fashion, you are agreeing to be bound by
; the terms of this license.
; 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]))
(defn copy-resources-from-user!
[fs-prefix resources target-path ignore-patterns]
(let [resource-path "templates"]
(cp-io/copy-resources! fs-prefix resource-path resources
target-path ignore-patterns)))
(defn copy-resources-from-theme!
[fs-prefix theme target-path ignore-patterns]
(let [theme-path (str "templates/themes/" theme)]
(cp-io/copy-resources! fs-prefix theme-path ["css" "js"]
target-path ignore-patterns)
(cp-io/copy-resources! fs-prefix (str theme-path "/html") ["404.html"]
target-path ignore-patterns)))
(defn get-distinct-markup-dirs
[fs-prefix posts pages ignore-patterns]
(let [base-path "templates/md"
resources (cp-io/get-resources-recursive
fs-prefix base-path [pages posts])
filtered-resources (->> (filter #(= (:resource-type %) :dir) resources)
(cp-io/distinct-resources-by-path))]
filtered-resources))
(defn create-dirs-from-markup-folders!
"Copy resources from markup folders. This does not copy the markup entries."
[fs-prefix posts pages target-path ignore-patterns]
(let [resources (get-distinct-markup-dirs fs-prefix posts pages
ignore-patterns)]
(doseq [resource resources]
(io/make-parents (io/file (str target-path "/" (:path resource))))
(.mkdir (io/file (str target-path "/" (:path resource)))))))