From 70fc1acc68d5f6cb156436340c5b528a0e8c6e57 Mon Sep 17 00:00:00 2001 From: Dan Anglin Date: Thu, 22 Feb 2024 14:57:17 +0000 Subject: [PATCH] fix: strip HTML tags when displaying account info --- cmd/enbas/show.go | 22 ++++++++++++++++++++-- go.mod | 6 ++++-- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/cmd/enbas/show.go b/cmd/enbas/show.go index 87c6904..8de4e6b 100644 --- a/cmd/enbas/show.go +++ b/cmd/enbas/show.go @@ -4,9 +4,11 @@ import ( "errors" "flag" "fmt" + "strings" "codeflow.dananglin.me.uk/apollo/enbas/internal/client" "codeflow.dananglin.me.uk/apollo/enbas/internal/config" + "golang.org/x/net/html" ) var instanceDetailsFormat = `INSTANCE: @@ -134,7 +136,7 @@ func (c *showCommand) showAccount(gts *client.Client) error { metadata := "" for _, field := range account.Fields { - metadata += fmt.Sprintf("\n %s: %s", field.Name, field.Value) + metadata += fmt.Sprintf("\n %s: %s", field.Name, stripHTMLTags(field.Value)) } fmt.Printf( @@ -146,10 +148,26 @@ func (c *showCommand) showAccount(gts *client.Client) error { account.FollowersCount, account.FollowingCount, account.StatusCount, - account.Note, + stripHTMLTags(account.Note), metadata, account.URL, ) return nil } + +func stripHTMLTags(text string) string { + token := html.NewTokenizer(strings.NewReader(text)) + + var builder strings.Builder + + for { + tt := token.Next() + switch tt { + case html.ErrorToken: + return builder.String() + case html.TextToken: + builder.WriteString(token.Token().Data + " ") + } + } +} diff --git a/go.mod b/go.mod index 6b09132..c2568ff 100644 --- a/go.mod +++ b/go.mod @@ -2,11 +2,13 @@ module codeflow.dananglin.me.uk/apollo/enbas go 1.22.0 -require golang.org/x/oauth2 v0.17.0 +require ( + golang.org/x/net v0.21.0 + golang.org/x/oauth2 v0.17.0 +) require ( github.com/golang/protobuf v1.5.3 // indirect - golang.org/x/net v0.21.0 // indirect google.golang.org/appengine v1.6.7 // indirect google.golang.org/protobuf v1.31.0 // indirect )