2014-12-05 15:56:40 +00:00
|
|
|
(ns cryogen-core.compiler
|
2014-12-04 16:38:48 +00:00
|
|
|
(:require [selmer.parser :refer [cache-off! render-file]]
|
2015-06-14 10:23:58 +00:00
|
|
|
[selmer.util :refer [set-custom-resource-path!]]
|
2014-12-05 15:56:40 +00:00
|
|
|
[cryogen-core.io :refer
|
2015-06-14 15:16:35 +00:00
|
|
|
[get-resource find-assets create-folder wipe-public-folder copy-resources
|
|
|
|
copy-resources-from-theme]]
|
2014-12-05 15:56:40 +00:00
|
|
|
[cryogen-core.sitemap :as sitemap]
|
|
|
|
[cryogen-core.rss :as rss]
|
2014-12-04 16:38:48 +00:00
|
|
|
[io.aviso.exception :refer [write-exception]]
|
|
|
|
[clojure.java.io :refer [copy file reader writer]]
|
|
|
|
[clojure.string :as s]
|
|
|
|
[text-decoration.core :refer :all]
|
2014-12-05 15:56:40 +00:00
|
|
|
[cryogen-core.toc :refer [generate-toc]]
|
2014-12-30 12:31:46 +00:00
|
|
|
[cryogen-core.sass :as sass]
|
|
|
|
[cryogen-core.markup :as m]))
|
2014-12-04 16:38:48 +00:00
|
|
|
|
|
|
|
(cache-off!)
|
|
|
|
|
2014-12-05 21:37:04 +00:00
|
|
|
(def public "resources/public")
|
|
|
|
|
|
|
|
(defn root-path
|
|
|
|
"Creates the root path for posts, tags and pages"
|
|
|
|
[config k]
|
2014-12-04 16:38:48 +00:00
|
|
|
(if-let [root (k config)]
|
|
|
|
(str "/" root "/") "/"))
|
|
|
|
|
2015-01-28 21:19:01 +00:00
|
|
|
(defn re-pattern-from-ext
|
|
|
|
"Creates a properly quoted regex pattern for the given file extension"
|
|
|
|
[ext]
|
|
|
|
(re-pattern (str (s/replace ext "." "\\.") "$")))
|
|
|
|
|
2014-12-05 21:37:04 +00:00
|
|
|
(defn find-posts
|
|
|
|
"Returns a list of markdown files representing posts under the post root in templates/md"
|
2014-12-30 12:31:46 +00:00
|
|
|
[{:keys [post-root ignored-files]} mu]
|
|
|
|
(find-assets (str "templates/" (m/dir mu) post-root) (m/ext mu) ignored-files))
|
2014-12-04 16:38:48 +00:00
|
|
|
|
2014-12-05 21:37:04 +00:00
|
|
|
(defn find-pages
|
|
|
|
"Returns a list of markdown files representing pages under the page root in templates/md"
|
2014-12-30 12:31:46 +00:00
|
|
|
[{:keys [page-root ignored-files]} mu]
|
|
|
|
(find-assets (str "templates/" (m/dir mu) page-root) (m/ext mu) ignored-files))
|
2014-12-04 16:38:48 +00:00
|
|
|
|
2014-12-05 21:37:04 +00:00
|
|
|
(defn parse-post-date
|
|
|
|
"Parses the post date from the post's file name and returns the corresponding java date object"
|
|
|
|
[file-name date-fmt]
|
2014-12-04 16:38:48 +00:00
|
|
|
(let [fmt (java.text.SimpleDateFormat. date-fmt)]
|
|
|
|
(.parse fmt (.substring file-name 0 10))))
|
|
|
|
|
2014-12-05 21:37:04 +00:00
|
|
|
(defn post-uri
|
|
|
|
"Creates a post uri from the post file name"
|
2014-12-30 12:31:46 +00:00
|
|
|
[file-name {:keys [blog-prefix post-root]} mu]
|
2015-01-28 21:19:01 +00:00
|
|
|
(str blog-prefix post-root (s/replace file-name (re-pattern-from-ext (m/ext mu)) ".html")))
|
2014-12-04 16:38:48 +00:00
|
|
|
|
2014-12-05 21:37:04 +00:00
|
|
|
(defn page-uri
|
|
|
|
"Creates a page uri from the page file name"
|
2014-12-30 12:31:46 +00:00
|
|
|
[page-name {:keys [blog-prefix page-root]} mu]
|
2015-01-28 21:19:01 +00:00
|
|
|
(str blog-prefix page-root (s/replace page-name (re-pattern-from-ext (m/ext mu)) ".html")))
|
2014-12-04 16:38:48 +00:00
|
|
|
|
2014-12-05 21:37:04 +00:00
|
|
|
(defn read-page-meta
|
|
|
|
"Returns the clojure map from the top of a markdown page/post"
|
|
|
|
[page rdr]
|
2014-12-04 16:38:48 +00:00
|
|
|
(try
|
|
|
|
(read rdr)
|
|
|
|
(catch Exception _
|
|
|
|
(throw (IllegalArgumentException. (str "Malformed metadata on page: " page))))))
|
|
|
|
|
2014-12-30 12:31:46 +00:00
|
|
|
(defn page-content
|
|
|
|
"Returns a map with the given page's file-name, metadata and content parsed from
|
|
|
|
the file with the given markup."
|
|
|
|
[page config markup]
|
2014-12-04 16:38:48 +00:00
|
|
|
(with-open [rdr (java.io.PushbackReader. (reader page))]
|
|
|
|
(let [page-name (.getName page)
|
2015-01-28 21:19:01 +00:00
|
|
|
file-name (s/replace page-name (re-pattern-from-ext (m/ext markup)) ".html")
|
2014-12-04 16:38:48 +00:00
|
|
|
page-meta (read-page-meta page-name rdr)
|
2014-12-30 12:31:46 +00:00
|
|
|
content ((m/render-fn markup) rdr config)]
|
|
|
|
{:file-name file-name
|
|
|
|
:page-meta page-meta
|
|
|
|
:content content})))
|
|
|
|
|
|
|
|
(defn merge-meta-and-content
|
|
|
|
"Merges the page metadata and content maps, adding :toc if necessary."
|
|
|
|
[file-name page-meta content]
|
|
|
|
(merge
|
|
|
|
(update-in page-meta [:layout] #(str (name %) ".html"))
|
|
|
|
{:file-name file-name
|
|
|
|
:content content
|
|
|
|
:toc (if (:toc page-meta) (generate-toc content))}))
|
|
|
|
|
|
|
|
(defn parse-page
|
|
|
|
"Parses a page/post and returns a map of the content, uri, date etc."
|
|
|
|
[page config markup]
|
|
|
|
(let [{:keys [file-name page-meta content]} (page-content page config markup)]
|
|
|
|
(merge
|
|
|
|
(merge-meta-and-content file-name page-meta content)
|
|
|
|
{:uri (page-uri file-name config markup)
|
|
|
|
:page-index (:page-index page-meta)})))
|
|
|
|
|
|
|
|
(defn parse-post
|
|
|
|
"Return a map with the given post's information."
|
|
|
|
[page config markup]
|
|
|
|
(let [{:keys [file-name page-meta content]} (page-content page config markup)]
|
|
|
|
(merge
|
|
|
|
(merge-meta-and-content file-name page-meta content)
|
|
|
|
(let [date (parse-post-date file-name (:post-date-format config))
|
|
|
|
archive-fmt (java.text.SimpleDateFormat. "yyyy MMMM" (java.util.Locale. "en"))
|
|
|
|
formatted-group (.format archive-fmt date)]
|
|
|
|
{:date date
|
|
|
|
:formatted-archive-group formatted-group
|
|
|
|
:parsed-archive-group (.parse archive-fmt formatted-group)
|
|
|
|
:uri (post-uri file-name config markup)
|
2015-06-14 15:16:35 +00:00
|
|
|
:tags (set (:tags page-meta))}))))
|
2014-12-04 16:38:48 +00:00
|
|
|
|
2014-12-05 21:37:04 +00:00
|
|
|
(defn read-posts
|
|
|
|
"Returns a sequence of maps representing the data from markdown files of posts.
|
|
|
|
Sorts the sequence by post date."
|
|
|
|
[config]
|
2014-12-30 12:31:46 +00:00
|
|
|
(->> (mapcat
|
|
|
|
(fn [mu]
|
|
|
|
(->>
|
|
|
|
(find-posts config mu)
|
|
|
|
(map #(parse-post % config mu))))
|
|
|
|
(m/markups))
|
2014-12-04 16:38:48 +00:00
|
|
|
(sort-by :date)
|
|
|
|
reverse))
|
|
|
|
|
2014-12-05 21:37:04 +00:00
|
|
|
(defn read-pages
|
|
|
|
"Returns a sequence of maps representing the data from markdown files of pages.
|
2014-12-30 12:31:46 +00:00
|
|
|
Sorts the sequence by post date."
|
2014-12-05 21:37:04 +00:00
|
|
|
[config]
|
2014-12-30 12:31:46 +00:00
|
|
|
(->> (mapcat
|
|
|
|
(fn [mu]
|
|
|
|
(->>
|
|
|
|
(find-pages config mu)
|
|
|
|
(map #(parse-page % config mu))))
|
|
|
|
(m/markups))
|
2014-12-04 16:38:48 +00:00
|
|
|
(sort-by :page-index)))
|
|
|
|
|
2014-12-05 21:37:04 +00:00
|
|
|
(defn tag-post
|
|
|
|
"Adds the uri and title of a post to the list of posts under each of its tags"
|
|
|
|
[tags post]
|
2014-12-04 16:38:48 +00:00
|
|
|
(reduce (fn [tags tag]
|
2015-06-23 14:08:26 +00:00
|
|
|
(update-in tags [tag] (fnil conj []) (select-keys post [:uri :title :content :date])))
|
2014-12-04 16:38:48 +00:00
|
|
|
tags (:tags post)))
|
|
|
|
|
2014-12-05 21:37:04 +00:00
|
|
|
(defn group-by-tags
|
|
|
|
"Maps all the tags with a list of posts that contain each tag"
|
|
|
|
[posts]
|
2014-12-04 16:38:48 +00:00
|
|
|
(reduce tag-post {} posts))
|
|
|
|
|
2014-12-05 21:37:04 +00:00
|
|
|
(defn group-for-archive
|
|
|
|
"Groups the posts by month and year for archive sorting"
|
|
|
|
[posts]
|
2014-12-04 16:38:48 +00:00
|
|
|
(->> posts
|
|
|
|
(map #(select-keys % [:title :uri :date :formatted-archive-group :parsed-archive-group]))
|
|
|
|
(group-by :formatted-archive-group)
|
|
|
|
(map (fn [[group posts]]
|
|
|
|
{:group group
|
|
|
|
:parsed-group (:parsed-archive-group (get posts 0))
|
|
|
|
:posts (map #(select-keys % [:title :uri :date]) posts)}))
|
|
|
|
(sort-by :parsed-group)
|
|
|
|
reverse))
|
|
|
|
|
2014-12-05 21:37:04 +00:00
|
|
|
(defn tag-info
|
|
|
|
"Returns a map containing the name and uri of the specified tag"
|
|
|
|
[{:keys [blog-prefix tag-root]} tag]
|
2014-12-04 16:38:48 +00:00
|
|
|
{:name (name tag)
|
|
|
|
:uri (str blog-prefix tag-root (name tag) ".html")})
|
|
|
|
|
2014-12-05 21:37:04 +00:00
|
|
|
(defn add-prev-next
|
|
|
|
"Adds a :prev and :next key to the page/post data containing the title and uri of the prev/next
|
|
|
|
post/page if it exists"
|
|
|
|
[pages]
|
2014-12-04 16:38:48 +00:00
|
|
|
(map (fn [[prev target next]]
|
|
|
|
(assoc target
|
|
|
|
:prev (if prev (select-keys prev [:title :uri]) nil)
|
|
|
|
:next (if next (select-keys next [:title :uri]) nil)))
|
|
|
|
(partition 3 1 (flatten [nil pages nil]))))
|
|
|
|
|
2014-12-05 21:37:04 +00:00
|
|
|
(defn group-pages
|
|
|
|
"Separates the pages into links for the navbar and links for the sidebar"
|
|
|
|
[pages]
|
2014-12-04 16:38:48 +00:00
|
|
|
(let [{navbar-pages true
|
|
|
|
sidebar-pages false} (group-by #(boolean (:navbar? %)) pages)]
|
|
|
|
(map (partial sort-by :page-index) [navbar-pages sidebar-pages])))
|
|
|
|
|
2014-12-05 21:37:04 +00:00
|
|
|
(defn compile-pages
|
|
|
|
"Compiles all the pages into html and spits them out into the public folder"
|
2015-02-23 18:14:31 +00:00
|
|
|
[{:keys [blog-prefix page-root] :as params} pages]
|
2014-12-04 16:38:48 +00:00
|
|
|
(when-not (empty? pages)
|
|
|
|
(println (blue "compiling pages"))
|
|
|
|
(create-folder (str blog-prefix page-root))
|
|
|
|
(doseq [{:keys [uri] :as page} pages]
|
|
|
|
(println "\t-->" (cyan uri))
|
|
|
|
(spit (str public uri)
|
2015-07-06 02:02:42 +00:00
|
|
|
(render-file (str "/html/" (:layout page))
|
2015-02-23 18:14:31 +00:00
|
|
|
(merge params
|
2014-12-04 16:38:48 +00:00
|
|
|
{:servlet-context "../"
|
2014-12-05 21:37:04 +00:00
|
|
|
:page page
|
2015-01-01 04:21:38 +00:00
|
|
|
:uri uri}))))))
|
2014-12-04 16:38:48 +00:00
|
|
|
|
2014-12-05 21:37:04 +00:00
|
|
|
(defn compile-posts
|
|
|
|
"Compiles all the posts into html and spits them out into the public folder"
|
2015-02-23 18:14:31 +00:00
|
|
|
[{:keys [blog-prefix post-root disqus-shortname] :as params} posts]
|
2014-12-04 16:38:48 +00:00
|
|
|
(when-not (empty? posts)
|
|
|
|
(println (blue "compiling posts"))
|
|
|
|
(create-folder (str blog-prefix post-root))
|
|
|
|
(doseq [post posts]
|
|
|
|
(println "\t-->" (cyan (:uri post)))
|
|
|
|
(spit (str public (:uri post))
|
2015-06-14 15:16:35 +00:00
|
|
|
(render-file (str "/html/" (:layout post))
|
2015-02-23 18:14:31 +00:00
|
|
|
(merge params
|
2014-12-04 16:38:48 +00:00
|
|
|
{:servlet-context "../"
|
|
|
|
:post post
|
2014-12-05 21:37:04 +00:00
|
|
|
:disqus-shortname disqus-shortname
|
2015-01-01 04:21:38 +00:00
|
|
|
:uri (:uri post)}))))))
|
2014-12-04 16:38:48 +00:00
|
|
|
|
2014-12-05 21:37:04 +00:00
|
|
|
(defn compile-tags
|
|
|
|
"Compiles all the tag pages into html and spits them out into the public folder"
|
2015-02-23 18:14:31 +00:00
|
|
|
[{:keys [blog-prefix tag-root] :as params} posts-by-tag]
|
2014-12-04 16:38:48 +00:00
|
|
|
(when-not (empty? posts-by-tag)
|
|
|
|
(println (blue "compiling tags"))
|
|
|
|
(create-folder (str blog-prefix tag-root))
|
|
|
|
(doseq [[tag posts] posts-by-tag]
|
2015-02-23 18:14:31 +00:00
|
|
|
(let [{:keys [name uri]} (tag-info params tag)]
|
2014-12-04 16:38:48 +00:00
|
|
|
(println "\t-->" (cyan uri))
|
|
|
|
(spit (str public uri)
|
2015-06-14 15:16:35 +00:00
|
|
|
(render-file "/html/tag.html"
|
2015-02-23 18:14:31 +00:00
|
|
|
(merge params
|
2015-01-01 04:21:38 +00:00
|
|
|
{:servlet-context "../"
|
|
|
|
:name name
|
|
|
|
:posts posts
|
|
|
|
:uri uri})))))))
|
2014-12-04 16:38:48 +00:00
|
|
|
|
2014-12-05 21:37:04 +00:00
|
|
|
(defn compile-index
|
|
|
|
"Compiles the index page into html and spits it out into the public folder"
|
2015-02-23 18:14:31 +00:00
|
|
|
[{:keys [blog-prefix disqus?] :as params}]
|
2014-12-04 16:38:48 +00:00
|
|
|
(println (blue "compiling index"))
|
|
|
|
(spit (str public blog-prefix "/index.html")
|
2015-06-14 15:16:35 +00:00
|
|
|
(render-file "/html/home.html"
|
2015-02-23 18:14:31 +00:00
|
|
|
(merge params
|
2014-12-31 21:55:29 +00:00
|
|
|
{:home true
|
|
|
|
:disqus? disqus?
|
2015-02-23 18:14:31 +00:00
|
|
|
:post (get-in params [:latest-posts 0])
|
2015-01-01 04:21:38 +00:00
|
|
|
:uri (str blog-prefix "/index.html")}))))
|
2014-12-04 16:38:48 +00:00
|
|
|
|
2014-12-05 21:37:04 +00:00
|
|
|
(defn compile-archives
|
|
|
|
"Compiles the archives page into html and spits it out into the public folder"
|
2015-02-23 18:14:31 +00:00
|
|
|
[{:keys [blog-prefix] :as params} posts]
|
2014-12-04 16:38:48 +00:00
|
|
|
(println (blue "compiling archives"))
|
|
|
|
(spit (str public blog-prefix "/archives.html")
|
2015-06-14 15:16:35 +00:00
|
|
|
(render-file "/html/archives.html"
|
2015-02-23 18:14:31 +00:00
|
|
|
(merge params
|
2014-12-04 16:38:48 +00:00
|
|
|
{:archives true
|
2015-01-01 04:21:38 +00:00
|
|
|
:groups (group-for-archive posts)
|
|
|
|
:uri (str blog-prefix "/archives.html")}))))
|
2014-12-04 16:38:48 +00:00
|
|
|
|
2014-12-05 21:37:04 +00:00
|
|
|
(defn tag-posts
|
|
|
|
"Converts the tags in each post into links"
|
|
|
|
[posts config]
|
2014-12-04 16:38:48 +00:00
|
|
|
(map #(update-in % [:tags] (partial map (partial tag-info config))) posts))
|
|
|
|
|
2015-06-14 15:16:35 +00:00
|
|
|
(defn copy-resources-from-markup-folders
|
2015-06-14 05:47:18 +00:00
|
|
|
"Copy resources from markup folders"
|
|
|
|
[config]
|
|
|
|
(copy-resources
|
|
|
|
(merge config
|
|
|
|
{:resources (for [mu (m/markups)
|
|
|
|
t ["posts" "pages"]] (str (m/dir mu) "/" t))
|
|
|
|
:ignored-files (map #(re-pattern-from-ext (m/ext %)) (m/markups))})))
|
|
|
|
|
2014-12-05 21:37:04 +00:00
|
|
|
(defn read-config
|
|
|
|
"Reads the config file"
|
|
|
|
[]
|
2015-04-03 02:22:17 +00:00
|
|
|
(try
|
|
|
|
(let [config (-> "templates/config.edn"
|
|
|
|
get-resource
|
|
|
|
slurp
|
|
|
|
read-string
|
|
|
|
(update-in [:blog-prefix] (fnil str ""))
|
|
|
|
(update-in [:rss-name] (fnil str "rss.xml"))
|
|
|
|
(update-in [:rss-filters] (fnil seq []))
|
|
|
|
(update-in [:sass-src] (fnil str "css"))
|
|
|
|
(update-in [:sass-dest] (fnil str "css"))
|
|
|
|
(update-in [:post-date-format] (fnil str "yyyy-MM-dd"))
|
|
|
|
(update-in [:keep-files] (fnil seq []))
|
|
|
|
(update-in [:ignored-files] (fnil seq [#"^\.#.*" #".*\.swp$"])))]
|
|
|
|
(merge
|
|
|
|
config
|
|
|
|
{:page-root (root-path :page-root config)
|
|
|
|
:post-root (root-path :post-root config)
|
|
|
|
:tag-root (root-path :tag-root config)}))
|
|
|
|
(catch Exception _
|
|
|
|
(throw (IllegalArgumentException. "Failed to parse config.edn")))))
|
2014-12-04 16:38:48 +00:00
|
|
|
|
2014-12-05 21:37:04 +00:00
|
|
|
(defn compile-assets
|
|
|
|
"Generates all the html and copies over resources specified in the config"
|
|
|
|
[]
|
2014-12-04 16:38:48 +00:00
|
|
|
(println (green "compiling assets..."))
|
2014-12-27 07:38:25 +00:00
|
|
|
(let [{:keys [site-url blog-prefix rss-name recent-posts sass-src sass-dest keep-files ignored-files] :as config} (read-config)
|
2014-12-04 16:38:48 +00:00
|
|
|
posts (add-prev-next (read-posts config))
|
|
|
|
pages (add-prev-next (read-pages config))
|
|
|
|
[navbar-pages sidebar-pages] (group-pages pages)
|
|
|
|
posts-by-tag (group-by-tags posts)
|
|
|
|
posts (tag-posts posts config)
|
2015-02-23 18:14:31 +00:00
|
|
|
params (merge config
|
|
|
|
{:title (:site-title config)
|
|
|
|
:tags (map (partial tag-info config) (keys posts-by-tag))
|
|
|
|
:latest-posts (->> posts (take recent-posts) vec)
|
|
|
|
:navbar-pages navbar-pages
|
|
|
|
:sidebar-pages sidebar-pages
|
|
|
|
:archives-uri (str blog-prefix "/archives.html")
|
|
|
|
:index-uri (str blog-prefix "/index.html")
|
|
|
|
:rss-uri (str blog-prefix "/" rss-name)
|
2015-06-14 10:23:58 +00:00
|
|
|
:site-url (if (.endsWith site-url "/") (.substring site-url 0 (dec (count site-url))) site-url)
|
2015-06-14 15:16:35 +00:00
|
|
|
:theme-path (str "file:resources/templates/themes/" (:theme config))})]
|
2014-12-04 16:38:48 +00:00
|
|
|
|
2015-06-14 15:16:35 +00:00
|
|
|
(set-custom-resource-path! (:theme-path params))
|
2014-12-04 16:38:48 +00:00
|
|
|
(wipe-public-folder keep-files)
|
2015-06-14 10:23:58 +00:00
|
|
|
(println (blue "copying theme resources"))
|
|
|
|
(copy-resources-from-theme config)
|
2014-12-04 16:38:48 +00:00
|
|
|
(println (blue "copying resources"))
|
|
|
|
(copy-resources config)
|
2015-06-14 15:16:35 +00:00
|
|
|
(copy-resources-from-markup-folders config)
|
2015-02-23 18:14:31 +00:00
|
|
|
(compile-pages params pages)
|
|
|
|
(compile-posts params posts)
|
|
|
|
(compile-tags params posts-by-tag)
|
|
|
|
(compile-index params)
|
|
|
|
(compile-archives params posts)
|
2014-12-04 16:38:48 +00:00
|
|
|
(println (blue "generating site map"))
|
2014-12-27 07:38:25 +00:00
|
|
|
(spit (str public blog-prefix "/sitemap.xml") (sitemap/generate site-url ignored-files))
|
2015-01-04 01:36:30 +00:00
|
|
|
(println (blue "generating main rss"))
|
2014-12-04 16:38:48 +00:00
|
|
|
(spit (str public blog-prefix "/" rss-name) (rss/make-channel config posts))
|
2015-01-04 01:36:30 +00:00
|
|
|
(println (blue "generating filtered rss"))
|
|
|
|
(rss/make-filtered-channels public config posts-by-tag)
|
2014-12-04 16:38:48 +00:00
|
|
|
(println (blue "compiling sass"))
|
2014-12-25 16:24:25 +00:00
|
|
|
(sass/compile-sass->css!
|
2015-02-21 23:23:05 +00:00
|
|
|
{:src-sass sass-src
|
|
|
|
:dest-sass (str "../public" blog-prefix "/" sass-dest)
|
|
|
|
:ignored-files ignored-files
|
|
|
|
:base-dir "resources/templates/"})))
|
2014-12-04 16:38:48 +00:00
|
|
|
|
|
|
|
(defn compile-assets-timed []
|
|
|
|
(time
|
|
|
|
(try
|
|
|
|
(compile-assets)
|
|
|
|
(catch Exception e
|
2015-01-01 08:49:03 +00:00
|
|
|
(if (or (instance? IllegalArgumentException e)
|
|
|
|
(instance? clojure.lang.ExceptionInfo e))
|
2014-12-04 16:38:48 +00:00
|
|
|
(println (red "Error:") (yellow (.getMessage e)))
|
|
|
|
(write-exception e))))))
|