This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;;; 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)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; 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))))])) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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 %))))))) | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; 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)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; 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] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; 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)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; 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 |