diff options
author | tjp <tjp@ctrl-c.club> | 2024-01-04 12:49:44 -0700 |
---|---|---|
committer | tjp <tjp@ctrl-c.club> | 2024-01-04 12:49:44 -0700 |
commit | 1352f915a62676959f4d7832e7763cbe2589b680 (patch) | |
tree | 49edb13355a9c8445901e107e4fcb26d2996b0b4 | |
parent | 69c5336ea11aa51e2b47526f7d6108ac42ddcef1 (diff) |
unify spartan prompt lines with gemini link lines
-rw-r--r-- | gemini/gemtext/parse_line.go | 41 | ||||
-rw-r--r-- | gemini/gemtext/types.go | 24 |
2 files changed, 7 insertions, 58 deletions
diff --git a/gemini/gemtext/parse_line.go b/gemini/gemtext/parse_line.go index faf457d..d2a81cc 100644 --- a/gemini/gemtext/parse_line.go +++ b/gemini/gemtext/parse_line.go @@ -14,10 +14,10 @@ func ParseLine(line []byte) Line { break } if line[1] == '>' { - return parseLinkLine(line) + return parseLinkLine(line, LineTypeLink) } if line[1] == ':' { - return parsePromptLine(line) + return parseLinkLine(line, LineTypePrompt) } case '`': if len(line) < 3 || line[1] != '`' || line[2] != '`' { @@ -45,8 +45,8 @@ func ParseLine(line []byte) Line { return TextLine{raw: line} } -func parseLinkLine(raw []byte) LinkLine { - line := LinkLine{raw: raw} +func parseLinkLine(raw []byte, typ LineType) LinkLine { + line := LinkLine{raw: raw, typ: typ} // move past =>[<whitespace>] raw = bytes.TrimLeft(raw[2:], " \t") @@ -78,39 +78,6 @@ func parseLinkLine(raw []byte) LinkLine { return line } -func parsePromptLine(raw []byte) PromptLine { - line := PromptLine{raw: raw} - - // move past =:[<whitespace>] - raw = bytes.TrimLeft(raw[2:], " \t") - - // find the next space or tab - spIdx := bytes.IndexByte(raw, ' ') - tbIdx := bytes.IndexByte(raw, '\t') - idx := spIdx - if idx == -1 { - idx = tbIdx - } - if tbIdx >= 0 && tbIdx < idx { - idx = tbIdx - } - - if idx < 0 { - line.url = bytes.TrimRight(raw, "\r\n") - return line - } - - line.url = raw[:idx] - raw = raw[idx+1:] - - label := bytes.TrimRight(bytes.TrimLeft(raw, " \t"), "\r\n") - if len(label) > 0 { - line.label = label - } - - return line -} - func parsePreformatToggleLine(raw []byte) PreformatToggleLine { line := PreformatToggleLine{raw: raw} diff --git a/gemini/gemtext/types.go b/gemini/gemtext/types.go index 3965b11..71f3242 100644 --- a/gemini/gemtext/types.go +++ b/gemini/gemtext/types.go @@ -20,7 +20,7 @@ const ( // // =:[<ws>]<url>[<ws><label>][\r]\n // - // The line is a PromptLine. + // The line is a LinkLine. LineTypePrompt // LineTypePreformatToggle switches the document between pre-formatted text or not. @@ -104,9 +104,10 @@ type LinkLine struct { raw []byte url []byte label []byte + typ LineType } -func (ll LinkLine) Type() LineType { return LineTypeLink } +func (ll LinkLine) Type() LineType { return ll.typ } func (ll LinkLine) Raw() []byte { return ll.raw } func (ll LinkLine) String() string { return string(ll.raw) } @@ -118,25 +119,6 @@ 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) } -// PromptLine is a Spartan =: prompt line. -type PromptLine struct { - raw []byte - url []byte - label []byte -} - -func (pl PromptLine) Type() LineType { return LineTypePrompt } -func (pl PromptLine) Raw() []byte { return pl.raw } -func (pl PromptLine) String() string { return string(pl.raw) } - -// URL returns the original url portion of the line. -// -// It is not guaranteed to be a valid URL. -func (pl PromptLine) URL() string { return string(pl.url) } - -// Label retrns the label portion of the line. -func (pl PromptLine) Label() string { return string(pl.label) } - // PreformatToggleLine is a preformatted text toggle line. type PreformatToggleLine struct { raw []byte |