From 3ba69edab5f0c787424dac9649e43a7743da13ca Mon Sep 17 00:00:00 2001 From: Srivathsan Murali Date: Sun, 3 Nov 2019 13:51:14 +0100 Subject: Add Templates with Parsing + Changes NewComposer to return error. + Add lib to handle templates using "text/template". + Add -T option to following commands - compose. - reply - forward + Quoted replies using templates. + Forwards as body using templates + Default templates are installed similar to filters. + Templates Config in aerc.conf. - Required templates are parsed while loading config. + Add aerc-templates.7 manual for using template data. --- widgets/aerc.go | 7 +++++-- widgets/compose.go | 37 ++++++++++++++++++++++++++++++++++--- 2 files changed, 39 insertions(+), 5 deletions(-) (limited to 'widgets') diff --git a/widgets/aerc.go b/widgets/aerc.go index af51a0f4..d324908b 100644 --- a/widgets/aerc.go +++ b/widgets/aerc.go @@ -431,8 +431,11 @@ func (aerc *Aerc) Mailto(addr *url.URL) error { defaults[header] = strings.Join(vals, ",") } } - composer := NewComposer(aerc, aerc.Config(), - acct.AccountConfig(), acct.Worker(), defaults) + composer, err := NewComposer(aerc, aerc.Config(), + acct.AccountConfig(), acct.Worker(), "", defaults) + if err != nil { + return nil + } composer.FocusSubject() title := "New email" if subj, ok := defaults["Subject"]; ok { diff --git a/widgets/compose.go b/widgets/compose.go index 22c58da5..a55d147f 100644 --- a/widgets/compose.go +++ b/widgets/compose.go @@ -23,6 +23,7 @@ import ( "github.com/pkg/errors" "git.sr.ht/~sircmpwn/aerc/config" + "git.sr.ht/~sircmpwn/aerc/lib/templates" "git.sr.ht/~sircmpwn/aerc/lib/ui" "git.sr.ht/~sircmpwn/aerc/worker/types" ) @@ -53,7 +54,7 @@ type Composer struct { } func NewComposer(aerc *Aerc, conf *config.AercConfig, - acct *config.AccountConfig, worker *types.Worker, defaults map[string]string) *Composer { + acct *config.AccountConfig, worker *types.Worker, template string, defaults map[string]string) (*Composer, error) { if defaults == nil { defaults = make(map[string]string) @@ -62,13 +63,14 @@ func NewComposer(aerc *Aerc, conf *config.AercConfig, defaults["From"] = acct.From } + templateData := templates.ParseTemplateData(defaults) layout, editors, focusable := buildComposeHeader( conf.Compose.HeaderLayout, defaults) email, err := ioutil.TempFile("", "aerc-compose-*.eml") if err != nil { // TODO: handle this better - return nil + return nil, err } c := &Composer{ @@ -86,11 +88,14 @@ func NewComposer(aerc *Aerc, conf *config.AercConfig, } c.AddSignature() + if err := c.AddTemplate(template, templateData); err != nil { + return nil, err + } c.updateGrid() c.ShowTerminal() - return c + return c, nil } func buildComposeHeader(layout HeaderLayout, defaults map[string]string) ( @@ -163,6 +168,32 @@ func (c *Composer) AppendContents(reader io.Reader) { c.email.Sync() } +func (c *Composer) AddTemplate(template string, data interface{}) error { + if template == "" { + return nil + } + + templateText, err := templates.ParseTemplateFromFile(template, c.config.Templates.TemplateDirs, data) + if err != nil { + return err + } + c.PrependContents(bytes.NewReader(templateText)) + return nil +} + +func (c *Composer) AddTemplateFromString(template string, data interface{}) error { + if template == "" { + return nil + } + + templateText, err := templates.ParseTemplate(template, data) + if err != nil { + return err + } + c.PrependContents(bytes.NewReader(templateText)) + return nil +} + func (c *Composer) AddSignature() { var signature []byte if c.acct.SignatureCmd != "" { -- cgit