Skip to content

Commit

Permalink
Render post content.
Browse files Browse the repository at this point in the history
  • Loading branch information
raek committed Feb 23, 2012
1 parent 69eb381 commit 63875d0
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 15 deletions.
30 changes: 26 additions & 4 deletions src/bokareis/main.clj
@@ -1,6 +1,7 @@
(ns bokareis.main
(:require [clojure.java.io :as io]
[clojure.data.json :as json])
[clojure.data.json :as json]
[clojure.java.shell :as sh])
(:use [clojure.java.io :only [file] :rename {file f}])
(:import (org.joda.time DateTime)))

Expand All @@ -14,6 +15,8 @@

(def json-file-encoding "UTF-8")

(def markdown-command "markdown")

(declare list-posts read-post read-json-file relative-post-output-dir)

(defn -main [root]
Expand All @@ -22,17 +25,36 @@
(doseq [post (map read-post (list-posts (f root "posts")))
:let [post-out-dir (f root "out" (relative-post-output-dir post))]]
(.mkdirs post-out-dir)
(spit (f post-out-dir "index.html") "")))
(spit (f post-out-dir "index.html") (get post "text"))))

(defn list-posts [node]
(if (.exists (f node post-meta-file-name))
(list node)
(mapcat list-posts (.listFiles node))))

(defn- slurp-shell-out
"Same as clojure.java.shell/sh, but returns only the stdout of the
sub-process. An exception is thrown if the sub-process exits with
non-zero status. This function does not touch the :out-enc option."
[command & args]
(let [{:keys [exit, out], :as m} (apply sh/sh command args)]
(if-not (zero? exit)
(throw (RuntimeException.
(str "Command exited with non-zero status: " command)))
out)))

(defn render-and-slurp-markdown
"Process the file through Markdown and slurp the result as a
string. The resulting HTML text is decoded using UTF-8."
[file]
(slurp-shell-out markdown-command (str file) :out-enc markdown-file-encoding))

(defn read-post [post-dir]
(let [meta (read-json-file (f post-dir post-meta-file-name))
text (slurp (f post-dir post-text-file-name) :encoding markdown-file-encoding)]
(assoc meta post-text-key text, "published" (DateTime/parse (get meta "published")))))
text (render-and-slurp-markdown (f post-dir post-text-file-name))]
(assoc meta
post-text-key text
"published" (DateTime/parse (get meta "published")))))

(defn- read-json-file [file]
(with-open [in (io/reader file :encoding json-file-encoding)]
Expand Down
25 changes: 14 additions & 11 deletions test/bokareis/main_test.clj
@@ -1,7 +1,8 @@
(ns bokareis.main-test
(:use midje.sweet
bokareis.main)
(:use [clojure.java.io :only [file] :rename {file f}])
bokareis.main
[clojure.java.io :only [file] :rename {file f}]
[clojure.data.json :only [json-str]])
(:import (java.io File)
(org.joda.time DateTime)))

Expand Down Expand Up @@ -43,15 +44,16 @@
(fact "application renders posts"
(with-temp-dir root
(create-post (f root "posts")
"{\"slug\": \"hello-world\", \"published\": \"2012-02-11T14:47:00Z\"}"
"Hello world")
(json-str {"slug" "hello-world"
"published" "2012-02-11T14:47:00Z"})
"# Hello world\n\nThis is a paragraph\n")
(spit (f root "blog.meta")
"{}")
(-main (str root))
(.exists (f root "out" "index.html"))
=> truthy
(.exists (f root "out" "2012" "02" "11" "hello-world" "index.html"))
=> truthy))
(slurp (f root "out" "2012" "02" "11" "hello-world" "index.html"))
=> (contains "<h1>Hello world</h1>")))

(fact "list-post finds a post"
(with-temp-dir root
Expand Down Expand Up @@ -81,15 +83,16 @@
(list-posts root)
=> []))

(fact
(fact "read-post can read a post"
(with-temp-dir root
(create-post (f root "a")
"{\"slug\": \"foo-bar\", \"published\": \"2012-02-11T14:47:00Z\"}"
(json-str {"slug" "foo-bar"
"published" "2012-02-11T14:47:00Z"})
"Foo bar.")
(read-post (f root "a"))
=> {"slug" "foo-bar"
"published" (DateTime/parse "2012-02-11T14:47:00Z")
"text" "Foo bar."}))
=> (contains {"slug" "foo-bar"
"published" (DateTime/parse "2012-02-11T14:47:00Z")
"text" (contains "Foo bar.")})))

(fact
(relative-post-output-dir {"slug" "foo-bar"
Expand Down

0 comments on commit 63875d0

Please sign in to comment.