Yet another blog engine#
Mon Feb 16 21:35:11 2015
Yablog is the unimaginatively
named "Yet Another Blog engine" and is what's now behind the blog at
ww.telent.net - mostly because every third attempt to post with
my-way ended up with having to
relearn out how all the baroque git hooks worked.
It was supposed to be a two-hour Tuesday morning hack, but it's now almost the following Tuesday morning as I did not anticipate that
- I would need to write my own Textile parser (see https://github.com/telent/texticlj )
- I would need to write my own date parser (see
https://github.com/telent/yablog/blob/master/src/yablog/time.clj and
https://github.com/telent/yablog/blob/master/resources/dates.bnf ). Instaparse
is awesome, it took less time to do this than it was taking to find
and download other people's date parsers and find that they didn't
support all the date formts I needed to parse
- I would get sucked into the fiddling-around-with-CSS vortex
This entry is mostly just a placeholder to check the new posting process
works, but do check out Instaparse if you haven't used it already.
Regaining my compojure#
Thu Feb 19 10:16:22 2015
Picking up $secret_project which I put down in November to do Sledge
and Yablog, I find that the routing of http requests is a horrendous
mess based on substring matching and ad hoc argument parsing, and
would really benefit from some Compojure
(Now, that is, that I've actually got my head around how Compojure
works)
Because I'm using Hiccup to
compose pages out of parts, I thought it would be neat if I could
return hiccup page bodies directly as responses so that they get
uniformly topped and tailed and turned into response maps. Turns out to be
fairly straightforward:
- define a type for the hiccup response
- extend the
compojure.response/Renderable
protocol to deal
with the new type
(deftype Hiccupage [body])
(defn page-surround [page]
(let [head [:head
[:link {:rel "stylesheet" :type "text/css"
:href "/static/default.css"}]
[:script {:src "/static/stuff.js"}]
[:title "Hey you"]]]
(into head (.body page))))
(extend-protocol compojure.response/Renderable
Hiccupage
(render [page req]
{:status 200
:headers {"Content-Type" "text/html; charset=utf-8"}
:body (hiccup.page/html5 (page-surround page))}))
Now we can return Hiccupage (hiccup page? geddit? I'm still looking for a
better name, yes) objects directly from our routes
(defroutes app
(GET "/hello/:name" [name]
(Hiccupage.
[[:h1 "hello"]
[:p "hello " name]
[:p "This is text. I like text"]]))
...
)