From 44d0da8f464660cdead388a6f69dc4fae97dbe09 Mon Sep 17 00:00:00 2001 From: Charlotte Allen Date: Mon, 27 Jan 2020 17:13:09 -0800 Subject: [PATCH 1/4] Add basic post generation method --- .gitignore | 1 + src/clj/shapey_shifty/posts/core.clj | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+) create mode 100644 src/clj/shapey_shifty/posts/core.clj diff --git a/.gitignore b/.gitignore index 0d2092a..e4c8262 100644 --- a/.gitignore +++ b/.gitignore @@ -14,3 +14,4 @@ profiles.clj /node_modules /log +*.swp diff --git a/src/clj/shapey_shifty/posts/core.clj b/src/clj/shapey_shifty/posts/core.clj new file mode 100644 index 0000000..1f75014 --- /dev/null +++ b/src/clj/shapey_shifty/posts/core.clj @@ -0,0 +1,18 @@ +(ns shapey-shifty.posts.core) + +(defn create-empty-post [] {:type nil :properties {:name nil :author nil :published nil :content nil}}) + +(defn set-publish-date [post date] + (assoc-in post [:properties :published] date)) + +(defn set-type [post post-type] + (assoc post :type post-type)) + +(defn set-author [post author] + (assoc-in post [:properties :author] author)) + +(defn set-name [post post-name] + (assoc-in post [:properties :name] post-name)) + +(defn set-content [post post-content] + (assoc-in post [:properties :content] post-content)) -- 2.40.1 From c787cacc522f9eb5b4277e4f1890d4d9d6bc3f14 Mon Sep 17 00:00:00 2001 From: Charlotte Allen Date: Mon, 27 Jan 2020 18:54:24 -0800 Subject: [PATCH 2/4] Implement basic post recording --- .gitignore | 1 + src/clj/shapey_shifty/posts/posts_io.clj | 35 ++++++++++++++++++++++++ 2 files changed, 36 insertions(+) create mode 100644 src/clj/shapey_shifty/posts/posts_io.clj diff --git a/.gitignore b/.gitignore index e4c8262..8885ace 100644 --- a/.gitignore +++ b/.gitignore @@ -15,3 +15,4 @@ profiles.clj /node_modules /log *.swp +resources/posts/ diff --git a/src/clj/shapey_shifty/posts/posts_io.clj b/src/clj/shapey_shifty/posts/posts_io.clj new file mode 100644 index 0000000..fee6cd2 --- /dev/null +++ b/src/clj/shapey_shifty/posts/posts_io.clj @@ -0,0 +1,35 @@ +(ns shapey-shifty.posts.posts-io + (:require + [clojure.data.json :as json] + [shapey-shifty.posts.core :as core])) + +(def post-filename "post.json") +(def base-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-path path)] + (->> final-path + clojure.java.io/file + file-seq + (filter #(.isDirectory %)) + count + dec))) + +(defn jsonify-post [post] + (json/write-str post)) + +(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/%d/%s" base-path path increment post-filename)] + (clojure.java.io/make-parents final-path) + (spit final-path post))) + -- 2.40.1 From 703520077ecff2cd17ad6daf00199355cd33d439 Mon Sep 17 00:00:00 2001 From: Charlotte Allen Date: Mon, 27 Jan 2020 18:57:49 -0800 Subject: [PATCH 3/4] Remove unneeded json stuff. Turns out we can serialize the data structures directly --- src/clj/shapey_shifty/posts/posts_io.clj | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/clj/shapey_shifty/posts/posts_io.clj b/src/clj/shapey_shifty/posts/posts_io.clj index fee6cd2..8bc2b7b 100644 --- a/src/clj/shapey_shifty/posts/posts_io.clj +++ b/src/clj/shapey_shifty/posts/posts_io.clj @@ -1,6 +1,5 @@ (ns shapey-shifty.posts.posts-io (:require - [clojure.data.json :as json] [shapey-shifty.posts.core :as core])) (def post-filename "post.json") @@ -23,9 +22,6 @@ count dec))) -(defn jsonify-post [post] - (json/write-str post)) - (defn write-post [post dt-path] (let [path (pathmap-to-path dt-path) increment (inc (count-posts-in-date dt-path)) -- 2.40.1 From a07bfbd98cf9f65fcfba997c7bc8affb2bfee36c Mon Sep 17 00:00:00 2001 From: Charlotte Allen Date: Mon, 27 Jan 2020 19:16:48 -0800 Subject: [PATCH 4/4] Add parsing of posts. Need to look into the security of read-string. --- src/clj/shapey_shifty/posts/core.clj | 4 ++-- src/clj/shapey_shifty/posts/posts_io.clj | 16 +++++++++++----- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/src/clj/shapey_shifty/posts/core.clj b/src/clj/shapey_shifty/posts/core.clj index 1f75014..9e6bdd5 100644 --- a/src/clj/shapey_shifty/posts/core.clj +++ b/src/clj/shapey_shifty/posts/core.clj @@ -9,10 +9,10 @@ (assoc post :type post-type)) (defn set-author [post author] - (assoc-in post [:properties :author] author)) + (assoc-in post [:properties :author] author)) (defn set-name [post post-name] (assoc-in post [:properties :name] post-name)) (defn set-content [post post-content] - (assoc-in post [:properties :content] post-content)) + (assoc-in post [:properties :content] post-content)) diff --git a/src/clj/shapey_shifty/posts/posts_io.clj b/src/clj/shapey_shifty/posts/posts_io.clj index 8bc2b7b..7ff20c6 100644 --- a/src/clj/shapey_shifty/posts/posts_io.clj +++ b/src/clj/shapey_shifty/posts/posts_io.clj @@ -1,12 +1,11 @@ (ns shapey-shifty.posts.posts-io - (:require - [shapey-shifty.posts.core :as core])) + (:require + [shapey-shifty.posts.core :as core])) (def post-filename "post.json") (def base-path "resources/posts") - -(defn create-path-by-date [year month day] +(defn create-path-by-date [year month day] {:year year :month month :day day}) (defn pathmap-to-path [{:keys [year month day]}] @@ -25,7 +24,14 @@ (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/%d/%s" base-path path increment post-filename)] + final-path (format "%s/%s/%d/%s" base-path path increment post-filename)] (clojure.java.io/make-parents final-path) (spit final-path post))) +(defn read-post [dt-path n] + (let [path (format "%s/%s/%d/%s" base-path (pathmap-to-path dt-path) n post-filename) + f (clojure.java.io/file path)] + (when (.exists f) + (-> f + slurp + read-string)))) -- 2.40.1