yenu/src/clj/yenu/layout.clj

66 lines
2.4 KiB
Clojure
Raw Normal View History

(ns yenu.layout
(:require [selmer.parser :as parser]
2017-03-17 00:23:13 +01:00
[selmer.filters :as filter]
[ring.util.http-response :refer [content-type ok]]
[ring.util.anti-forgery :refer [anti-forgery-field]]
2017-03-15 00:28:27 +01:00
[yenu.helpers.images :as image-helper]
2017-03-21 23:35:13 +01:00
[ring.middleware.anti-forgery :refer [*anti-forgery-token*]]
2017-04-23 22:33:28 +02:00
[yenu.config :refer [env]]
[yenu.db.core :as db]
2017-04-23 22:33:28 +02:00
[digest :as digest]
[java-time :refer [local-date-time]])
2017-03-21 23:35:13 +01:00
(:use [markdown.core]))
2017-03-29 14:39:03 +02:00
(declare ^:dynamic *identity*)
(declare ^:dynamic *app-context*)
(parser/set-resource-path! (clojure.java.io/resource "templates"))
(parser/add-tag! :csrf-field (fn [_ _] (anti-forgery-field)))
2017-03-17 00:23:13 +01:00
(filter/add-filter! :inc inc)
(filter/add-filter! :dec dec)
2017-03-21 23:35:13 +01:00
(filter/add-filter! :markdown-to-html md-to-html-string)
(filter/add-filter! :parse-date #(local-date-time "yyyy-MM-dd HH:mm:ss" %))
(defn footer []
2018-03-22 00:33:26 +01:00
(if (= (:count (db/get-image-count)) 0)
2017-05-12 22:37:22 +02:00
(clojure.string/join ", " (:authors env))
(let [first-year (subs (:created_at (db/get-first-year)) 0 4)
recent-year (subs (:created_at (db/get-recent-year)) 0 4)
copy (if (= first-year recent-year) first-year (str first-year " - " recent-year))]
(str "© " copy " " (:authors env)))))
2017-04-23 22:33:28 +02:00
(defn render-file [template & [params]]
(content-type
2017-03-15 00:28:27 +01:00
(ok
(parser/render-file
template
(assoc params
:page template
:footer (footer)
2017-03-15 00:28:27 +01:00
:csrf-token *anti-forgery-token*
2017-03-29 14:39:03 +02:00
:servlet-context *app-context*
2017-04-23 22:33:28 +02:00
:passwordhash (digest/md5 (:user-password env))
2017-03-29 22:14:55 +02:00
:identity *identity*)))
2017-03-15 00:28:27 +01:00
"text/html; charset=utf-8"))
2017-04-23 22:33:28 +02:00
(defn render [string contenttype]
(content-type
(-> (ok string)
(assoc :servlet-context *app-context*
:csrf-token *anti-forgery-token*
:passwordhash (digest/md5 (:user-password env))
:identity *identity*))
(str contenttype "; charset=utf-8")))
(defn error-page
"error-details should be a map containing the following keys:
:status - error status
:title - error title (optional)
:message - detailed error message (optional)
returns a response map with the error page as the body
and the status specified by the status key"
[error-details]
{:status (:status error-details)
:headers {"Content-Type" "text/html; charset=utf-8"}
:body (parser/render-file "error.html" error-details)})