diff --git a/src/mw_ui/handler.clj b/src/mw_ui/handler.clj index 322aadf..7915b62 100644 --- a/src/mw_ui/handler.clj +++ b/src/mw_ui/handler.clj @@ -46,6 +46,7 @@ an app server such as Tomcat put any initialization code here" [] + ;; TODO fix timbre config! ;; (timbre/set-config! ;; [:appenders :rotor] ;; {:min-level :info diff --git a/src/mw_ui/layout.clj b/src/mw_ui/layout.clj index 9861426..da3175b 100644 --- a/src/mw_ui/layout.clj +++ b/src/mw_ui/layout.clj @@ -35,12 +35,12 @@ (deftype RenderableTemplate [template params] Renderable - (render [this request] + (render [_ request] (content-type (->> (assoc (merge params {:version (System/getProperty "mw-ui.version")}) (keyword (s/replace template #".html" "-selected")) "active" :servlet-context - (if-let [context (:servlet-context request)] + (when-let [context (:servlet-context request)] (.getContextPath context))) (parser/render-file (str template-path template)) response) diff --git a/src/mw_ui/render_world.clj b/src/mw_ui/render_world.clj index 2d91180..6499339 100644 --- a/src/mw_ui/render_world.clj +++ b/src/mw_ui/render_world.clj @@ -34,9 +34,10 @@ ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; -(defn format-css-class [statekey] +(defn format-css-class "Format this statekey, assumed to be a keyword indicating a state in the world, into a CSS class" + [statekey] (subs (str statekey) 1)) @@ -92,7 +93,8 @@ (defn render-inspector "Render in Hiccup format the HTML content of an inspector on this cell." - [cell table] + ([cell _] (render-inspector cell)) + ([cell] [:table {:class "music-ruled"} [:tr [:td {:colspan 2 :style "text-align: center;"} @@ -100,6 +102,6 @@ :width 64 :height 64}]]] [:tr [:th "Key"][:th "Value"]] - (map #(vector :tr (vector :th %)(vector :td (cell %))) (keys cell))]) + (map #(vector :tr (vector :th %)(vector :td (cell %))) (keys cell))])) diff --git a/src/mw_ui/routes/home.clj b/src/mw_ui/routes/home.clj index efdda85..0e82dc1 100644 --- a/src/mw_ui/routes/home.clj +++ b/src/mw_ui/routes/home.clj @@ -1,9 +1,9 @@ (ns ^{:doc "Routes which serve the main pages of the application." :author "Simon Brooke"} mw-ui.routes.home - (:use clojure.walk - compojure.core) - (:require [clojure.pprint :only [pprint]] + (:require [clojure.java/io :refer [file]] + [clojure.walk :refer [keywordize-keys]] + [compojure.core :refer [defroutes GET POST]] [hiccup.core :refer [html]] [mw-engine.utils :as engine-utils] [mw-ui.layout :as layout] @@ -13,7 +13,6 @@ [mw-ui.routes.params :as params] [mw-ui.routes.save :as save] [mw-ui.util :as util] - [noir.io :as io] [noir.session :as session] [ring.util.response :as response])) @@ -45,7 +44,8 @@ (sort (filter #(not (nil? %)) (map #(first (rest (re-matches #"([0-9a-z-]+).png" (.getName %)))) - (file-seq (clojure.java.io/file "resources/public/img/tiles")))))) + ;; TODO: this will not work when running from jar; see utils.clj + (file-seq (file "resources/public/img/tiles")))))) (defn about-page [] @@ -63,14 +63,16 @@ :components ["mw-engine" "mw-parser" "mw-ui"] :version (System/getProperty "mw-ui.version")})) -(defn home-page [] +(defn home-page "Render the home page." + [] (layout/render "trusted-content.html" {:title "Welcome to MicroWorld" :content (util/md->html "/md/mw-ui.md") :version (System/getProperty "mw-ui.version")})) -(defn inspect-page [request] +(defn inspect-page "Open an inspector on the cell at the co-ordinates specified in this request" + [request] (let [params (keywordize-keys (:params request)) xs (:x params) ys (:y params) @@ -86,7 +88,7 @@ true (layout/render "inspector.html" {:title (format "Inspect cell at %d, %d" x y) - :content (html (world/render-inspector cell world)) + :content (html (world/render-inspector cell)) :cell cell :x (:x cell) :y (:y cell) diff --git a/src/mw_ui/routes/load.clj b/src/mw_ui/routes/load.clj index 0636ccf..7894ab6 100644 --- a/src/mw_ui/routes/load.clj +++ b/src/mw_ui/routes/load.clj @@ -1,14 +1,10 @@ (ns ^{:doc "Route which handles the upload of worlds/rules from the client." :author "Simon Brooke"} mw-ui.routes.load - (:use clojure.walk - compojure.core) - (:require [hiccup.core :refer [html]] + (:require [clojure.walk :refer [keywordize-keys]] [noir.io :as io] [noir.session :as session] - [ring.util.response :as response] - [mw-ui.layout :as layout] - )) + [mw-ui.layout :as layout])) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;; @@ -38,7 +34,7 @@ (io/upload-file "/tmp/" file) (cond (session/put! :world - (with-open [eddi (java.io.FileReader. (:tempfile file))] (read))) + (with-open [_ (java.io.FileReader. (:tempfile file))] (read))) (str "Successfully loaded your world from " (:filename file)))) @@ -52,8 +48,7 @@ ([] (load-page nil)) ([request] - (let [params (keywordize-keys (:params request)) - file (:file request)] + (let [file (:file request)] (try (layout/render "load.html" {:title "Load World" diff --git a/src/mw_ui/routes/params.clj b/src/mw_ui/routes/params.clj index f68fcb3..2d6d781 100644 --- a/src/mw_ui/routes/params.clj +++ b/src/mw_ui/routes/params.clj @@ -1,15 +1,11 @@ (ns ^{:doc "Route which serves and handles the parameters page." :author "Simon Brooke"} mw-ui.routes.params - (:use clojure.walk - clojure.java.io - compojure.core) - (:require [hiccup.core :refer [html]] + (:require [clojure.walk :refer [keywordize-keys]] [mw-engine.heightmap :as heightmap] [mw-parser.bulk :as compiler] [mw-ui.layout :as layout] [mw-ui.util :as util] - [mw-ui.render-world :as world] [noir.io :as io] [noir.session :as session])) @@ -58,14 +54,14 @@ pause (:pause params) rulefile (:ruleset params) rulepath (str "/rulesets/" rulefile ".txt")] - (if (not= map "") + (when (not= map "") (session/put! :world (heightmap/apply-heightmap (io/get-resource (str "/img/heightmaps/" map ".png"))))) (when (not= rulefile "") (session/put! :rule-text (io/slurp-resource rulepath)) (session/put! :rules (compiler/compile-file (io/get-resource rulepath)))) - (if (not= pause "") + (when (not= pause "") (session/put! :pause pause)) (layout/render "params.html" (merge (send-params) diff --git a/src/mw_ui/routes/rules.clj b/src/mw_ui/routes/rules.clj index b49a4c8..bd43291 100644 --- a/src/mw_ui/routes/rules.clj +++ b/src/mw_ui/routes/rules.clj @@ -1,16 +1,11 @@ (ns ^{:doc "Route which serves and handles the rules page." :author "Simon Brooke"} mw-ui.routes.rules - (:use clojure.walk - compojure.core) - (:require [hiccup.core :refer [html]] + (:require [clojure.walk :refer [keywordize-keys]] [mw-parser.bulk :as compiler] [mw-ui.layout :as layout] - [mw-ui.util :as util] - [mw-ui.render-world :as world] [noir.io :as io] - [noir.session :as session] - [ring.util.response :as response])) + [noir.session :as session])) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;; @@ -47,7 +42,7 @@ :message (str "Successfully compiled " (count rules) " rules") }) - true {:rule-text (or + :else {:rule-text (or (session/get :rule-text) (io/slurp-resource "/rulesets/basic.txt")) :message "No rules found in request; loading defaults"}) @@ -67,9 +62,9 @@ the session or from `resources/rulesets/basic.txt` and pass that back." ([request] (let [processed (process-rules-request request)] - (if (:rules processed) + (when (:rules processed) (session/put! :rules (:rules processed))) - (if (:rule-text processed) + (when (:rule-text processed) (session/put! :rule-text (:rule-text processed))) (layout/render "rules.html" (merge {:title "Edit Rules"} processed)))) diff --git a/src/mw_ui/routes/save.clj b/src/mw_ui/routes/save.clj index 407495b..6f1f760 100644 --- a/src/mw_ui/routes/save.clj +++ b/src/mw_ui/routes/save.clj @@ -1,7 +1,7 @@ (ns ^{:doc "Route which handles the saving of world state the client." :author "Simon Brooke"} mw-ui.routes.save - (:require [clojure.pprint :as pretty :only [pprint]] + (:require [clojure.pprint :refer [pprint]] [noir.session :as session] [noir.response :as response])) @@ -29,12 +29,13 @@ ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; -(defn save-page [] +(defn save-page "Save the current world to the browser, using our own custom mime-type in an attempt to prevent the browser trying to do anything clever with it. Note that it is saved as a raw Clojure data structure, not as XML or any proprietary format." + [] (response/content-type "application/journeyman-mwm; charset=utf-8" - (with-out-str (pretty/pprint (session/get :world))))) + (with-out-str (pprint (session/get :world))))) diff --git a/src/mw_ui/util.clj b/src/mw_ui/util.clj index fa59383..c5ff74d 100644 --- a/src/mw_ui/util.clj +++ b/src/mw_ui/util.clj @@ -1,7 +1,8 @@ (ns ^{:doc "Utility functions used by other namespaces in this package." :author "Simon Brooke"} mw-ui.util - (:require [noir.io :as io] + (:require [clojure.java.io :refer [file]] + [noir.io :as io] [noir.session :as session] [markdown.core :as md])) @@ -36,13 +37,17 @@ (io/slurp-resource filename) (md/md-to-html-string))) +;; TODO: The reason we can't list files in a jar file, and what to do about it, +;; is here. Too tired to fix this tonight. +;; https://stackoverflow.com/questions/46488466/clojure-list-subfolders-in-resources-in-uberjar -(defn list-resources [directory pattern] +(defn list-resources "List resource files matching `pattern` in `directory`." + [directory pattern] (let [path (str (io/resource-path) directory)] (session/put! :list-resources-path path) (sort (remove nil? (map #(first (rest (re-matches pattern (.getName %)))) - (file-seq (clojure.java.io/file path))))))) + (file-seq (file path)))))))