Permalink
Browse files

Resource protocol for urlable things

  • Loading branch information...
stuarthalloway committed Sep 27, 2010
1 parent 63194bd commit 3b7ea01d10c9ed7b91b30b6660d5d77413a30191
Showing with 72 additions and 31 deletions.
  1. +1 −0 .gitignore
  2. +45 −11 src/mycroft/breadcrumb.clj
  3. +3 −5 src/mycroft/data.clj
  4. +11 −12 src/mycroft/namespace.clj
  5. +12 −3 src/mycroft/server.clj
View
@@ -1 +1,2 @@
lib/*
+pom.xml
View
@@ -1,5 +1,6 @@
(ns mycroft.breadcrumb
- (:use [hiccup.page-helpers :only (encode-params)]))
+ (:use [hiccup.page-helpers :only (encode-params)]
+ [hiccup.core :only (escape-html)]))
(defn options->query-string
"Generate query string for the options provided. Options include
@@ -10,18 +11,51 @@
[options]
(encode-params options))
-(defn namespace-link
- [ns-name]
- [:a {:href (str "/vars/" ns-name)} ns-name])
+(defprotocol Resource
+ (url-for [o])
+ (link-name [o]))
+
+(extend-protocol Resource
+ Object
+ (url-for [o] nil)
+ (link-name [o] (str o))
+
+ clojure.lang.Var
+ (url-for
+ [v]
+ (let [ns-name (.. v ns name)
+ var-name (.. v sym getName)]
+ (str "/vars/" ns-name "/" (java.net.URLEncoder/encode (str var-name)))))
+ (link-name
+ [v] (.sym v))
+
+ clojure.lang.Namespace
+ (url-for
+ [ns]
+ (str "/vars/" (.name ns)))
+ (link-name
+ [ns]
+ (.name ns))
+
+ Class
+ (url-for
+ [o]
+ (let [classname (.getName o)]
+ (str "/classes/" classname)))
+ (link-name
+ [c]
+ (.getName c)))
+
+(defn link-to
+ [o]
+ (if-let [url (url-for o)]
+ [:a {:href url} (link-name o)]
+ (escape-html (link-name o))))
(defn top-link
[]
[:a {:href (str "/vars")} "top"])
-(defn var-link
- [ns-name var-name]
- [:a {:href (str "/vars/" ns-name "/" (java.net.URLEncoder/encode (str var-name)))} var-name])
-
(defn breadcrumb-text
"Convert the internal names for meta and deref into user-friendly terms,
everything else renders unchanged."
@@ -31,13 +65,13 @@
(defn render
[ns var {:keys [selectors]}]
[:div {:id "breadcrumb"}
- (if ns (top-link) "ns")
+ (if ns (top-link) "top")
(when ns
[:span " « "
- (if var (namespace-link ns) ns)])
+ (if var (link-to ns) ns)])
(when var
[:span " / "
- (if selectors (var-link (.ns var) (.sym var)) (.sym var))])
+ (if selectors (link-to var) (link-name var))])
(when selectors
(let [first-crumb (if (= ::deref (first selectors)) 2 1)]
View
@@ -1,7 +1,6 @@
(ns mycroft.data
(:use clojure.pprint
- [hiccup.core :only (escape-html)]
- [clojure.contrib.core :only (.?.)])
+ [hiccup.core :only (escape-html)])
(:require [mycroft.docs :as docs]
[mycroft.breadcrumb :as breadcrumb]))
@@ -120,9 +119,8 @@
[:span
[:a {:href (str "?" (breadcrumb/options->query-string (add-selector options ::meta)))} "metadata"]]
[:span.disabled-button "metadata"])
- (if-let [classname (.?. selection getClass getName)]
- [:span
- [:a {:href (str "/classes/" classname)} (str "class " classname)]]
+ (if-let [class (.getClass selection)]
+ [:span (breadcrumb/link-to class)]
[:span.disabled-button "no class"])
(if-let [doc-url (docs/doc-url selection)]
[:span
View
@@ -1,29 +1,28 @@
(ns mycroft.namespace
(:require [mycroft.breadcrumb :as breadcrumb]))
-(defn- namespace-names
- "Sorted list of namespace names (strings)."
+(defn namespaces
+ "Sorted list of namespaces"
[]
(->> (all-ns)
- (map #(.name %))
- (sort)))
+ (sort-by #(.name %))))
-(defn- var-names
+(defn vars
"Sorted list of var names in a namespace (symbols)."
[ns]
(when-let [ns (find-ns (symbol ns))]
- (sort (keys (ns-publics ns)))))
+ (sort-by #(str (.sym %)) (vals (ns-publics ns)))))
(defn browser
- ([] (browser (namespace-names)))
- ([ns-names]
+ ([] (browser (namespaces)))
+ ([nses]
[:div
[:div
(breadcrumb/render nil nil nil)]
[:ul
(map
- (fn [ns] [:li (breadcrumb/namespace-link ns)])
- ns-names)]]))
+ (fn [ns] [:li (breadcrumb/link-to ns)])
+ nses)]]))
(defn safe-load-ns
[ns]
@@ -37,8 +36,8 @@
(breadcrumb/render ns nil nil)
[:ul
(map
- (fn [var] [:li (breadcrumb/var-link ns var)])
- (var-names ns))]])
+ (fn [var] [:li (breadcrumb/link-to var)])
+ (vars ns))]])
View
@@ -50,14 +50,23 @@
"\n\tSession " (:session request)))
response))))
+(defn read-param-string
+ "Use Clojure reader to read a param strings, or return
+ default is param string empty/nil."
+ ([s] (read-param-string s nil))
+ ([s default]
+ (if (seq s)
+ (read-string s)
+ default)))
+
(defn normalize-options
"Convert options from string form (as coming in from web)
to data structures as needed."
[options]
(-> (keywordize-keys options)
- (update-in [:selectors] (fnil read-string "nil"))
- (update-in [:headers] (fnil read-string "nil"))
- (update-in [:start] (fnil read-string "0"))))
+ (update-in [:selectors] read-param-string)
+ (update-in [:headers] read-param-string)
+ (update-in [:start] read-param-string 0)))
(defroutes namespace-routes
(GET "/vars" []

0 comments on commit 3b7ea01

Please sign in to comment.