annotate src/clojure/contrib/repl_utils/javadoc.clj @ 10:ef7dbbd6452c

added clojure source goodness
author Robert McIntyre <rlm@mit.edu>
date Sat, 21 Aug 2010 06:25:44 -0400
parents
children
rev   line source
rlm@10 1 ; Copyright (c) Christophe Grand, November 2008. All rights reserved.
rlm@10 2
rlm@10 3 ; The use and distribution terms for this software are covered by the
rlm@10 4 ; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
rlm@10 5 ; which can be found in the file epl-v10.html at the root of this
rlm@10 6 ; distribution.
rlm@10 7 ; By using this software in any fashion, you are agreeing to be bound by
rlm@10 8 ; the terms of this license.
rlm@10 9 ; You must not remove this notice, or any other, from this software.
rlm@10 10
rlm@10 11 ; thanks to Stuart Sierra
rlm@10 12
rlm@10 13 ; a repl helper to quickly open javadocs.
rlm@10 14
rlm@10 15 (def *feeling-lucky-url* "http://www.google.com/search?btnI=I%27m%20Feeling%20Lucky&q=allinurl:")
rlm@10 16 (def *feeling-lucky* true)
rlm@10 17
rlm@10 18 (def
rlm@10 19 ^{:doc "Ref to a list of local paths for Javadoc-generated HTML
rlm@10 20 files."}
rlm@10 21 *local-javadocs* (ref (list)))
rlm@10 22
rlm@10 23 (def *core-java-api*
rlm@10 24 (if (= "1.5" (System/getProperty "java.specification.version"))
rlm@10 25 "http://java.sun.com/j2se/1.5.0/docs/api/"
rlm@10 26 "http://java.sun.com/javase/6/docs/api/"))
rlm@10 27
rlm@10 28 (def
rlm@10 29 ^{:doc "Ref to a map from package name prefixes to URLs for remote
rlm@10 30 Javadocs."}
rlm@10 31 *remote-javadocs*
rlm@10 32 (ref (sorted-map
rlm@10 33 "java." *core-java-api*
rlm@10 34 "javax." *core-java-api*
rlm@10 35 "org.ietf.jgss." *core-java-api*
rlm@10 36 "org.omg." *core-java-api*
rlm@10 37 "org.w3c.dom." *core-java-api*
rlm@10 38 "org.xml.sax." *core-java-api*
rlm@10 39 "org.apache.commons.codec." "http://commons.apache.org/codec/api-release/"
rlm@10 40 "org.apache.commons.io." "http://commons.apache.org/io/api-release/"
rlm@10 41 "org.apache.commons.lang." "http://commons.apache.org/lang/api-release/")))
rlm@10 42
rlm@10 43 (defn add-local-javadoc
rlm@10 44 "Adds to the list of local Javadoc paths."
rlm@10 45 [path]
rlm@10 46 (dosync (commute *local-javadocs* conj path)))
rlm@10 47
rlm@10 48 (defn add-remote-javadoc
rlm@10 49 "Adds to the list of remote Javadoc URLs. package-prefix is the
rlm@10 50 beginning of the package name that has docs at this URL."
rlm@10 51 [package-prefix url]
rlm@10 52 (dosync (commute *remote-javadocs* assoc package-prefix url)))
rlm@10 53
rlm@10 54 (defn find-javadoc-url
rlm@10 55 "Searches for a URL for the given class name. Tries
rlm@10 56 *local-javadocs* first, then *remote-javadocs*. Returns a string."
rlm@10 57 {:tag String}
rlm@10 58 [^String classname]
rlm@10 59 (let [file-path (.replace classname \. File/separatorChar)
rlm@10 60 url-path (.replace classname \. \/)]
rlm@10 61 (if-let [file ^File (first
rlm@10 62 (filter #(.exists ^File %)
rlm@10 63 (map #(File. (str %) (str file-path ".html"))
rlm@10 64 @*local-javadocs*)))]
rlm@10 65 (-> file .toURI str)
rlm@10 66 ;; If no local file, try remote URLs:
rlm@10 67 (or (some (fn [[prefix url]]
rlm@10 68 (when (.startsWith classname prefix)
rlm@10 69 (str url url-path ".html")))
rlm@10 70 @*remote-javadocs*)
rlm@10 71 ;; if *feeling-lucky* try a web search
rlm@10 72 (when *feeling-lucky* (str *feeling-lucky-url* url-path ".html"))))))
rlm@10 73
rlm@10 74 (defn javadoc
rlm@10 75 "Opens a browser window displaying the javadoc for the argument.
rlm@10 76 Tries *local-javadocs* first, then *remote-javadocs*."
rlm@10 77 [class-or-object]
rlm@10 78 (let [^Class c (if (instance? Class class-or-object)
rlm@10 79 class-or-object
rlm@10 80 (class class-or-object))]
rlm@10 81 (if-let [url (find-javadoc-url (.getName c))]
rlm@10 82 (browse-url url)
rlm@10 83 (println "Could not find Javadoc for" c))))