rlm@1: #+title: Serving a blog using Clojure rlm@1: #+author: Robert McIntyre rlm@1: #+MATHJAX: align:"left" mathml:t path:"../MathJax/MathJax.js" rlm@1: #+STYLE: rlm@1: #+BEGIN_HTML rlm@1:

{{{title}}}

rlm@1: #+END_HTML rlm@1: rlm@1: * Aurellem Export Program rlm@1: #+srcname: publish rlm@1: #+begin_src clojure :results silent rlm@1: (ns aurellem.publish rlm@1: (:use rlm.ns-rlm)) rlm@1: (rlm.ns-rlm/ns-clone rlm.light-base) rlm@1: (import org.htmlcleaner.HtmlCleaner) rlm@1: (import org.htmlcleaner.TagNode) rlm@1: (import java.io.File) rlm@1: (import java.net.URL) rlm@1: (import org.apache.commons.io.FileUtils) rlm@1: (use 'clojure.java.io) rlm@1: (use 'rlm.sanitize-file) rlm@1: (use 'net.cgrand.enlive-html) rlm@1: (use 'rlm.pikasemechu) rlm@1: (require 'rlm.push) rlm@1: (use 'clojure.contrib.shell-out) rlm@1: rlm@1: (declare publish) rlm@1: rlm@1: rlm@1: (defvar *exports* rlm@1: [(file-str "~/aurellem/src/pokemon/types.html") rlm@1: (file-str "~/aurellem/src/pokemon/lpsolve.html") rlm@1: (file-str "~/aurellem/src/abomination/no_parens.html") rlm@1: (file-str "/home/r/aurellem/src/qm/quandary.html") rlm@1: (file-str "/home/r/cortex/cortex.html") rlm@1: (file-str "/home/r/cortex/capture-video.html")] rlm@1: "The html files which will be exported to the auerllem rlm@1: website. Listed in the order they will appear on the site rlm@1: to add more entries to the site, add them here.") rlm@1: rlm@1: (defvar *other-files* rlm@1: [(file-str "/home/r/aurellem/src/MathJax/") rlm@1: (file-str "/home/r/aurellem/src/css/aurellem.css") rlm@1: (file-str "/home/r/aurellem/src/js/jquery.min.js") rlm@1: (file-str "/home/r/aurellem/src/aurellem/err.html") rlm@1: (file-str "/home/r/cortex/sources/turing.pdf") rlm@1: (file-str "/home/r/cortex/images/brick-wall-standing.jpg") rlm@1: (file-str "/home/r/cortex/images/brick-wall-knocked-down.jpg") rlm@1: (file-str "/home/r/cortex/images/dominos.jpg") rlm@1: (file-str "/home/r/cortex/images/simple-app.jpg")] rlm@1: rlm@1: "other files needed by the website, but which are not posts.") rlm@1: rlm@1: (defvar *index* rlm@1: (file-str "~/aurellem/src/aurellem/index.html") rlm@1: "this is the main index.html file for the site. It will be updated rlm@1: with new posts") rlm@1: rlm@1: (defvar *site* rlm@1: (file-str "~/aurellem/site-output") rlm@1: "the target output directoty for the site's content") rlm@1: rlm@1: (defvar *export-base* (file-str "~/")) rlm@1: rlm@1: (defn target-file [#^File file] rlm@1: (file-str rlm@1: (.replace (.getCanonicalPath file) rlm@1: (.getCanonicalPath *export-base*) rlm@1: (.getCanonicalPath *site*)))) rlm@1: rlm@1: (defn title [page] rlm@1: (str (.getText (first (tags-by-name (parse page) "title"))))) rlm@1: rlm@1: (defn link [#^File file] rlm@1: (.replace (.getCanonicalPath (target-file file)) rlm@1: (.getCanonicalPath *site*) rlm@1: ".")) rlm@1: rlm@1: (defn rsync-string [#^java.io.File file] rlm@1: (if (.isFile file) rlm@1: (.getPath file) rlm@1: (str (.getPath file) "/"))) rlm@1: rlm@1: (defn rsync-local [#^File src #^File dst] rlm@1: (let [parent (.getParentFile dst)] rlm@1: (if (not (.exists parent)) rlm@1: ;; rsync won't make parent directories rlm@1: (sh "mkdir" "-p" (.getCanonicalPath parent)))) rlm@1: (sw "rsync" "-avz" "--human-readable" (rsync-string src) (.getCanonicalPath dst))) rlm@1: rlm@1: (defn cp [#^File src #^File dst] rlm@1: (if (.isDirectory src) rlm@1: (FileUtils/copyDirectory src dst) rlm@1: (FileUtils/copyFile src dst))) rlm@1: rlm@1: (defn copy-site-files rlm@1: "copy all the files in *exports* and *other-files* to the site directory rlm@1: preserving the folder structure." rlm@1: [] rlm@1: (dorun rlm@1: (for [file (concat *other-files* *exports*)] rlm@1: (let [source file destination (target-file file)] rlm@1: (rsync-local source destination))))) rlm@1: rlm@1: (deftemplate fill-list *index* [posts] rlm@1: [:div#posts :ul.post_list :li] rlm@1: (clone-for [post posts] rlm@1: (content {:tag :a rlm@1: :attrs {:href (link post)} rlm@1: :content [(title post)]}))) rlm@1: rlm@1: (defn update-index rlm@1: "update the index.html post list and write it to the site directory" rlm@1: [] rlm@1: (println "Rebuilding index.html") rlm@1: (FileUtils/writeStringToFile rlm@1: (file-str "~/aurellem/site-output/index.html") rlm@1: ;; reverse the list to get reverse chronological order for the site. rlm@1: (apply str (fill-list *exports*)))) rlm@1: rlm@1: (defn publish-local [] rlm@1: (rlm.rlm-commands/re) rlm@1: (copy-site-files) rlm@1: (update-index)) rlm@1: rlm@1: (defn publish-web [] rlm@1: (publish-local) rlm@1: (rlm.push/push "-u" "-t" "slice")) rlm@1: rlm@1: #+end_src rlm@1: rlm@1: rlm@1: rlm@1: #+begin_src clojure :results silent :tangle publish.clj :noweb yes :exports none rlm@1: <> rlm@1: #+end_src rlm@1: