From 8299617ebc24a4a5bd1dc03070e17713be7e1e1b Mon Sep 17 00:00:00 2001 From: Simon Ser Date: Wed, 12 Feb 2020 14:42:51 +0100 Subject: Turn message part viewers into plugins --- plugins/viewtext/plugin.go | 10 ++++++++++ plugins/viewtext/viewer.go | 49 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+) create mode 100644 plugins/viewtext/plugin.go create mode 100644 plugins/viewtext/viewer.go (limited to 'plugins/viewtext') diff --git a/plugins/viewtext/plugin.go b/plugins/viewtext/plugin.go new file mode 100644 index 0000000..c7a2bcc --- /dev/null +++ b/plugins/viewtext/plugin.go @@ -0,0 +1,10 @@ +package koushinviewtext + +import ( + "git.sr.ht/~emersion/koushin" +) + +func init() { + p := koushin.GoPlugin{Name: "viewtext"} + koushin.RegisterPluginLoader(p.Loader()) +} diff --git a/plugins/viewtext/viewer.go b/plugins/viewtext/viewer.go new file mode 100644 index 0000000..cca38a8 --- /dev/null +++ b/plugins/viewtext/viewer.go @@ -0,0 +1,49 @@ +package koushinviewtext + +import ( + "bytes" + "fmt" + "html/template" + "io/ioutil" + "strings" + + "git.sr.ht/~emersion/koushin" + koushinbase "git.sr.ht/~emersion/koushin/plugins/base" + "github.com/emersion/go-message" +) + +// TODO: dim quotes and "On xxx, xxx wrote:" lines +// TODO: turn URLs into links + +const tpl = `
{{.}}
` + +type viewer struct{} + +func (viewer) ViewMessagePart(ctx *koushin.Context, msg *koushinbase.IMAPMessage, part *message.Entity) (interface{}, error) { + mimeType, _, err := part.Header.ContentType() + if err != nil { + return nil, err + } + if !strings.EqualFold(mimeType, "text/plain") { + return nil, koushinbase.ErrViewUnsupported + } + + body, err := ioutil.ReadAll(part.Body) + if err != nil { + return nil, fmt.Errorf("failed to read part body: %v", err) + } + + t := template.Must(template.New("view-text.html").Parse(tpl)) + + var buf bytes.Buffer + err = t.Execute(&buf, string(body)) + if err != nil { + return nil, err + } + + return template.HTML(buf.String()), nil +} + +func init() { + koushinbase.RegisterViewer(viewer{}) +} -- cgit v1.2.3