Skip to content

Instantly share code, notes, and snippets.

View cgrand's full-sized avatar

Christophe Grand cgrand

View GitHub Profile
;;; CREATE HTML PAGE
(deftemplate tweet-template "html/template.html" [tweets date] :lockstep
{[:title] (content date)
[:div.tweet-div]
(clone-for [tweet tweets] :lockstep
{[:div] (set-attr :id (str "div-" (:id tweet)))
[:td.tweet-text]
(transformation :lockstep
{[:span.tweet-from] (content (:from-user tweet))
(defmacro ensure-node [nodes parent-selector tag transform-fn]
`(ensure-node* nodes (selector ~parent-selector) (selector [:> ~tag]) tag transform-fn))
(defn ensure-node* [nodes parent-selector child-selector tag transform-fn]
(transform nodes parent-selector
(fn [elt] (let [content (:content elt)
content* (transform content child-selector transform-fn)]
(if (= content content*) ;; hmm no it would be better to use zip-select
;; similar to http://clj-me.cgrand.net/2009/11/18/are-pipe-dreams-made-of-promises/ but without the spinning atom
(defn pipe []
(let [q (java.util.concurrent.LinkedBlockingQueue.)
EOQ (Object.)
NIL (Object.)
s (fn s [] (lazy-seq (let [x (.take q)] (when-not (= EOQ x) (cons (when-not (= NIL x) x) (s))))))]
[(s) (fn ([] (.put q EOQ)) ([x] (.put q (or x NIL))))]))
(ns net.cgrand.repl-ay)
(defn- prompt? [x] (and (symbol? x) (.endsWith (name x) "=>")))
(def *last-ns-name*)
(defn replay* [forms]
(loop [forms forms ])
(when-let [[form & forms] (seq forms)]
(if (prompt? form)
(defn letters [charset-name]
(let [ce (-> charset-name java.nio.charset.Charset/forName .newEncoder)]
(apply str (->> (range 0 (int Character/MAX_VALUE)) (map char)
(filter #(and (.canEncode ce %) (Character/isLetter %)))))))
;; answer to what I understood of http://d.hatena.ne.jp/fatrow/20100407/1270662784
;; your original code
(en/defsnippet lib-model "src/html/a.html" [:.library]
[{:keys [name description url category]}]
[:.name] (en/content name)
[:.descriptionj] (en/content description)
[:.site :a] (en/do-> (en/set-attr :href url)
(en/content url))
;; Growing a little DSL for regexes
;; prompted by http://stackoverflow.com/questions/2553668/how-to-remove-list-of-words-from-strings
(defmulti pattern type)
(defn regex [spec]
(-> spec pattern java.util.regex.Pattern/compile))
(defmethod pattern String [s]
(java.util.regex.Pattern/quote s))
(defmethod pattern clojure.lang.IPersistentSet [alts]
;; something like that should be closer to what you want
(defmacro defplugin [& body]
(let [cmd-list (into {} (for [[docs words cmdkey] body word words] [word {:cmd cmdkey :doc docs}]))]
`(do
~@(for [[docs words cmdkey & method-stuff] body]
`(defmethod respond ~cmdkey ~@method-stuff))
(dosync
(let [m-name# (keyword (last (.split (str *ns*) "\\.")))]
(alter modules assoc m-name#
{:load #(dosync (alter commands assoc m-name# ~cmd-list))
user=> (defn audit-ns [ns]
(let [publics (ns-publics ns)]
(map key (remove #(let [m (-> % val meta)] (or (:doc m) (:added m))) publics))))
#'user/audit-ns
user=> (audit-ns (find-ns 'clojure.core))
(chunked-seq? find-protocol-impl chunk-buffer find-protocol-method EMPTY-NODE await1 -reset-methods *allow-unresolved-vars* proxy-call-with-super
munge print-doc *math-context* with-loading-context unquote-splicing chunk-cons chunk-append destructure -cache-protocol-fn print-dup
*use-context-classloader* proxy-name print-ctor chunk-rest method-sig print-method hash-combine chunk definterface unquote primitives-classnames
rational? chunk-first *source-path* *assert* print-special-doc chunk-next print-simple)
;; in reply to http://www.sids.in/blog/2010/05/06/html-parsing-in-clojure-using-htmlcleaner/
(ns html-parser
(:require [net.cgrand.enlive-html :as e]))
(defn parse-page
"Given the HTML source of a web page, parses it and returns the :title
and the tag-stripped :content of the page. Does not do any encoding
detection, it is expected that this has already been done."
[page-src]
(-> page-src java.io.StringReader. e/html-resource