58 lines
1.6 KiB
Clojure
58 lines
1.6 KiB
Clojure
(ns shapey-shifty.posts.posts-io
|
|
(:require
|
|
[shapey-shifty.posts.core :as core]
|
|
[shapey-shifty.authors.author-core :as author]
|
|
[clojure.java.io :as io]
|
|
[clojure.edn :as edn]))
|
|
|
|
(def post-filename "post.edn")
|
|
(def base-posts-path "resources/posts")
|
|
|
|
(defn create-path-by-date [year month day]
|
|
{:year year :month month :day day})
|
|
|
|
(defn pathmap-to-path [{:keys [year month day]}]
|
|
(format "%d/%d/%d" year month day))
|
|
|
|
(defn count-posts-in-date [dt-path]
|
|
(let [path (pathmap-to-path dt-path)
|
|
final-path (format "%s/%s" base-posts-path path)]
|
|
(->> final-path
|
|
io/file
|
|
file-seq
|
|
(filter #(.isDirectory %))
|
|
count
|
|
dec)))
|
|
|
|
(defn write-post [post dt-path]
|
|
(let [path (pathmap-to-path dt-path)
|
|
increment (inc (count-posts-in-date dt-path))
|
|
final-path (format "%s/%s/%s/%s" base-posts-path path increment post-filename)]
|
|
(io/make-parents final-path)
|
|
(spit final-path (pr-str post))))
|
|
|
|
(defn assoc-author [post]
|
|
(let [filename (get-in post [:properties :author])
|
|
author (author/load-author filename)
|
|
card (get author :card)]
|
|
(assoc post :author card)))
|
|
|
|
(defn read-post
|
|
([file]
|
|
(when (.exists file)
|
|
(-> file slurp edn/read-string assoc-author)))
|
|
([dt-path n]
|
|
(let [path (format "%s/%s/%d/%s" base-posts-path (pathmap-to-path dt-path) n post-filename)
|
|
f (io/file path)]
|
|
(read-post f))))
|
|
|
|
(defn read-all-posts
|
|
([]
|
|
(read-all-posts base-posts-path))
|
|
([path]
|
|
(->> path
|
|
io/file
|
|
file-seq
|
|
(filter #(.isFile %))
|
|
(map #(read-post %)))))
|