about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorLeszek CimaƂa <ernierasta@zori.cz>2020-01-08 21:44:18 +0100
committerDrew DeVault <sir@cmpwn.com>2020-01-09 14:31:19 -0500
commitbf0f72a533d5c1868b9819f769836ea22d5fa583 (patch)
treed120574b57481fd5700c1ce3c12ad4a08bbeef4d
parentfe9ec67eca201c75c09f1ef7ff51ac189de3a4d4 (diff)
downloadaerc-bf0f72a533d5c1868b9819f769836ea22d5fa583.tar.gz
template: add exec and wrap
-rw-r--r--doc/aerc-templates.7.scd21
-rw-r--r--lib/templates/template.go23
2 files changed, 40 insertions, 4 deletions
diff --git a/doc/aerc-templates.7.scd b/doc/aerc-templates.7.scd
index 9382f2e..adcc85c 100644
--- a/doc/aerc-templates.7.scd
+++ b/doc/aerc-templates.7.scd
@@ -72,20 +72,33 @@ available always.
 
 	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.
 	```
-	{{wrapText .OriginalText 72}}
+	{{wrap 72 .OriginalText}}
 	```
 
 	_quote_ function prepends each line with "> ".
 	```
 	{{quote .OriginalText}}
 	```
+	_exec_ function execute external command to process message.
+	```
+	{{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}}
+	```
 
-	All of the above can be chained together if needed, for example
+	Automatic HTML parsing can be achieved.
 	```
-	{{wrapText .OriginalText 72 | quote}}
+	{{if eq .OriginalMIMEType "text/html"}}
+	{{exec `/usr/local/share/aerc/filters/html` .OriginalText | wrap 72 | quote}}
+	{{else}}
+	{{wrap 72 .OriginalText | quote}}
+	{{end}}
 	```
 
 # SEE ALSO
diff --git a/lib/templates/template.go b/lib/templates/template.go
index 5402472..f2765e8 100644
--- a/lib/templates/template.go
+++ b/lib/templates/template.go
@@ -5,6 +5,7 @@ import (
 	"errors"
 	"net/mail"
 	"os"
+	"os/exec"
 	"path"
 	"strings"
 	"text/template"
@@ -72,6 +73,11 @@ func parseAddressList(list string) []*mail.Address {
 	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 {
 	words := strings.Fields(text)
 	if len(words) == 0 {
@@ -135,10 +141,27 @@ func quote(text string) 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{
 	"quote":      quote,
 	"wrapText":   wrapText,
+	"wrap":       wrap,
 	"dateFormat": time.Time.Format,
+	"exec":       cmd,
 }
 
 func findTemplate(templateName string, templateDirs []string) (string, error) {