diff org/publish.org @ 1:12573db75437

added cute error page by dylan. Going to fix index page
author Robert McIntyre <rlm@mit.edu>
date Mon, 17 Oct 2011 22:21:38 -0700
parents
children 3186b6b76cde
line wrap: on
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/org/publish.org	Mon Oct 17 22:21:38 2011 -0700
     1.3 @@ -0,0 +1,140 @@
     1.4 +#+title: Serving a blog using Clojure
     1.5 +#+author: Robert McIntyre
     1.6 +#+MATHJAX: align:"left" mathml:t path:"../MathJax/MathJax.js"
     1.7 +#+STYLE: <link rel="stylesheet" type="text/css" href="../css/aurellem.css" />
     1.8 +#+BEGIN_HTML
     1.9 +<h1>{{{title}}}</h1>
    1.10 +#+END_HTML
    1.11 +
    1.12 +
    1.13 +* Aurellem Export Program
    1.14 +#+srcname: publish
    1.15 +#+begin_src clojure :results silent
    1.16 +(ns aurellem.publish
    1.17 +  (:use rlm.ns-rlm))
    1.18 +(rlm.ns-rlm/ns-clone rlm.light-base)
    1.19 +(import org.htmlcleaner.HtmlCleaner)
    1.20 +(import org.htmlcleaner.TagNode)
    1.21 +(import java.io.File)
    1.22 +(import java.net.URL)
    1.23 +(import org.apache.commons.io.FileUtils)
    1.24 +(use 'clojure.java.io)
    1.25 +(use 'rlm.sanitize-file)
    1.26 +(use 'net.cgrand.enlive-html)
    1.27 +(use 'rlm.pikasemechu)
    1.28 +(require 'rlm.push)
    1.29 +(use 'clojure.contrib.shell-out)
    1.30 +
    1.31 +(declare publish)
    1.32 +
    1.33 +
    1.34 +(defvar *exports*
    1.35 +  [(file-str "~/aurellem/src/pokemon/types.html")
    1.36 +   (file-str "~/aurellem/src/pokemon/lpsolve.html")
    1.37 +   (file-str "~/aurellem/src/abomination/no_parens.html")
    1.38 +   (file-str "/home/r/aurellem/src/qm/quandary.html")
    1.39 +   (file-str "/home/r/cortex/cortex.html")
    1.40 +   (file-str "/home/r/cortex/capture-video.html")]
    1.41 +  "The html files which will be exported to the auerllem
    1.42 +  website. Listed in the order they will appear on the site
    1.43 +  to add more entries to the site, add them here.")
    1.44 +
    1.45 +(defvar *other-files*
    1.46 +  [(file-str "/home/r/aurellem/src/MathJax/")
    1.47 +   (file-str "/home/r/aurellem/src/css/aurellem.css")
    1.48 +   (file-str "/home/r/aurellem/src/js/jquery.min.js")
    1.49 +   (file-str "/home/r/aurellem/src/aurellem/err.html")
    1.50 +   (file-str "/home/r/cortex/sources/turing.pdf")
    1.51 +   (file-str "/home/r/cortex/images/brick-wall-standing.jpg")
    1.52 +   (file-str "/home/r/cortex/images/brick-wall-knocked-down.jpg")
    1.53 +   (file-str "/home/r/cortex/images/dominos.jpg")
    1.54 +   (file-str "/home/r/cortex/images/simple-app.jpg")]
    1.55 +   
    1.56 +  "other files needed by the website, but which are not posts.")
    1.57 +
    1.58 +(defvar *index*
    1.59 +  (file-str "~/aurellem/src/aurellem/index.html")
    1.60 +  "this is the main index.html file for the site. It will be updated
    1.61 +  with new posts")
    1.62 +
    1.63 +(defvar *site*
    1.64 +  (file-str "~/aurellem/site-output")
    1.65 +  "the target output directoty for the site's content")
    1.66 +
    1.67 +(defvar *export-base* (file-str "~/"))
    1.68 +
    1.69 +(defn target-file [#^File file]
    1.70 +  (file-str
    1.71 +   (.replace (.getCanonicalPath file)
    1.72 +	     (.getCanonicalPath *export-base*)
    1.73 +	     (.getCanonicalPath *site*))))
    1.74 +
    1.75 +(defn title [page]
    1.76 +  (str (.getText (first (tags-by-name (parse page) "title")))))
    1.77 +
    1.78 +(defn link [#^File file]
    1.79 +  (.replace (.getCanonicalPath (target-file file))
    1.80 +	    (.getCanonicalPath *site*)
    1.81 +	    "."))
    1.82 +
    1.83 +(defn rsync-string [#^java.io.File file]
    1.84 +  (if (.isFile file) 
    1.85 +    (.getPath file)
    1.86 +    (str (.getPath file) "/")))
    1.87 +
    1.88 +(defn rsync-local [#^File src #^File dst]
    1.89 +  (let [parent (.getParentFile dst)]
    1.90 +    (if (not (.exists parent))
    1.91 +      ;; rsync won't make parent directories
    1.92 +      (sh "mkdir" "-p" (.getCanonicalPath parent))))
    1.93 +  (sw "rsync" "-avz" "--human-readable" (rsync-string src) (.getCanonicalPath dst)))
    1.94 +
    1.95 +(defn cp [#^File src #^File dst]
    1.96 +  (if (.isDirectory src)
    1.97 +    (FileUtils/copyDirectory src dst)
    1.98 +    (FileUtils/copyFile src dst)))
    1.99 +
   1.100 +(defn copy-site-files 
   1.101 +  "copy all the files in *exports* and *other-files* to the site directory
   1.102 + preserving the folder structure."
   1.103 +  []
   1.104 +  (dorun
   1.105 +   (for [file (concat *other-files* *exports*)]
   1.106 +     (let [source file destination (target-file file)]
   1.107 +	   (rsync-local source destination)))))
   1.108 +
   1.109 +(deftemplate fill-list *index* [posts]
   1.110 +  [:div#posts :ul.post_list :li]
   1.111 +  (clone-for [post posts]
   1.112 +	     (content {:tag :a 
   1.113 +		       :attrs {:href (link post)}
   1.114 +		       :content [(title post)]})))
   1.115 +  
   1.116 +(defn update-index
   1.117 +  "update the index.html post list and write it to the site directory"
   1.118 +  []
   1.119 +  (println "Rebuilding index.html")
   1.120 +  (FileUtils/writeStringToFile 
   1.121 +   (file-str "~/aurellem/site-output/index.html")
   1.122 +   ;; reverse the list to get reverse chronological order for the site.
   1.123 +   (apply str (fill-list *exports*))))
   1.124 +  
   1.125 +(defn publish-local []
   1.126 +  (rlm.rlm-commands/re)
   1.127 +  (copy-site-files)
   1.128 +  (update-index))
   1.129 +
   1.130 +(defn publish-web []
   1.131 +  (publish-local)
   1.132 +  (rlm.push/push "-u" "-t" "slice"))
   1.133 +
   1.134 +
   1.135 +
   1.136 +#+end_src
   1.137 +
   1.138 +
   1.139 +
   1.140 +#+begin_src clojure :results silent :tangle publish.clj :noweb yes :exports none
   1.141 +<<publish>>
   1.142 +#+end_src
   1.143 +