annotate org/publish.org @ 78:06c628fe1555

s.
author Robert McIntyre <rlm@mit.edu>
date Sun, 03 May 2015 14:17:24 -0700
parents 3186b6b76cde
children
rev   line source
rlm@1 1 #+title: Serving a blog using Clojure
rlm@1 2 #+author: Robert McIntyre
rlm@1 3 #+MATHJAX: align:"left" mathml:t path:"../MathJax/MathJax.js"
rlm@1 4 #+STYLE: <link rel="stylesheet" type="text/css" href="../css/aurellem.css" />
rlm@1 5 #+BEGIN_HTML
rlm@1 6 <h1>{{{title}}}</h1>
rlm@1 7 #+END_HTML
rlm@1 8
rlm@1 9 * Aurellem Export Program
rlm@1 10 #+srcname: publish
rlm@1 11 #+begin_src clojure :results silent
rlm@1 12 (ns aurellem.publish
rlm@1 13 (:use rlm.ns-rlm))
rlm@1 14 (rlm.ns-rlm/ns-clone rlm.light-base)
rlm@1 15 (import org.htmlcleaner.HtmlCleaner)
rlm@1 16 (import org.htmlcleaner.TagNode)
rlm@1 17 (import java.io.File)
rlm@1 18 (import java.net.URL)
rlm@1 19 (import org.apache.commons.io.FileUtils)
rlm@1 20 (use 'clojure.java.io)
rlm@1 21 (use 'rlm.sanitize-file)
rlm@1 22 (use 'net.cgrand.enlive-html)
rlm@1 23 (use 'rlm.pikasemechu)
rlm@1 24 (require 'rlm.push)
rlm@1 25 (use 'clojure.contrib.shell-out)
rlm@1 26
rlm@1 27 (declare publish)
rlm@1 28
rlm@1 29
rlm@1 30 (defvar *exports*
rlm@1 31 [(file-str "~/aurellem/src/pokemon/types.html")
rlm@1 32 (file-str "~/aurellem/src/pokemon/lpsolve.html")
rlm@1 33 (file-str "~/aurellem/src/abomination/no_parens.html")
rlm@1 34 (file-str "/home/r/aurellem/src/qm/quandary.html")
rlm@1 35 (file-str "/home/r/cortex/cortex.html")
rlm@1 36 (file-str "/home/r/cortex/capture-video.html")]
rlm@1 37 "The html files which will be exported to the auerllem
rlm@1 38 website. Listed in the order they will appear on the site
rlm@1 39 to add more entries to the site, add them here.")
rlm@1 40
rlm@1 41 (defvar *other-files*
rlm@1 42 [(file-str "/home/r/aurellem/src/MathJax/")
rlm@1 43 (file-str "/home/r/aurellem/src/css/aurellem.css")
rlm@1 44 (file-str "/home/r/aurellem/src/js/jquery.min.js")
rlm@1 45 (file-str "/home/r/aurellem/src/aurellem/err.html")
rlm@1 46 (file-str "/home/r/cortex/sources/turing.pdf")
rlm@1 47 (file-str "/home/r/cortex/images/brick-wall-standing.jpg")
rlm@1 48 (file-str "/home/r/cortex/images/brick-wall-knocked-down.jpg")
rlm@1 49 (file-str "/home/r/cortex/images/dominos.jpg")
rlm@1 50 (file-str "/home/r/cortex/images/simple-app.jpg")]
rlm@1 51
rlm@1 52 "other files needed by the website, but which are not posts.")
rlm@1 53
rlm@1 54 (defvar *index*
rlm@1 55 (file-str "~/aurellem/src/aurellem/index.html")
rlm@1 56 "this is the main index.html file for the site. It will be updated
rlm@1 57 with new posts")
rlm@1 58
rlm@1 59 (defvar *site*
rlm@1 60 (file-str "~/aurellem/site-output")
rlm@1 61 "the target output directoty for the site's content")
rlm@1 62
rlm@1 63 (defvar *export-base* (file-str "~/"))
rlm@1 64
rlm@1 65 (defn target-file [#^File file]
rlm@1 66 (file-str
rlm@1 67 (.replace (.getCanonicalPath file)
rlm@1 68 (.getCanonicalPath *export-base*)
rlm@1 69 (.getCanonicalPath *site*))))
rlm@1 70
rlm@1 71 (defn title [page]
rlm@1 72 (str (.getText (first (tags-by-name (parse page) "title")))))
rlm@1 73
rlm@1 74 (defn link [#^File file]
rlm@1 75 (.replace (.getCanonicalPath (target-file file))
rlm@1 76 (.getCanonicalPath *site*)
rlm@1 77 "."))
rlm@1 78
rlm@1 79 (defn rsync-string [#^java.io.File file]
rlm@1 80 (if (.isFile file)
rlm@1 81 (.getPath file)
rlm@1 82 (str (.getPath file) "/")))
rlm@1 83
rlm@1 84 (defn rsync-local [#^File src #^File dst]
rlm@1 85 (let [parent (.getParentFile dst)]
rlm@1 86 (if (not (.exists parent))
rlm@1 87 ;; rsync won't make parent directories
rlm@1 88 (sh "mkdir" "-p" (.getCanonicalPath parent))))
rlm@1 89 (sw "rsync" "-avz" "--human-readable" (rsync-string src) (.getCanonicalPath dst)))
rlm@1 90
rlm@1 91 (defn cp [#^File src #^File dst]
rlm@1 92 (if (.isDirectory src)
rlm@1 93 (FileUtils/copyDirectory src dst)
rlm@1 94 (FileUtils/copyFile src dst)))
rlm@1 95
rlm@1 96 (defn copy-site-files
rlm@1 97 "copy all the files in *exports* and *other-files* to the site directory
rlm@1 98 preserving the folder structure."
rlm@1 99 []
rlm@1 100 (dorun
rlm@1 101 (for [file (concat *other-files* *exports*)]
rlm@1 102 (let [source file destination (target-file file)]
rlm@1 103 (rsync-local source destination)))))
rlm@1 104
rlm@1 105 (deftemplate fill-list *index* [posts]
rlm@1 106 [:div#posts :ul.post_list :li]
rlm@1 107 (clone-for [post posts]
rlm@1 108 (content {:tag :a
rlm@1 109 :attrs {:href (link post)}
rlm@1 110 :content [(title post)]})))
rlm@1 111
rlm@1 112 (defn update-index
rlm@1 113 "update the index.html post list and write it to the site directory"
rlm@1 114 []
rlm@1 115 (println "Rebuilding index.html")
rlm@1 116 (FileUtils/writeStringToFile
rlm@1 117 (file-str "~/aurellem/site-output/index.html")
rlm@1 118 ;; reverse the list to get reverse chronological order for the site.
rlm@1 119 (apply str (fill-list *exports*))))
rlm@1 120
rlm@1 121 (defn publish-local []
rlm@1 122 (rlm.rlm-commands/re)
rlm@1 123 (copy-site-files)
rlm@1 124 (update-index))
rlm@1 125
rlm@1 126 (defn publish-web []
rlm@1 127 (publish-local)
rlm@1 128 (rlm.push/push "-u" "-t" "slice"))
rlm@1 129
rlm@1 130 #+end_src
rlm@1 131
rlm@1 132
rlm@1 133
rlm@1 134 #+begin_src clojure :results silent :tangle publish.clj :noweb yes :exports none
rlm@1 135 <<publish>>
rlm@1 136 #+end_src
rlm@1 137