From 0d904f9f10590d3a9117af27151f8c4fe2aea5ff Mon Sep 17 00:00:00 2001 From: tjpcc Date: Sat, 14 Jan 2023 09:57:16 -0700 Subject: Completed gemtext package. resolves #2 - fuzz testing - split out line parsing into a separate file - changed line type-specific public []byte fields to string accessor methods - added document parsing test for a stress test doc - added dependency on stretchr/testify --- gemtext/types.go | 65 +++++++++++++++++++++++++++++--------------------------- 1 file changed, 34 insertions(+), 31 deletions(-) (limited to 'gemtext/types.go') diff --git a/gemtext/types.go b/gemtext/types.go index fb9352a..fefbece 100644 --- a/gemtext/types.go +++ b/gemtext/types.go @@ -91,39 +91,39 @@ func (tl TextLine) Raw() []byte { return tl.raw } // LinkLine is a line of LineTypeLink. type LinkLine struct { raw []byte - - // URL is the original bytes of the url portion of the line. - // - // It is not guaranteed to be a valid URL. - URL []byte - - // Label is the label portion of the line. - // - // If there was no label it will always be nil, never []byte{}. - Label []byte + url []byte + label []byte } func (ll LinkLine) Type() LineType { return LineTypeLink } func (ll LinkLine) Raw() []byte { return ll.raw } +// URL returns the original url portion of the line. +// +// It is not guaranteed to be a valid URL. +func (ll LinkLine) URL() string { return string(ll.url) } + +// Label returns the label portion of the line. +func (ll LinkLine) Label() string { return string(ll.label) } + // PreformatToggleLine is a preformatted text toggle line. type PreformatToggleLine struct { raw []byte - - // AltText contains the alt-text portion of the line. - // - // It will either have len() > 0 or be nil. - // - // If the line was parsed as part of a full document by Parse(), - // and this is a *closing* toggle, any alt-text present will be - // stripped and this will be nil. If the line was parsed by - // ParseLine() no such correction is performed. - AltText []byte + altText []byte } func (tl PreformatToggleLine) Type() LineType { return LineTypePreformatToggle } func (tl PreformatToggleLine) Raw() []byte { return tl.raw } -func (tl *PreformatToggleLine) clearAlt() { tl.AltText = nil } + +// AltText returns the alt-text portion of the line. +// +// If the line was parsed as part of a full document by Parse(), +// and this is a *closing* toggle, any alt-text present will be +// stripped and this will be empty. If the line was parsed by +// ParseLine() no such correction is performed. +func (tl PreformatToggleLine) AltText() string { return string(tl.altText) } + +func (tl *PreformatToggleLine) clearAlt() { tl.altText = nil } // PreformattedTextLine represents a line between two toggles. // @@ -140,32 +140,35 @@ func (tl PreformattedTextLine) Raw() []byte { return tl.raw } type HeadingLine struct { raw []byte lineType LineType - - // Body is the portion of the line with the header text. - Body []byte + body []byte } func (hl HeadingLine) Type() LineType { return hl.lineType } func (hl HeadingLine) Raw() []byte { return hl.raw } +// Body returns the portion of the line with the header text. +func (hl HeadingLine) Body() string { return string(hl.body) } + // ListItemLine is a line of LineTypeListItem. type ListItemLine struct { raw []byte - - // Body is the text of the list item. - Body []byte + body []byte } func (li ListItemLine) Type() LineType { return LineTypeListItem } func (li ListItemLine) Raw() []byte { return li.raw } +// Body returns the text of the list item. +func (li ListItemLine) Body() string { return string(li.body) } + // QuoteLine is a line of LineTypeQuote. type QuoteLine struct { - raw []byte - - // Body is the text of the quote. - Body []byte + raw []byte + body []byte } func (ql QuoteLine) Type() LineType { return LineTypeQuote } func (ql QuoteLine) Raw() []byte { return ql.raw } + +// Body returns the text of the quote. +func (ql QuoteLine) Body() string { return string(ql.body) } -- cgit v1.2.3