Skip to content

Commit

Permalink
Add Google search, fix up for latest Clojure, contrib, and visual style.
Browse files Browse the repository at this point in the history
  • Loading branch information
Chouser committed Oct 17, 2008
1 parent 956ffd3 commit c2cfc49
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 71 deletions.
109 changes: 52 additions & 57 deletions irc-log-split.clj
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
;(set! *warn-on-reflection* true)

(clojure.lang.RT/loadResourceScript "lib.clj")
(lib/use duck-streams)
(ns irc-log
(:require (clojure.contrib [duck-streams :as ds]))
(:use (clojure.contrib [mmap :only (mmap)]
[str-utils :only (re-partition)]))
(:import (java.util Date)
(java.text SimpleDateFormat)
(java.nio ByteBuffer)
(java.io File)))

(import '(java.util Date)
'(java.text SimpleDateFormat)
'(java.nio ByteBuffer)
'(java.io File))
;(set! *warn-on-reflection* true)

(def #^SimpleDateFormat date-in-fmt (SimpleDateFormat. "MMM dd yyyy"))
(def #^SimpleDateFormat date-file-fmt (SimpleDateFormat. "yyyy-MM-dd"))
Expand All @@ -28,22 +29,6 @@
(apply str (map xhtml v))
:else v)))

(defn take-ns [n xs]
(when (seq xs)
(lazy-cons (take n xs) (take-ns n (drop n xs)))))

