Permalink
Browse files

Render post content.

  • Loading branch information...
raek committed Feb 23, 2012
1 parent 69eb381 commit 63875d0b7b975fd6393c6b8dc9f2fdab4b44a008
Showing with 40 additions and 15 deletions.
  1. +26 −4 src/bokareis/main.clj
  2. +14 −11 test/bokareis/main_test.clj
View
@@ -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)))
@@ -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]
@@ -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)]
@@ -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)))
@@ -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
@@ -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"

0 comments on commit 63875d0

Please sign in to comment.