fix: content missing when displaying Status

This commit fixes the issue where the contents of a status is not shown
when displayed on screen. For statuses made in GTS the Text field is
populated with the plain text version of the content however this is
unfortunately not the case for Mastodon statuses, therefore the Content
field is used instead and the contents is processed with StripHTMLTags
function to remove the HTML tags before printing.
This commit is contained in:
Dan Anglin 2024-05-26 15:07:34 +01:00
parent 7a3f2928de
commit f3d9d34f3c
Signed by: dananglin
GPG key ID: 0C1D44CFBEE68638
3 changed files with 19 additions and 12 deletions

View file

@ -158,7 +158,6 @@ func (s Status) String() string {
%s
%s
%s
%s
@ -179,10 +178,9 @@ func (s Status) String() string {
return fmt.Sprintf(
format,
utilities.DisplayNameFormat(s.Account.DisplayName),
s.Account.Username,
utilities.DisplayNameFormat(s.Account.DisplayName), s.Account.Username,
utilities.HeaderFormat("CONTENT:"),
utilities.WrapLines(s.Text, "\n ", 80),
utilities.WrapLines(utilities.StripHTMLTags(s.Content), "\n ", 80),
utilities.HeaderFormat("STATUS ID:"),
s.ID,
utilities.HeaderFormat("CREATED AT:"),

View file

@ -16,11 +16,11 @@ func (t Timeline) String() string {
separator := "────────────────────────────────────────────────────────────────────────────────"
builder.WriteString(t.Name + "\n" + separator + "\n")
builder.WriteString(utilities.HeaderFormat(t.Name) + "\n\n")
for _, status := range t.Statuses {
builder.WriteString(utilities.DisplayNameFormat(status.Account.DisplayName) + " (@" + status.Account.Username + ")\n\n")
builder.WriteString(utilities.WrapLines(status.Text, "\n", 80) + "\n\n")
builder.WriteString(utilities.DisplayNameFormat(status.Account.DisplayName) + " (@" + status.Account.Username + ")\n")
builder.WriteString(utilities.WrapLines(utilities.StripHTMLTags(status.Content), "\n", 80) + "\n\n")
builder.WriteString(utilities.FieldFormat("ID:") + " " + status.ID + "\t" + utilities.FieldFormat("Created at:") + " " + utilities.FormatTime(status.CreatedAt) + "\n")
builder.WriteString(separator + "\n")
}

View file

@ -43,17 +43,26 @@ func StripHTMLTags(text string) string {
case html.ErrorToken:
return builder.String()
case html.TextToken:
text := token.Token().String()
text := token.Token().Data
builder.WriteString(text)
case html.StartTagToken:
case html.StartTagToken, html.EndTagToken:
tag := token.Token().String()
if tag == "<br>" {
builder.WriteString("\n")
}
builder.WriteString(transformTag(tag))
}
}
}
func transformTag(tag string) string {
switch tag {
case "<br>":
return "\n"
case "<p>", "</p>":
return "\n"
}
return ""
}
func WrapLines(text, separator string, charLimit int) string {
lines := strings.Split(text, "\n")