1
0
mirror of https://git.sr.ht/~sircmpwn/aerc synced 2024-11-23 16:42:07 +01:00

template: add exec and wrap

This commit is contained in:
Leszek Cimała 2020-01-08 21:44:18 +01:00 committed by Drew DeVault
parent fe9ec67eca
commit bf0f72a533
2 changed files with 41 additions and 5 deletions

@ -72,20 +72,33 @@ available always.
Example: Example:
_wrapText_ function can be used to wrap the original text to a number _wrap_ function can be used to wrap the original text to a number
of characters per line. of characters per line.
``` ```
{{wrapText .OriginalText 72}} {{wrap 72 .OriginalText}}
``` ```
_quote_ function prepends each line with "> ". _quote_ function prepends each line with "> ".
``` ```
{{quote .OriginalText}} {{quote .OriginalText}}
``` ```
_exec_ function execute external command to process message.
All of the above can be chained together if needed, for example
``` ```
{{wrapText .OriginalText 72 | quote}} {{exec `/usr/local/share/aerc/filters/html`}}
```
All of the above can be chained together if needed, for example.
```
{{exec `/usr/local/share/aerc/filters/html` .OriginalText | wrap 72 | quote}}
```
Automatic HTML parsing can be achieved.
```
{{if eq .OriginalMIMEType "text/html"}}
{{exec `/usr/local/share/aerc/filters/html` .OriginalText | wrap 72 | quote}}
{{else}}
{{wrap 72 .OriginalText | quote}}
{{end}}
``` ```
# SEE ALSO # SEE ALSO

@ -5,6 +5,7 @@ import (
"errors" "errors"
"net/mail" "net/mail"
"os" "os"
"os/exec"
"path" "path"
"strings" "strings"
"text/template" "text/template"
@ -72,6 +73,11 @@ func parseAddressList(list string) []*mail.Address {
return addrs return addrs
} }
// wrap allows to chain wrapText
func wrap(lineWidth int, text string) string {
return wrapText(text, lineWidth)
}
func wrapLine(text string, lineWidth int) string { func wrapLine(text string, lineWidth int) string {
words := strings.Fields(text) words := strings.Fields(text)
if len(words) == 0 { if len(words) == 0 {
@ -135,10 +141,27 @@ func quote(text string) string {
return quoted.String() return quoted.String()
} }
// cmd allow to parse reply by shell command
// text have to be passed by cmd param
// if there is error, original string is returned
func cmd(cmd, text string) string {
var out bytes.Buffer
c := exec.Command("sh", "-c", cmd)
c.Stdin = strings.NewReader(text)
c.Stdout = &out
err := c.Run()
if err != nil {
return text
}
return out.String()
}
var templateFuncs = template.FuncMap{ var templateFuncs = template.FuncMap{
"quote": quote, "quote": quote,
"wrapText": wrapText, "wrapText": wrapText,
"wrap": wrap,
"dateFormat": time.Time.Format, "dateFormat": time.Time.Format,
"exec": cmd,
} }
func findTemplate(templateName string, templateDirs []string) (string, error) { func findTemplate(templateName string, templateDirs []string) (string, error) {