From 93048cd5a323d867081e72ba228f59bdad00bf2e Mon Sep 17 00:00:00 2001 From: gutmet Date: Wed, 23 Nov 2022 23:40:58 +0100 Subject: [PATCH] strip html tags by default --- go.mod | 1 + go.sum | 2 ++ swill.go | 10 ++++++++++ 3 files changed, 13 insertions(+) diff --git a/go.mod b/go.mod index 99c4357..2fd2118 100644 --- a/go.mod +++ b/go.mod @@ -5,4 +5,5 @@ go 1.16 require ( git.gutmet.org/go-mastodon.git v0.0.0-20221123195156-1cc330baf3e9 git.gutmet.org/goutil.git v0.0.0-20201108182825-c19893df11f9 + github.com/grokify/html-strip-tags-go v0.0.1 ) diff --git a/go.sum b/go.sum index 87bba47..6da48a1 100644 --- a/go.sum +++ b/go.sum @@ -4,3 +4,5 @@ git.gutmet.org/goutil.git v0.0.0-20201108182825-c19893df11f9 h1:XVD037Slgdl/CxcC git.gutmet.org/goutil.git v0.0.0-20201108182825-c19893df11f9/go.mod h1:iMgpxo9FxmbnUiQu5ugpjdtVZmh2rA9nySCr/GHkA64= git.gutmet.org/linkheader.git v0.0.0-20221120205136-a51e89fd8486 h1:7F1dwJvIgvHNvglosyIE7SA49BwG6b8DFkvD8NtHMD8= git.gutmet.org/linkheader.git v0.0.0-20221120205136-a51e89fd8486/go.mod h1:xArmd5A1lL5BEfB56+FUwqeG4XDfAvFGe35pqAgifCc= +github.com/grokify/html-strip-tags-go v0.0.1 h1:0fThFwLbW7P/kOiTBs03FsJSV9RM2M/Q/MOnCQxKMo0= +github.com/grokify/html-strip-tags-go v0.0.1/go.mod h1:2Su6romC5/1VXOQMaWL2yb618ARB8iVo6/DR99A6d78= diff --git a/swill.go b/swill.go index 0bc8488..31e18b8 100644 --- a/swill.go +++ b/swill.go @@ -13,6 +13,7 @@ import ( "git.gutmet.org/go-mastodon.git" goutil "git.gutmet.org/goutil.git/misc" + strip "github.com/grokify/html-strip-tags-go" ) const ( @@ -21,6 +22,7 @@ const ( ) var formatTemplate *template.Template +var stripHTML bool type mediaKind int @@ -249,6 +251,11 @@ func updateStatus(f statusFlags, args []string) []*mastodon.Status { } func PrintStatuses(statuses []*mastodon.Status) { + if stripHTML { + for _, status := range statuses { + status.Content = strip.StripTags(status.Content) + } + } if formatTemplate != nil { optLogFatal("printStatuses", formatTemplate.Execute(os.Stdout, statuses)) } else { @@ -304,6 +311,7 @@ func userTimelineCommand() (goutil.CommandFlagsInit, goutil.CommandFunc) { type generalFlags struct { templateFile string + stripHTML bool } func wrapCommand(cmd goutil.CommandFunc) func() (goutil.CommandFlagsInit, goutil.CommandFunc) { @@ -314,6 +322,7 @@ func wrapCommandFl(cmd func() (goutil.CommandFlagsInit, goutil.CommandFunc)) fun f := generalFlags{} flagsInit := func(s *flag.FlagSet) { s.StringVar(&f.templateFile, "template", "", "use a template file to format tweets") + s.BoolVar(&f.stripHTML, "strip-html", true, "strip HTML tags from statuses") } return func() (goutil.CommandFlagsInit, goutil.CommandFunc) { formerInit, commandFunc := cmd() @@ -326,6 +335,7 @@ func wrapCommandFl(cmd func() (goutil.CommandFlagsInit, goutil.CommandFunc)) fun if f.templateFile != "" { formatTemplate = template.Must(template.New(filepath.Base(f.templateFile)).Funcs(template.FuncMap{"replaceAll": strings.ReplaceAll}).ParseFiles(f.templateFile)) } + stripHTML = f.stripHTML return commandFunc(args) } }