aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--doc/aerc-templates.7.scd35
-rw-r--r--lib/templates/functions.go25
2 files changed, 55 insertions, 5 deletions
diff --git a/doc/aerc-templates.7.scd b/doc/aerc-templates.7.scd
index 06b34781..160fb77f 100644
--- a/doc/aerc-templates.7.scd
+++ b/doc/aerc-templates.7.scd
@@ -440,6 +440,41 @@ aerc provides the following additional functions:
{{compactDir .Folder}}
```
+*contains*
+ Checks if a string contains a substring.
+
+ ```
+ {{contains "<!DOCTYPE html>" .OriginalText}}
+ ```
+
+*hasPrefix*
+ Checks if a string has a prefix.
+
+ ```
+ {{hasPrefix "Business" .Folder}}
+ ```
+
+*toLower*
+ Convert a string to lowercase.
+
+ ```
+ {{toLower "SPECIAL OFFER!"}}
+ ```
+
+*toUpper*
+ Convert a string to uppercase.
+
+ ```
+ {{toUpper "important"}}
+ ```
+
+*replace*
+ Perform a regular expression substitution on the passed string.
+
+ ```
+ {{replace `(.+) - .+ at .+\..+` `$1` ((index .OriginalFrom 0).Name)}}
+ ```
+
*.Style*
Apply a user-defined style (see *aerc-stylesets*(7)) to a string.
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,
}