From 17ce7c762cb10ce4272de8ddd45367ba94360456 Mon Sep 17 00:00:00 2001 From: Moritz Poldrack Date: Fri, 27 Jan 2023 19:48:55 +0100 Subject: templates: add trimSignature function Some contacts, especially corporate, include a wall of text in their signatures. To not clutter the reply chain, this commit introduces a new function to the templating engine that removes the signature from a message. Link: https://learn.microsoft.com/en-us/microsoft-365/admin/setup/create-signatures-and-disclaimers Signed-off-by: Moritz Poldrack Acked-by: Robin Jarry --- lib/templates/functions.go | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) (limited to 'lib') diff --git a/lib/templates/functions.go b/lib/templates/functions.go index 2e551594..f6cb0df4 100644 --- a/lib/templates/functions.go +++ b/lib/templates/functions.go @@ -1,6 +1,7 @@ package templates import ( + "bufio" "bytes" "fmt" "os" @@ -181,6 +182,23 @@ func join(sep string, elems []string) string { return strings.Join(elems, sep) } +// removes a signature from the piped in message +func trimSignature(message string) string { + var res strings.Builder + + input := bufio.NewScanner(strings.NewReader(message)) + + for input.Scan() { + line := input.Text() + if line == "-- " { + break + } + res.WriteString(line) + res.WriteRune('\n') + } + return res.String() +} + var templateFuncs = template.FuncMap{ "quote": quote, "wrapText": wrapText, @@ -196,4 +214,5 @@ var templateFuncs = template.FuncMap{ "humanReadable": humanReadable, "cwd": cwd, "join": join, + "trimSignature": trimSignature, } -- cgit