Created
May 26, 2010 20:41
-
-
Save remleduff/415019 to your computer and use it in GitHub Desktop.
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
(use 'clojure.java.io) | |
(def string-url "http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html") | |
(defonce text (let [writer (java.io.StringWriter.)] | |
(copy (-> (java.net.URL. url) .openStream) writer) writer)) | |
(defonce jdoc-pattern #"(?s)<A NAME=\"(\w*)\((.*?)\).*?</A>.*?<PRE>(.*?)</PRE>(.*?)<HR>") | |
(def html-entities | |
{#"<.*?>" "" | |
#" " " " | |
#"<" "<" | |
#">" ">"}) | |
(def doc-matches | |
{#"(?s)<TR.*?<CODE>.*?#(.*?)\".*?</CODE>.*?<CODE>(.*?)</CODE>(.*?)</TR>" [:ignored1 :returnType :name :summary] | |
#"(?s)<A NAME=\"(\w*)\((.*?)\).*?</A>.*?<PRE>(.*?)</PRE>(.*?)<HR>" [:ignored2 :name :args :declaration :docs]}) | |
(defn- plain-text [html] | |
(reduce | |
(fn [str entry] | |
(let [matcher (re-matcher (key entry) str)] | |
(if matcher (.replaceAll matcher (val entry)) str))) html html-entities)) | |
(defn re-zipmap[text re bindings] | |
(let [matches (re-seq re text)] | |
(map #(zipmap bindings (map plain-text %)) matches))) | |
(defn scrape [html] | |
(apply join (map #(re-zipmap html (key %) (val %)) doc-matches))) | |
(defn jdoc [url func] | |
"Takes the URL to a javadoc and a function name and returns plaintext-formatted documentation meant to be used by the doc macro" | |
(let [docs (re-seq jdoc-pattern (str text)) | |
matches (filter #(= func (nth % 1)) docs)] | |
(for [item matches, index [3 4], :let [html (item index)]] (plain-text html)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
(jdoc string-url "replaceAll")