Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Bring client inline with the new WSAPI changes.

Added ability to use just a var to search for examples, comments or see-also.

Added function for browsing to the clojuredocs url in OS default browser,
(browse-to ...)

Pretty-printing functions for examples and comments (thanks Zach).
  • Loading branch information...
commit 6691c563a48c040afff8154b9afb43a522fe7225 1 parent abacb50
@dakrone dakrone authored
Showing with 100 additions and 27 deletions.
  1. +25 −0 README.markdown
  2. +75 −27 src/cd_client/core.clj
View
25 README.markdown
@@ -6,14 +6,39 @@ A tiny client for the http://clojuredocs.org API
(use 'cd-client.core)
+Examples work with strings for the ns and name as well as just a
+method. Use 'pr-examples' to pretty-print a list of all examples
+for a method.
+
(examples "clojure.core" "map")
+ (examples map)
+ (pr-examples map) ; pretty-prints the examples
+
+Search for a method using just the name or a namespace and name.
+
(search "pmap")
(search "clojure.core" "map")
+
+Comments works just like examples do, with strings, a var and pretty-
+printing.
+
(comments "clojure.contrib.json" "read-json")
+ (comments read-json)
+ (pr-comments read-json) ; pretty-prints the comments
+
+See-also works with either strings for ns/name or a method var.
+
(see-also "clojure.test" "are")
+ (see-also are)
+
+Browse to the url for a method in your default browser:
+
+ (browse-to "clojure.core" "map")
+ (browse-to map)
## Installation
+ lein deps
lein jar
## License
View
102 src/cd_client/core.clj
@@ -1,7 +1,8 @@
(ns cd-client.core
- (:require [org.danlarkin.json :as json])
- (:require [clj-http.client :as http]
- [clojure.string :as string]))
+ (:require [org.danlarkin.json :as json]
+ [clj-http.client :as http]
+ [clojure.string :as string])
+ (:use [clojure.java.browse :only [browse-url]]))
; For testing purposes use localhost:8080
@@ -14,21 +15,19 @@
(def *seealso-api* (str *clojuredocs-root* "/see-also/"))
-(defn format-examples
- [examples]
- (map (fn
- [ex]
- (let [body (:body ex)]
- (assoc ex :body (-> body
- (.replaceAll "<pre>" "")
- (.replaceAll "</pre>" "")
- (.replaceAll "<p>" "")
- (.replaceAll "</p>" "")
- (.replaceAll "&gt;" ">")
- (.replaceAll "<br>" "")
- (.replaceAll "<br/>" "")
- (.replaceAll "<br />" "")
- (.replaceAll "\\\\r\\\\n" "\\\\n"))))) examples))
+(defn remove-markdown
+ "Remove basic markdown syntax from a string."
+ [text]
+ (-> text
+ (.replaceAll "<pre>" "")
+ (.replaceAll "</pre>" "")
+ (.replaceAll "<p>" "")
+ (.replaceAll "</p>" "")
+ (.replaceAll "&gt;" ">")
+ (.replaceAll "<br>" "")
+ (.replaceAll "<br/>" "")
+ (.replaceAll "<br />" "")
+ (.replaceAll "\\\\r\\\\n" "\\\\n")))
(defn examples
@@ -39,16 +38,19 @@
name (str (:name m))]
(examples ns name)))
([ns name]
- (format-examples (json/decode-from-str (:body (http/get (str *examples-api* ns "/" name)))))))
+ (json/decode-from-str (:body (http/get (str *examples-api* ns "/" name))))))
-(defn pr-examples [v]
+
+(defn pr-examples
+ "Given a var, pretty-print all the examples for it from clojuredocs"
+ [v]
(let [res (examples v)
m (meta v)
ns (str (.name (:ns m)))
name (str (:name m))]
(println)
(println "======================================== vvv")
- (doseq [ex res]
+ (doseq [ex (:examples res)]
(println "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
(println)
(println " " (string/replace (:body ex) #"\n" "\n "))
@@ -56,7 +58,9 @@
(println " *** Last Updated:" (:updated_at ex))
(println))
(println "======================================== ^^^")
- (println (count res) "example(s) found for" (str ns "/" name))))
+ (println (count res) "example(s) found for" (str ns "/" name))
+ (println "Taken from" (:url res))))
+
(defn search
"Search for a method name within an (optional) namespace"
@@ -68,13 +72,57 @@
(defn comments
"Return comments for a given namespace and method name."
- [ns name]
- (format-examples
- (json/decode-from-str (:body (http/get (str *comments-api* ns "/" name))))))
+ ([v]
+ (let [m (meta v)
+ ns (str (.name (:ns m)))
+ name (str (:name m))]
+ (comments ns name)))
+ ([ns name]
+ (json/decode-from-str (:body (http/get (str *comments-api* ns "/" name))))))
+
+
+(defn pr-comments
+ "Given a var, pretty-print all the comments for it from clojuredocs"
+ [v]
+ (let [res (comments v)
+ m (meta v)
+ ns (str (.name (:ns m)))
+ name (str (:name m))]
+ (println)
+ (println "======================================== vvv")
+ (doseq [ex res]
+ (println "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
+ (println)
+ (println " " (-> (remove-markdown (:body ex))
+ (string/replace #"^M" "")
+ (string/replace #"\n" "\n ")))
+ (println)
+ (println " *** Last Updated:" (:updated_at ex))
+ (println))
+ (println "======================================== ^^^")
+ (println (count res) "comment(s) found for" (str ns "/" name))
+ ; no URL in comments yet
+ #_(println "Taken from" (:url res))))
(defn see-also
"Return methods to 'see also' for a given namespace and method name."
- [ns name]
- (json/decode-from-str (:body (http/get (str *seealso-api* ns "/" name)))))
+ ([v]
+ (let [m (meta v)
+ ns (str (.name (:ns m)))
+ name (str (:name m))]
+ (see-also ns name)))
+ ([ns name]
+ (json/decode-from-str (:body (http/get (str *seealso-api* ns "/" name))))))
+
+(defn browse-to
+ "Open a browser to the clojuredocs page for a given namespace and method name."
+ ([v]
+ (let [m (meta v)
+ ns (str (.name (:ns m)))
+ name (str (:name m))]
+ (browse-to ns name)))
+ ([ns name]
+ (when-let [url (:url (examples ns name))]
+ (browse-url url))))
Please sign in to comment.
Something went wrong with that request. Please try again.