From 90b0bfb4a4c8d4aaeefe6913b3e2012925b5f7f9 Mon Sep 17 00:00:00 2001 From: Simon Brooke Date: Tue, 7 Aug 2018 14:50:50 +0100 Subject: [PATCH] Added Selmer filters for address and email --- src/adl_support/filters.clj | 63 +++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 src/adl_support/filters.clj 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"})