Another Year, Another Blog

November 12, 2014

Welcome to the first post of what is probably my 3rd or 4th attempt at maintaining a blog. The first few times didn't blow over too well. I'm not one for creative writing and my lack of motivation eventually led to the abandonment of my previous blogging attempts.

The last time I tried to maintain a blog was probably two to three years ago and I was working with WordPress. The templates weren't very palateable and neither was the code. The admin interface was nice but it just wasn't worth it if I only wanted to serve up static content. I recall trying out blogspot but that wasn't my cup of tea either.

Enter Misaki

Originally, I tried to use Misaki, a Jekyll inspired static site generator written in Clojure. It was simple at first. There were a few included hiccup layouts and a few example posts (also written in hiccup). But I soon found that this was going to be much more complicated than I had anticipated.

I encountered my first problem when I wanted to display the most recent blog entry on the home page. After searching through the API, I concluded that this was the best and only way to display the contents of the latest post:

[:div
   (->> (:posts site)
        first
        :lazy-content
        force)]

Except... this wasn't quite right. Misaki's compiler escaped html tags when it populated the lazy content. I guess this was an easy fix though. I simply modified function from the line of code where the lazy content was populated.

My biggest complaint was that Misaki didn't have out of the box support for Markdown posts. There's actually another version called Misaki Markdown but any pages that I wanted to add had to be written in markdown as well. What I wanted was to be able to create custom pages with html/css and write my posts in markdown.

I ended up altering misaki.server in order to preprocess markdown posts into html posts before compiling the entire site. The first three functions below are what I added.

(defn- find-assets [f ext]
  (->> f
       file-seq
       (filter (fn [file] (-> file .getName (.endsWith ext))))
       rest))

(defn preprocess [file]
  (with-open [rdr (clojure.java.io/reader file)]
    (->> (doall (line-seq rdr))
         (split-with #(.startsWith (.trim %) ";"))
         (map #(clojure.string/join "\n" %)))))

(defn compile-markdown []
  (doseq [file (find-assets (clojure.java.io/file "md") "md")]
    (spit (str "my-blog/template/posts/"
               (first (clojure.string/split (.getName file) #"\."))
               ".html.clj")
          (let [[header content] (preprocess file)]
            (str header "\n"  "[:div " (with-out-str (pprint (md/to-html content))) "]")))))

; =do-all-compile
(defn do-all-compile
  []
  (compile-markdown)
  (print-compile-result "all templates" (call-all-compile))
  (println " * Finish Compiling"))

Setting up my site to my specifications took way longer than it should have and I couldn't help but think there must be another way.

Introducing Cryogen

All this resulted in Cryogen, a simple static site generator written in Clojure by Yogthos and I.

Features

  • Blog posts and pages with Markdown
    • Twitter Bootstrap theming
    • Plain HTML page templates with Selmer
    • Code syntax highlighting with Highlight.js
    • Tags
    • Sitemap
    • RSS feed

Setting Up Cryogen

The easiest way to get Cryogen set up is to use Leiningen. Once you have Leiningen 2.5.0 or above installed, simply run the following command to create a new cryogen template.

$ lein new cryogen <project name>

Alternatively, you can put cryogen in the Leningen Template to use: field if you are creating a new Leiningen project in Eclipse with Counterclockwise.

Project Structure

The new project will have the following structure:

my-blog
|---.gitignore
|---project.clj
|---resources
|   |---config.edn
|   |---templates
|   |   |---css
|   |   |   |---screen.css
|   |   |---js
|   |   |   |---main.js
|   |   |---img
|   |   |   |---favicon.png
|   |   |---html
|   |   |   |---layouts
|   |   |       |---archives.html
|   |   |       |---base.html
|   |   |       |---home.html
|   |   |       |---page.html
|   |   |       |---post.html
|   |   |       |---tag.html
|   |   |---md
|   |       |---pages
|   |       |   |---about.md
|   |       |   |---another-page.md
|   |       |---posts
|   |           |---10-03-2014-first-post.md
|   |           |---11-04-2014-second-post.md
|   |           |---13-11-2014-docs.md
|---src
|   |---cryogen
|   |   |---compiler.clj
|   |   |---io.clj
|   |   |---rss.clj
|   |   |---server.clj
|   |   |---sitemap.clj
|   |   |---watcher.clj

The skeleton site has a few example posts and pages in it.

  • templates/md - blog posts written in Markdown
    • templates/pages- custom pages written in Markdown
    • templates/css - save your custom CSS files
    • templates/js - save your custom js files here

Compiling Posts and Running the Server

The server can be started using the lein-ring plugin. Running the following in the root folder of your Cryogen project will compile your resources to resources/public then start the site at http://localhost:3000

$ lein ring server

That's it for getting the basic template up and running. Additional docs can be found on the github page.


So here we go! Another attempt at maintaining a blog - this time using Cryogen. It's actually my first noteworthy project using Clojure and hopefully that will encourage me to write here regularly. I'd also like to give a huge thanks to Yogthos for helping me out with my first project!

Tags: cryogen clojure misaki

Comments