Added Selmer filters for address and email

This commit is contained in:
Simon Brooke 2018-08-07 14:50:50 +01:00
parent d6991b5f09
commit ad4e6ef134
2 changed files with 65 additions and 0 deletions

View file

@ -0,0 +1,63 @@
(ns adl-support.filters
(:require [clojure.string :as s]
[selmer.filters :as f]
[selmer.parser :as p]))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;
;;;; adl-support.filters: selmer filters required by ADL selmer views.
;;;;
;;;; This program is free software; you can redistribute it and/or
;;;; modify it under the terms of the MIT-style licence provided; see LICENSE.
;;;;
;;;; This program is distributed in the hope that it will be useful,
;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;;;; License for more details.
;;;;
;;;; Copyright (C) 2018 Simon Brooke
;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(def *default-international-dialing-prefix*
"The international dialing prefix to use, if none is specified."
"44")
(defn telephone
"If `arg` is, or appears to be, a valid telephone number, convert it into
a `tel:` link, else leave it be."
[^String arg]
(let [number
(s/replace
(s/replace
arg
#"^0"
(str "+" *default-international-dialing-prefix* "-"))
#"\s+" "-")]
(if (re-matches #"[0-9 +-]*" arg)
[:safe (str "<a href='tel:" number "'>" arg "</a>")]
arg)))
;; (telephone "07768 130255")
;; (telephone "Freddy")
(defn email
"If `arg` is, or appears to be, a valid email address, convert it into
a `mailto:` link, else leave it be."
[^String arg]
(if (re-matches #"^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,6}$" arg)
[:safe (str "<a href='mailto:" arg "'>" arg "</a>")]
arg))
;; (email "simon@journeyman.cc")
;; (email "simon@journeyman")
;; (email "simon@journeyman.cc.")
(f/add-filter! :telephone telephone)
(f/add-filter! :email email)
;; (p/render "{{p|telephone}}" {:p "07768 130255"})

View file

@ -98,6 +98,8 @@
"Params for insertion into the database must have keys for all fields in the "Params for insertion into the database must have keys for all fields in the
insert query, even if the value of some of those keys is nil. Massage these insert query, even if the value of some of those keys is nil. Massage these
`params` to have a value for each field in these `fields`." `params` to have a value for each field in these `fields`."
;; TODO: should intelligently handle dates and times, but that might imply
;; access to ADL at runtime!
[params fields] [params fields]
`(merge `(merge
(reduce merge {} (map #(hash-map (keyword %) nil) ~fields)) (reduce merge {} (map #(hash-map (keyword %) nil) ~fields))