Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: doo/lein-hiccster
base: 01fb6c5843
...
head fork: doo/lein-hiccster
compare: 17f9d34e18
  • 2 commits
  • 2 files changed
  • 0 commit comments
  • 2 contributors
Showing with 39 additions and 21 deletions.
  1. +1 −1  project.clj
  2. +38 −20 src/leiningen/hiccster.clj
View
2  project.clj
@@ -1,4 +1,4 @@
-(defproject lein-hiccster "0.1.1"
+(defproject lein-hiccster "0.1.2-SNAPSHOT"
:description "A hiccup page server plugin for Leiningen"
:dependencies [[org.clojure/clojure "1.3.0"]
[hiccup "1.0.0-beta1"]
View
58 src/leiningen/hiccster.clj
@@ -6,13 +6,16 @@
ring.middleware.file
ring.middleware.file-info
ring.util.response
- ring.adapter.jetty))
+ ring.adapter.jetty)
+ (:import java.io.File))
(def ^{:dynamic true :private true}
*modified-namespaces* nil)
(def *pages* (atom #{}))
+(def root-namespace (atom nil))
+
(defn- log [& msg]
(println (str "[" (java.util.Date.) "]")
(apply str msg)))
@@ -35,32 +38,46 @@
(response)
(content-type "text/html; charset=UTF-8")))
+(defn- ns-ref [ns var-name]
+ (get (ns-publics ns) var-name))
+
+(defn- file-name->ns-sym [filename]
+ (-> (.substring (str filename) 0 (.lastIndexOf (str filename) "."))
+ (.replaceAll (str File/separatorChar) ".")))
+
+(defn- ns-sym->file-name [ns-sym]
+ (-> (str ns-sym)
+ (.replaceAll (str "^" (java.util.regex.Pattern/quote (str @root-namespace "."))) "")
+ (.replace \. File/separatorChar)
+ (.concat ".")
+ (.concat (or (ns-ref (find-ns ns-sym) 'file-extension) "html"))))
+
(defn- page-index []
(html-response
[:html [:head [:title "hiccster page index"]]
[:body
[:h1 "pages"]
- [:ul (-> (map (fn [page]
- [:li [:a {:href (str "/" page)}
- (str page)]])
- (sort (fn [a b] (compare (str a) (str b)))
- (map name (deref *pages*)))))]]]))
+ [:ul (map (fn [page]
+ (let [file (ns-sym->file-name page)]
+ [:li [:a {:href (str "/" file)} file]]))
+ (sort (fn [a b] (compare (str a) (str b)))
+ (deref *pages*)))]]]))
(defn- handle-page [req]
(reload-modified-namespaces false)
- (let [ns-sym (->> (.substring (:uri req) 1)
- (symbol))
- ns (find-ns ns-sym)]
- (let [page (and ns (ns-resolve ns-sym 'page))]
- (cond page
- (-> (binding [*request* req]
- (page))
- (html-response))
- (= (:uri req) "/")
- (page-index)
- :else
- (-> (response (str "var " ns-sym "/page not found"))
- (status 404))))))
+ (if (= (:uri req) "/")
+ (page-index)
+ (let [ns-sym (->> (.substring (:uri req) 1)
+ (file-name->ns-sym)
+ (str @root-namespace ".")
+ (symbol))
+ ns (find-ns ns-sym)]
+ (if-let [page (and ns (ns-resolve ns-sym 'page))]
+ (-> (binding [*request* req]
+ (page))
+ (html-response))
+ (-> (response (str "var " ns-sym "/page not found"))
+ (status 404))))))
(defn- wrap-logging [handler]
(fn [req]
@@ -83,7 +100,7 @@
(reload-modified-namespaces quiet?))
(defn hiccster-config []
- (let [file (java.io.File. ".hiccster")]
+ (let [file (File. ".hiccster")]
(when (.exists file)
(with-open [f (java.io.PushbackReader. (java.io.FileReader. file))]
(read f)))))
@@ -92,4 +109,5 @@
([project] (hiccster project "src"))
([project & dirs]
(init! dirs)
+ (reset! root-namespace (:hiccster-root-namespace project))
(run-jetty #'handler {:port (get (hiccster-config) :port 8765)})))

No commit comments for this range

Something went wrong with that request. Please try again.