Skip to content

Commit

Permalink
templates: add basic string functions
Browse files Browse the repository at this point in the history
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 <[email protected]>
Tested-by: Koni Marti <[email protected]>
Acked-by: Robin Jarry <[email protected]>
  • Loading branch information
mpldr authored and rjarry committed Jan 19, 2024
1 parent 978b5e4 commit 2d9d7e0
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 5 deletions.
35 changes: 35 additions & 0 deletions doc/aerc-templates.7.scd
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
25 changes: 20 additions & 5 deletions lib/templates/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"os"
"os/exec"
"regexp"
"strings"
"text/template"
"time"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
}

0 comments on commit 2d9d7e0

Please sign in to comment.