Skip to content

Instantly share code, notes, and snippets.

@remleduff
Created May 26, 2010 20:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save remleduff/415019 to your computer and use it in GitHub Desktop.
Save remleduff/415019 to your computer and use it in GitHub Desktop.
(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
{#"<.*?>" ""
#"&nbsp;" " "
#"&lt;" "<"
#"&gt;" ">"})
(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))))
@remleduff
Copy link
Author

(jdoc string-url "replaceAll")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment