Browse Source

fix: enhance subject parsing (#657)

Co-authored-by: Jon Lundy <jon@xuu.cc>
Co-authored-by: James Mills <prologic@shortcircuit.net.au>
Reviewed-on: #657
Reviewed-by: James Mills <james@mills.io>
Co-authored-by: xuu <xuu@noreply@mills.io>
Co-committed-by: xuu <xuu@noreply@mills.io>
pull/659/head
xuu 2 weeks ago
committed by James Mills
parent
commit
54e6ace5b8
  1. 21
      types/lextwt/lextwt_test.go
  2. 9
      types/lextwt/parser.go

21
types/lextwt/lextwt_test.go

@ -641,9 +641,7 @@ func TestParseTwt(t *testing.T) {
twt: lextwt.NewTwt(
twter,
lextwt.NewDateTime(parseTime("2021-02-04T12:54:21Z"), "2021-02-04T12:54:21Z"),
lextwt.NewText("a twt witn "),
lextwt.NewSubject("not a"),
lextwt.NewText(" subject"),
lextwt.NewText("a twt witn (not a) subject"),
),
},
@ -722,7 +720,22 @@ func TestParseTwt(t *testing.T) {
lextwt.NewMedia("alt", "https://example.com/image.png", `"a title"`),
),
},
// "👋 Hey @<%s %s/twtxt.txt>, a new user (@<%s %s/twtxt.txt>) has joined your pod %s! 🥳"
{
lit: `2021-11-05T22:00:00+01:00 👋 Hey @<foo http://example.com/twtxt.txt>, a new user (@<bar http://example.com/twtxt.txt>) has joined your pod binbaz! 🥳`,
twt: lextwt.NewTwt(
twter,
lextwt.NewDateTime(parseTime("2021-11-05T22:00:00+01:00"), "2021-11-05T22:00:00+01:00"),
lextwt.NewText("👋 Hey "),
lextwt.NewMention("foo", "http://example.com/twtxt.txt"),
lextwt.NewText(", a new user ("),
lextwt.NewMention("bar", "http://example.com/twtxt.txt"),
lextwt.NewText(") has joined your pod binbaz! 🥳"),
),
},
}
fmtOpts := mockFmtOpts{"http://example.org"}
for i, tt := range tests {
t.Run(fmt.Sprintf("TestParseTwt %d", i), func(t *testing.T) {
@ -738,6 +751,8 @@ func TestParseTwt(t *testing.T) {
assert.True(twt != nil)
if twt != nil {
assert.Equal(fmt.Sprintf("%L", twt), fmt.Sprintf("%L", tt.twt))
testParseTwt(t, tt.twt, twt)
}
if tt.text != "" {

9
types/lextwt/parser.go

@ -366,11 +366,12 @@ func (p *parser) ParseElem() Elem {
e = p.parseSubjectOrText()
p.skipSubject = true
case TokHASH:
p.skipSubject = true
e = p.ParseTag()
p.skipSubject = true
case TokAT:
e = p.ParseMention()
case TokNL, TokEOF:
p.skipSubject = true
return nil
default:
if p.curTokenIs(TokSTRING) && p.peekTokenIs(TokSCHEME) {
@ -378,12 +379,18 @@ func (p *parser) ParseElem() Elem {
p.skipSubject = true
} else {
e = p.ParseText()
if text, ok := e.(*Text); ok && !text.IsSpace() {
p.skipSubject = true
}
}
}
// If parsing above failed convert to Text
if e == nil || e.IsNil() {
e = p.ParseText()
if text, ok := e.(*Text); ok && !text.IsSpace() {
p.skipSubject = true
}
}
return e

Loading…
Cancel
Save