Skip to content

Commit

Permalink
Polish the documentation some more.
Browse files Browse the repository at this point in the history
  • Loading branch information
Andy Fingerhut committed Dec 4, 2011
1 parent 192bdaf commit 7d61785
Showing 1 changed file with 87 additions and 34 deletions.
121 changes: 87 additions & 34 deletions src/cd_client/core.clj
Expand Up @@ -35,9 +35,32 @@
(def ^:private ^:dynamic *cd-client-mode* (ref {:source :web})) (def ^:private ^:dynamic *cd-client-mode* (ref {:source :web}))




(defn set-local-mode! [fname] ;; Handle errors in attempting to open the file, or as returned from
;; Handle errors in attempting to open the file, or as returned from ;; read?
;; read?
(defn set-local-mode!
"Change the behavior of future calls to cdoc and other documentation
access functions and macros in cd-client.core. Instead of using the
Internet, their results will be obtained from a file. This can be
useful when you do not have Internet access. Even if you do have
access, you may be able to get results more quickly in local mode,
and you will not put load on the clojuredocs.org servers.
Use set-web-mode! if you wish to change the mode back to retrieving
results from the Internet, or show-mode to see which mode you are
in.
A snapshot file is available here:
http://github.com/jafingerhut/cd-client/TBD
Example:
user=> (set-local-mode! \"clojuredocs-snapshot-2011-12-03.txt\")
Read info on 3574 names from file: clojuredocs-snapshot-2011-12-03.txt
Snapshot time: Sat Dec 03 18:03:43 PST 2011
nil"
[fname]
(let [x (with-open [s (java.io.PushbackReader. (let [x (with-open [s (java.io.PushbackReader.
(java.io.InputStreamReader. (java.io.InputStreamReader.
(java.io.FileInputStream. (java.io.FileInputStream.
Expand All @@ -53,12 +76,24 @@
(println "Snapshot time:" snapshot-time))) (println "Snapshot time:" snapshot-time)))




(defn set-web-mode! [] (defn set-web-mode!
"Change the behavior of future calls to cdoc and other documentation
access functions and macros in cd-client.core. Their results will
be obtained by accessing clojuredocs.org over the Internet.
See also: set-local-mode! show-mode"
[]
(dosync (alter *cd-client-mode* (fn [cur-val] {:source :web}))) (dosync (alter *cd-client-mode* (fn [cur-val] {:source :web})))
(println "Now retrieving clojuredocs data from web site" *clojuredocs-root*)) (println "Now retrieving clojuredocs data from web site" *clojuredocs-root*))




(defn show-mode [] (defn show-mode
"Print the mode you are in, local or web, for accessing
documentation with cdoc and other macros and functions in
cd-client.core.
See also: set-local-mode! set-web-mode!"
[]
(let [mode @*cd-client-mode*] (let [mode @*cd-client-mode*]
(if (= :web (:source mode)) (if (= :web (:source mode))
(println "Web mode. Data is retrieved from" *clojuredocs-root*) (println "Web mode. Data is retrieved from" *clojuredocs-root*)
Expand Down Expand Up @@ -173,7 +208,7 @@




(defmacro examples (defmacro examples
"Return examples from clojuredocs for a given (unquoted) var, fn, macro, "Return examples from clojuredocs for a given unquoted var, fn, macro,
special form, or a namespace and name (as strings). Returns a map special form, or a namespace and name (as strings). Returns a map
with a structure defined by the clojuredocs.org API. with a structure defined by the clojuredocs.org API.
Expand All @@ -195,8 +230,8 @@
;; saying that cd-client.core/pr-examples-core is not public. ;; saying that cd-client.core/pr-examples-core is not public.


(defn pr-examples-core (defn pr-examples-core
"Given a namespace and name (as strings), pretty-print all the examples for it "Given a namespace and name (as strings), pretty-print all the
from clojuredocs." examples for it from clojuredocs."
[ns name & verbose] [ns name & verbose]
(let [res (examples-core ns name) (let [res (examples-core ns name)
n (count (:examples res))] n (count (:examples res))]
Expand All @@ -219,8 +254,8 @@




(defmacro pr-examples (defmacro pr-examples
"Given an (unquoted) var, fn, macro, special form, or a namespace "Given an unquoted var, fn, macro, special form, or a namespace and
and name (as strings), pretty-print all the examples for it from name (as strings), pretty-print all the examples for it from
clojuredocs. clojuredocs.
See cdoc documentation for examples of the kinds of arguments that See cdoc documentation for examples of the kinds of arguments that
Expand All @@ -234,13 +269,17 @@
;; TBD: Think about how to implement search when in local mode. ;; TBD: Think about how to implement search when in local mode.


(defn search (defn search
"Search for a method name within an (optional) namespace" "Search for a method name within an (optional) namespace.
Note: This currently always attempts to access clojuredocs, even if
you have used set-local-mode!"
([name] (get-simple (str *search-api* name))) ([name] (get-simple (str *search-api* name)))
([ns name] (get-simple (str *search-api* ns "/" name)))) ([ns name] (get-simple (str *search-api* ns "/" name))))




(defn comments-core (defn comments-core
"Return comments from clojuredocs for a given namespace and name (as strings)" "Return comments from clojuredocs for a given namespace and name (as
strings)"
[ns name] [ns name]
(let [mode @*cd-client-mode*] (let [mode @*cd-client-mode*]
(if (= :web (:source mode)) (if (= :web (:source mode))
Expand All @@ -250,7 +289,7 @@




(defmacro comments (defmacro comments
"Return comments from clojuredocs for a given (unquoted) var, fn, macro, "Return comments from clojuredocs for a given unquoted var, fn, macro,
special form, or namespace and name (as strings). Returns nil if special form, or namespace and name (as strings). Returns nil if
there are no comments, or a vector of maps with a structure defined there are no comments, or a vector of maps with a structure defined
by the clojuredocs.org API if there are comments. by the clojuredocs.org API if there are comments.
Expand Down Expand Up @@ -289,7 +328,7 @@




(defmacro pr-comments (defmacro pr-comments
"Given a (unquoted) var, fn, macro, special form, or a namespace and "Given an unquoted var, fn, macro, special form, or a namespace and
name (as strings), pretty-print all the comments for it from name (as strings), pretty-print all the comments for it from
clojuredocs. clojuredocs.
Expand All @@ -302,7 +341,8 @@




(defn see-also-core (defn see-also-core
"Return 'see also' info from clojuredocs for a given namespace and name (as strings)" "Return 'see also' info from clojuredocs for a given namespace and
name (as strings)"
[ns name] [ns name]
(let [mode @*cd-client-mode*] (let [mode @*cd-client-mode*]
(if (= :web (:source mode)) (if (= :web (:source mode))
Expand All @@ -312,7 +352,7 @@




(defmacro see-also (defmacro see-also
"Given a (unquoted) var, fn, macro, special form, or a namespace and "Given an unquoted var, fn, macro, special form, or a namespace and
name (as strings), show the 'see also' for it from clojuredocs. name (as strings), show the 'see also' for it from clojuredocs.
Returns nil if there are no 'see alsos', or a vector of maps with a Returns nil if there are no 'see alsos', or a vector of maps with a
structure defined by the clojuredocs.org API if there are comments. structure defined by the clojuredocs.org API if there are comments.
Expand Down Expand Up @@ -344,7 +384,7 @@




(defmacro pr-see-also (defmacro pr-see-also
"Given a (unquoted) var, fn, macro, special form, or a namespace and "Given an unquoted var, fn, macro, special form, or a namespace and
name (as strings), pretty-print the 'see also' for it from name (as strings), pretty-print the 'see also' for it from
clojuredocs. clojuredocs.
Expand All @@ -366,28 +406,34 @@




(defmacro cdoc (defmacro cdoc
"Given a (unquoted) var, fn, macro, special form, or a namespace and "Given an unquoted var, fn, macro, special form, or a namespace and
name (as strings), show the Clojure documentation, and any examples, name (as strings), show the Clojure documentation, and any examples,
see also pointers, and comments available on clojuredocs. see also pointers, and comments available on clojuredocs.
By default, cdoc and its related functions and macros in
cd-client.core use the Internet to access clojuredocs.org at the
time you invoke them. If you wish to work off line from a snapshot
file, use set-local-mode!
See also: set-local-mode! pr-examples browse-to
Examples: Examples:
(cdoc *) (cdoc *)
(cdoc catch) (cdoc catch)
(cdoc ->>) (cdoc ->>)
Just as for clojure.repl/doc, you may name a var, fn, or macro using Just as for clojure.repl/doc, you may name a var, fn, or macro using
the full namespace/name, or any shorter version that you can use the full namespace/name, or any shorter version accessible due to
given additions to the current namespace from prior calls to prior calls to require, use, refer, etc.
require, use, refer, etc.
Thus the two cdoc invocations below give the same result: Thus the two cdoc calls below give the same result:
(cdoc clojure.string/join) (cdoc clojure.string/join)
(require '[clojure.string :as str]) (require '[clojure.string :as str])
(cdoc str/join) (cdoc str/join)
And all of the below give the same result: And all of the cdoc calls below give the same result:
(cdoc \"clojure.java.io\" \"reader\") (cdoc \"clojure.java.io\" \"reader\")
(cdoc clojure.java.io/reader) (cdoc clojure.java.io/reader)
Expand All @@ -412,10 +458,14 @@




(defmacro browse-to (defmacro browse-to
"Given a (unquoted) var, fn, macro, or special form, or a namespace "Given an unquoted var, fn, macro, or special form, or a namespace
and name (as strings), open a browser to the clojuredocs page for and name (as strings), open a browser to the clojuredocs page for
it. it.
Note: This macro always attempts to access the Internet, even if you
have used set-local-mode! Clojuredocs web page contents are not
saved in a snapshot file at this time.
See cdoc documentation for examples of the kinds of arguments that See cdoc documentation for examples of the kinds of arguments that
can be given. This macro can be given the same arguments as cdoc." can be given. This macro can be given the same arguments as cdoc."
([name] ([name]
Expand All @@ -428,17 +478,20 @@
;; + examples, see also list, and comments ;; + examples, see also list, and comments
;; + DON'T get the Clojure documentation string. Assume we have that ;; + DON'T get the Clojure documentation string. Assume we have that
;; locally already. ;; locally already.
;;
;; Use search-str "let" to get a partial snapshot, with only those
;; names that contain "let". This currently returns 39 results, so it
;; is a nice smaller test case for development and debugging.
;;
;; Use search-str "" to get a full snapshot. As of Mar 3, 2011 that
;; is information on a little over 4000 names, requiring 3 API calls
;; per name. Best to ask permission before hitting the server with
;; this kind of use.


(defn make-snapshot [search-str fname & quiet] (defn make-snapshot
"Create a snapshot file that can be read in with set-local-mode!
Warning: Please consider using a snapshot file created by someone
else, since doing so using this function can take a long time (over
an hour), and if many people do so it could add significant load to
the clojuredocs server.
Examples:
(make-snapshot \"\" \"clojuredocs-snapshot-2011-12-03.txt\")
(make-snapshot \"let\" \"only-clojuredocs-symbols-containing-let.txt\")"
[search-str fname & quiet]
(let [verbose (not quiet) (let [verbose (not quiet)
all-names-urls (search search-str) all-names-urls (search search-str)
junk (when verbose junk (when verbose
Expand Down

0 comments on commit 7d61785

Please sign in to comment.