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