ShapeyShifty/src/clj/shapey_shifty/posts/posts_io.clj

46 lines
1.4 KiB
Clojure
Raw Normal View History

2020-01-28 02:54:24 +00:00
(ns shapey-shifty.posts.posts-io
(:require
2020-02-13 00:36:35 +00:00
[shapey-shifty.posts.core :as core]
[shapey-shifty.authors.author-core :as author]))
2020-01-28 02:54:24 +00:00
(def post-filename "post.json")
2020-01-29 01:42:18 +00:00
(def base-posts-path "resources/posts")
2020-01-28 02:54:24 +00:00
(defn create-path-by-date [year month day]
2020-01-28 02:54:24 +00:00
{:year year :month month :day day})
(defn pathmap-to-path [{:keys [year month day]}]
2020-01-28 07:14:01 +00:00
(format "%s/%s/%s" year month day))
2020-01-28 02:54:24 +00:00
(defn count-posts-in-date [dt-path]
(let [path (pathmap-to-path dt-path)
2020-01-29 01:42:18 +00:00
final-path (format "%s/%s" base-posts-path path)]
2020-01-28 02:54:24 +00:00
(->> final-path
clojure.java.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))
2020-01-29 01:42:18 +00:00
final-path (format "%s/%s/%d/%s" base-posts-path path increment post-filename)]
2020-01-28 02:54:24 +00:00
(clojure.java.io/make-parents final-path)
(spit final-path post)))
2020-01-29 01:42:18 +00:00
(defn assoc-author [post]
(let [filename (get-in post [:properties :author])
author (author/load-author filename)
card (get author :card)]
(assoc post :author card)))
2020-02-03 20:29:19 +00:00
(defn read-post
([file]
(when (.exists file)
(-> file slurp read-string assoc-author)))
([dt-path n]
(let [path (format "%s/%s/%s/%s" base-posts-path (pathmap-to-path dt-path) n post-filename)
f (clojure.java.io/file path)]
(read-post f))))