view org/publish.org @ 86:311e0be4f247

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