aboutsummaryrefslogtreecommitdiffstats
path: root/lib/templates
diff options
context:
space:
mode:
authorMoritz Poldrack <git@moritz.sh>2024-01-18 23:33:30 +0100
committerRobin Jarry <robin@jarry.cc>2024-01-19 23:31:52 +0100
commit2d9d7e0bed87d11771a7264c08c02d110eef6751 (patch)
treef8aa544d86b1f3fae675e79f318db5556d1ddfba /lib/templates
parent978b5e444215255a9671ce439f1594888697f799 (diff)
downloadaerc-2d9d7e0bed87d11771a7264c08c02d110eef6751.tar.gz
templates: add basic string functions
Some clients are sending a text/plain part that contains nothing but the text/html part. This is rather suboptimal when replying. To be able to filter these, it is important to be able to detect things like <!doctype html>. Add basic string operations to the template functions. Signed-off-by: Moritz Poldrack <git@moritz.sh> Tested-by: Koni Marti <koni.marti@gmail.com> Acked-by: Robin Jarry <robin@jarry.cc>
Diffstat (limited to 'lib/templates')
-rw-r--r--lib/templates/functions.go25
1 files changed, 20 insertions, 5 deletions
diff --git a/lib/templates/functions.go b/lib/templates/functions.go
index 692974fd..8b7af449 100644
--- a/lib/templates/functions.go
+++ b/lib/templates/functions.go
@@ -6,6 +6,7 @@ import (
"fmt"
"os"
"os/exec"
+ "regexp"
"strings"
"text/template"
"time"
@@ -241,11 +242,7 @@ func join(sep string, elems []string) string {
}
func split(sep string, s string) []string {
- sp := strings.Split(s, sep)
- for i := range sp {
- sp[i] = strings.TrimSpace(sp[i])
- }
- return sp
+ return strings.Split(s, sep)
}
// removes a signature from the piped in message
@@ -324,6 +321,19 @@ top:
return mapped
}
+func replace(pattern, subst, value string) string {
+ re := regexp.MustCompile(pattern)
+ return re.ReplaceAllString(value, subst)
+}
+
+func contains(substring, s string) bool {
+ return strings.Contains(s, substring)
+}
+
+func hasPrefix(prefix, s string) bool {
+ return strings.HasPrefix(s, prefix)
+}
+
var templateFuncs = template.FuncMap{
"quote": quote,
"wrapText": wrapText,
@@ -352,4 +362,9 @@ var templateFuncs = template.FuncMap{
"default": default_,
"map": map_,
"exclude": exclude,
+ "contains": contains,
+ "hasPrefix": hasPrefix,
+ "toLower": strings.ToLower,
+ "toUpper": strings.ToUpper,
+ "replace": replace,
}