(defn re-split
[#^java.util.regex.Pattern re #^CharSequence cs]
(let [m (re-matcher re cs)]
((fn step [prevend]
(if (.find m)
(lazy-cons (.subSequence cs prevend (.start m))
(lazy-cons (re-groups m)
(step (+ (.start m) (count (.group m))))))
(when (< prevend (.length cs))
(list (.subSequence cs prevend (.length cs))))))
0)))

(defn charseq
([#^ByteBuffer buf] (charseq buf 0 (.limit buf)))
([#^ByteBuffer buf start end]
Expand All @@ -57,29 +42,27 @@
(.get buf ary)
(String. ary "ISO-8859-1"))))))

(defn mmap [f]
(let [READ_ONLY (java.nio.channels.FileChannel$MapMode/READ_ONLY)
channel (.getChannel (java.io.FileInputStream. f))]
(.map channel READ_ONLY 0 (.size channel))))

(defmacro hash-syms [& syms]
(cons 'hash-map (mapcat #(list (keyword (name %)) %) syms)))

(def escape-map {\& "&amp;", \< "&lt;", \> "&gt;",
\" "&quot;", \newline "<br />"})
(def link-re #"(?:https?://|www\\.)(?:<[^>]*>|[^<>\\s])*(?=(?:&gt;|&lt;|[.\\(\\)\\[\\]])*(?:\\s|$))")
(def wrap-re #"(?:<[^>]*>|&[^;]*;|[^/&?]){1,50}[/&?]?")
(def link-re #"(?:https?://|www\.)(?:<[^>]*>|[^<>\s])*(?=(?:&gt;|&lt;|[.()\[\]])*(?:\s|$))")
(def wrap-re #"(?:<[^>]*>|&[^;]*;|[^/&?]){1,50}[/&?]*")

(defn text-to-html [text]
(let [escaped (apply str (map #(or (escape-map %) %) text))
linked (apply str
(for [[text url] (take-ns 2 (re-split link-re escaped))]
(str text
(when url
(let [urltext (reduce #(str %1 "<wbr />" %2)
(re-seq wrap-re url))]
(xhtml [:a {:href url :class "nm"}
urltext]))))))]
(for [[text url]
(partition 2 (lazy-cat
(re-partition link-re escaped)
[nil]))]
(str text
(when url
(let [urltext (reduce #(str %1 "<wbr />" %2)
(re-seq wrap-re url))]
(xhtml [:a {:href url :class "nm"}
urltext]))))))]
(str linked "\n")))

(defn html-header [date]
Expand All @@ -99,14 +82,23 @@
"<div id=\"narrow\">"
(xhtml [
[:dl
[:dt "Related links"]
[:dd [:a {:href "http://clojure.org/"} "Main Clojure site"]]
[:dd [:a {:href "http://groups.google.com/group/clojure"}
"Google Group"]]
[:dd [:a {:href "irc://irc.freenode.net/clojure"} "IRC"]]
[:dd [:a {:href "http://en.wikibooks.org/wiki/Clojure_Programming"}
"Wiki"]]
[:dd [:a {:href "/date/"} "List of all logged dates"]]]
[:dt
[:form {:action "http://www.google.com/cse" :id "cse-search-box"}
[:div
[:input {:type "hidden" :name "cx"
:value "partner-pub-1237864095616304:e7qm3gycp2b"}]
[:input {:type "hidden" :name "ie" :value "UTF-8"}]
[:input {:type "text" :id "q" :name "q" :size "10"}]
[:input {:type "submit" :id "sa" :name "sa" :value "Go"}]]]
[:script {:type "text/javascript"
:src "http://www.google.com/coop/cse/brand?form=cse-search-box&amp;lang=en"}]]
[:dd [:a {:href "http://clojure.org/"} "Main Clojure site"]]
[:dd [:a {:href "http://groups.google.com/group/clojure"}
"Google Group"]]
[:dd [:a {:href "irc://irc.freenode.net/clojure"} "IRC"]]
[:dd [:a {:href "http://en.wikibooks.org/wiki/Clojure_Programming"}
"Wiki"]]
[:dd [:a {:href "/date/"} "List of all logged dates"]]]
[:div {:id "nav-head" :class "nav"}
[:noscript "Turn on JavaScript for date navigation."]
"&nbsp;"]])
Expand All @@ -121,7 +113,7 @@
"</div></body></html>\n"))

(defn minutes [timestr]
(Integer/parseInt (second (re-seq #"\\d+" timestr))))
(Integer/parseInt (second (re-seq #"\d+" timestr))))

(defn html-post [prevpost {:keys [timestr speak emote text imc]}]
(let [htmltext (text-to-html text)
Expand All @@ -137,21 +129,23 @@
(if speak htmltext [[:em emote] " " htmltext])])))

(defn parse-post [prevs line]
(let [[_ timestr c body] (re-matches #"(..:..) (#\\S+): (.*)" line)]
(let [[_ timestr c body] (re-matches #"(..:..) (#\S+): (.*)" line)]
(if (= c channel)
(conj prevs
(let [[_ speak emote text]
(re-matches #"(?:< (\\S+)> | \\* (\\S+))(.*)" body)
(re-matches #"(?:< (\S+)> | \* (\S+))(.*)" body)
imc (let [p (peek prevs)]
(if (= timestr (:timestr p)) (+ 1 (:imc p)) 0))
offset (count prevs)]
;(println line)
;(prn (hash-syms timestr speak emote text offset imc))
(hash-syms timestr speak emote text offset imc)))
prevs)))

(defn split-days [cs]
(let [#^SimpleDateFormat date-in-fmt (SimpleDateFormat. "MMM dd yyyy")]
(for [[[_ datestr] body]
(take-ns 2 (rest (re-split #"--- Day changed ... (.*)" cs)))]
(partition 2 (rest (re-partition #"--- Day changed ... (.*)" cs)))]
[(.parse date-in-fmt datestr) (re-seq #".+" (str body))])))

(defn skip-until [#^Date lastdate days]
Expand All @@ -161,24 +155,25 @@

(defn write-day [date lines]
(let [datestr (.format date-file-fmt date)
filename (str "date/" datestr ".html")]
filename (str "date/" datestr ".html")
goodposts (reduce parse-post [] lines)]
(.mkdir (File. "date"))
(with-open #^java.io.PrintWriter out (duck-streams/writer filename)
(with-open #^java.io.PrintWriter out (ds/writer filename)
(println "Writing" filename)
(.write out #^String (html-header date))
(let [goodposts (reduce parse-post [] lines)]
(doseq string (map html-post (cons nil (seq goodposts)) goodposts)
(.write out #^String string)))
(doseq string (map html-post (cons nil goodposts) goodposts)
(.write out #^String string))
(.write out #^String (html-footer date)))
filename))


(let [lastdate
(let [[_ datestr] (some #(re-find #"(....-..-..)\\.html" %)
(let [[_ datestr] (some #(re-find #"(....-..-..)\.html" %)
(-> (map str (.listFiles (File. "date")))
sort reverse))]
(when datestr (.parse date-file-fmt datestr)))
days
(skip-until lastdate (split-days (charseq (mmap "irc-01.log"))))
lastfile
(reduce (fn [prev [date lines]] (write-day date lines)) nil days)]
(.. Runtime (getRuntime) (exec (str "ln -sf " lastfile " index.html"))))
(.. Runtime getRuntime (exec (str "ln -sf " lastfile " index.html"))))
28 changes: 14 additions & 14 deletions irc.css
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
body {
font-family: Georgia,Palatino,Times,"Times New Roman",serif;
font-size: 9pt;
background: #EFEFE7 repeat-x top
url(http://clojure.sourceforge.net/sandvox_EarthandSky/verlauf.png);
background: #E3E3E3;
text-align: center;
}

Expand All @@ -13,7 +12,8 @@ body {
}

#main {
background: #fff;
background: #FFFFFF repeat-y right
url(http://clojure.org/space/showimage/content-background.gif);
}

p {
Expand All @@ -37,19 +37,14 @@ b {
}

dl {
float: right;
background: #ddd;
margin: 0 1em;
margin-top: -1em;
padding: 0;
padding-bottom: 1em;
}

dt {
font-weight: bold;
background: #9a3;
margin-bottom: 0.5em;
padding: 0.2em 1em;
background: #E4EAF7 url(http://clojure.org/space/showimage/resources-background.gif) repeat-y scroll 0% 0%;
border-top: 1px solid #ABC4E2;
border-bottom: 1px solid #ABC4E2;
float: right;
padding: 5px 0pt;
width: 234px;
}

dd { margin: 0; }
Expand All @@ -73,3 +68,8 @@ a.nm:visited, dd a:visited, .nav a { color: purple; }

/* opera compatibility for <wbr> */
wbr:after { content: "\00200B" }

dt { margin: 0 0 1em 1em; }
#q { width: 19em; }
#sa { padding: 0; }
#q, #sa { font-size: 80%; }

0 comments on commit c2cfc49

Please sign in to comment.