diff --git a/src/adl_support/filters.clj b/src/adl_support/filters.clj new file mode 100644 index 0000000..72f3576 --- /dev/null +++ b/src/adl_support/filters.clj @@ -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 "" arg "")] + 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 "" arg "")] + 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"}) diff --git a/src/adl_support/forms_support.clj b/src/adl_support/forms_support.clj index d51ffa7..fb7d0f1 100644 --- a/src/adl_support/forms_support.clj +++ b/src/adl_support/forms_support.clj @@ -98,6 +98,8 @@ "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 `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] `(merge (reduce merge {} (map #(hash-map (keyword %) nil) ~fields))