001  (ns ^{:doc "Layout content as HTML."
002        :author "Simon Brooke"}
003    mw-ui.layout
004    (:require [selmer.parser :as parser]
005              [clojure.string :as s]
006              [ring.util.response :refer [content-type response]]
007              [compojure.response :refer [Renderable]]))
008  
009  ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
010  ;;;;
011  ;;;; mw-ui: a servlet user/visualisation interface for MicroWorld.
012  ;;;;
013  ;;;; This program is free software; you can redistribute it and/or
014  ;;;; modify it under the terms of the GNU General Public License
015  ;;;; as published by the Free Software Foundation; either version 2
016  ;;;; of the License, or (at your option) any later version.
017  ;;;;
018  ;;;; This program is distributed in the hope that it will be useful,
019  ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
020  ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
021  ;;;; GNU General Public License for more details.
022  ;;;;
023  ;;;; You should have received a copy of the GNU General Public License
024  ;;;; along with this program; if not, write to the Free Software
025  ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
026  ;;;; USA.
027  ;;;;
028  ;;;; Copyright (C) 2014 Simon Brooke
029  ;;;;
030  ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
031  
032  
033  (def template-path "templates/")
034  
035  (deftype RenderableTemplate [template params]
036    Renderable
037    (render [_ request]
038      (content-type
039        (->> (assoc (merge params {:version (System/getProperty "mw-ui.version")})
040                    (keyword (s/replace template #".html" "-selected")) "active"
041                    :servlet-context
042                    (when-let [context (:servlet-context request)]
043                      (.getContextPath context)))
044          (parser/render-file (str template-path template))
045          response)
046        "text/html; charset=utf-8")))
047  
048  
049  (defn render [template & [params]]
050    (RenderableTemplate. template params))
051