Mercurial > lasercutter
diff src/clojure/java/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 |
line wrap: on
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/clojure/java/javadoc.clj Sat Aug 21 06:25:44 2010 -0400 1.3 @@ -0,0 +1,82 @@ 1.4 +; Copyright (c) Rich Hickey. All rights reserved. 1.5 +; The use and distribution terms for this software are covered by the 1.6 +; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) 1.7 +; which can be found in the file epl-v10.html at the root of this distribution. 1.8 +; By using this software in any fashion, you are agreeing to be bound by 1.9 +; the terms of this license. 1.10 +; You must not remove this notice, or any other, from this software. 1.11 +(ns 1.12 + ^{:author "Christophe Grand, Stuart Sierra", 1.13 + :doc "A repl helper to quickly open javadocs."} 1.14 + clojure.java.javadoc 1.15 + (:use [clojure.java.browse :only (browse-url)] ) 1.16 + (:import 1.17 + (java.io File))) 1.18 + 1.19 +(def *feeling-lucky-url* "http://www.google.com/search?btnI=I%27m%20Feeling%20Lucky&q=allinurl:") 1.20 +(def *feeling-lucky* true) 1.21 + 1.22 +(def *local-javadocs* (ref (list))) 1.23 + 1.24 +(def *core-java-api* 1.25 + (if (= "1.5" (System/getProperty "java.specification.version")) 1.26 + "http://java.sun.com/j2se/1.5.0/docs/api/" 1.27 + "http://java.sun.com/javase/6/docs/api/")) 1.28 + 1.29 +(def *remote-javadocs* 1.30 + (ref (sorted-map 1.31 + "java." *core-java-api* 1.32 + "javax." *core-java-api* 1.33 + "org.ietf.jgss." *core-java-api* 1.34 + "org.omg." *core-java-api* 1.35 + "org.w3c.dom." *core-java-api* 1.36 + "org.xml.sax." *core-java-api* 1.37 + "org.apache.commons.codec." "http://commons.apache.org/codec/api-release/" 1.38 + "org.apache.commons.io." "http://commons.apache.org/io/api-release/" 1.39 + "org.apache.commons.lang." "http://commons.apache.org/lang/api-release/"))) 1.40 + 1.41 +(defn add-local-javadoc 1.42 + "Adds to the list of local Javadoc paths." 1.43 + {:added "1.2"} 1.44 + [path] 1.45 + (dosync (commute *local-javadocs* conj path))) 1.46 + 1.47 +(defn add-remote-javadoc 1.48 + "Adds to the list of remote Javadoc URLs. package-prefix is the 1.49 + beginning of the package name that has docs at this URL." 1.50 + {:added "1.2"} 1.51 + [package-prefix url] 1.52 + (dosync (commute *remote-javadocs* assoc package-prefix url))) 1.53 + 1.54 +(defn- javadoc-url 1.55 + "Searches for a URL for the given class name. Tries 1.56 + *local-javadocs* first, then *remote-javadocs*. Returns a string." 1.57 + {:tag String, 1.58 + :added "1.2"} 1.59 + [^String classname] 1.60 + (let [file-path (.replace classname \. File/separatorChar) 1.61 + url-path (.replace classname \. \/)] 1.62 + (if-let [file ^File (first 1.63 + (filter #(.exists ^File %) 1.64 + (map #(File. (str %) (str file-path ".html")) 1.65 + @*local-javadocs*)))] 1.66 + (-> file .toURI str) 1.67 + ;; If no local file, try remote URLs: 1.68 + (or (some (fn [[prefix url]] 1.69 + (when (.startsWith classname prefix) 1.70 + (str url url-path ".html"))) 1.71 + @*remote-javadocs*) 1.72 + ;; if *feeling-lucky* try a web search 1.73 + (when *feeling-lucky* (str *feeling-lucky-url* url-path ".html")))))) 1.74 + 1.75 +(defn javadoc 1.76 + "Opens a browser window displaying the javadoc for the argument. 1.77 + Tries *local-javadocs* first, then *remote-javadocs*." 1.78 + {:added "1.2"} 1.79 + [class-or-object] 1.80 + (let [^Class c (if (instance? Class class-or-object) 1.81 + class-or-object 1.82 + (class class-or-object))] 1.83 + (if-let [url (javadoc-url (.getName c))] 1.84 + (browse-url url) 1.85 + (println "Could not find Javadoc for" c))))