2020-10-24 21:15:32 +02:00
|
|
|
package gemini
|
2020-09-29 16:13:57 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Line represents a line of a Gemini text response.
|
|
|
|
type Line interface {
|
2021-02-23 15:18:34 +01:00
|
|
|
// String formats the line for use in a Gemini text response.
|
2020-09-29 16:13:57 +02:00
|
|
|
String() string
|
2020-10-14 02:33:38 +02:00
|
|
|
line() // private function to prevent other packages from implementing Line
|
2020-09-29 16:13:57 +02:00
|
|
|
}
|
|
|
|
|
2021-02-23 15:18:34 +01:00
|
|
|
// LineLink is a link line.
|
2020-09-29 16:13:57 +02:00
|
|
|
type LineLink struct {
|
|
|
|
URL string
|
|
|
|
Name string
|
|
|
|
}
|
2020-10-14 02:33:38 +02:00
|
|
|
|
2021-02-23 15:18:34 +01:00
|
|
|
// LinePreformattingToggle is a preformatting toggle line.
|
2020-10-14 02:33:38 +02:00
|
|
|
type LinePreformattingToggle string
|
|
|
|
|
2021-02-23 15:18:34 +01:00
|
|
|
// LinePreformattedText is a preformatted text line.
|
2020-10-14 02:33:38 +02:00
|
|
|
type LinePreformattedText string
|
|
|
|
|
2021-02-23 15:18:34 +01:00
|
|
|
// LineHeading1 is a first-level heading line.
|
2020-10-14 02:33:38 +02:00
|
|
|
type LineHeading1 string
|
|
|
|
|
2021-02-23 15:18:34 +01:00
|
|
|
// LineHeading2 is a second-level heading line.
|
2020-10-14 02:33:38 +02:00
|
|
|
type LineHeading2 string
|
|
|
|
|
2021-02-23 15:18:34 +01:00
|
|
|
// LineHeading3 is a third-level heading line.
|
2020-10-14 02:33:38 +02:00
|
|
|
type LineHeading3 string
|
|
|
|
|
2021-02-23 15:18:34 +01:00
|
|
|
// LineListItem is an unordered list item line.
|
2020-10-14 02:33:38 +02:00
|
|
|
type LineListItem string
|
|
|
|
|
2021-02-23 15:18:34 +01:00
|
|
|
// LineQuote is a quote line.
|
2020-10-14 02:33:38 +02:00
|
|
|
type LineQuote string
|
|
|
|
|
2021-02-23 15:18:34 +01:00
|
|
|
// LineText is a text line.
|
2020-10-14 02:33:38 +02:00
|
|
|
type LineText string
|
2020-09-29 16:13:57 +02:00
|
|
|
|
|
|
|
func (l LineLink) String() string {
|
|
|
|
if l.Name != "" {
|
|
|
|
return fmt.Sprintf("=> %s %s", l.URL, l.Name)
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("=> %s", l.URL)
|
|
|
|
}
|
|
|
|
func (l LinePreformattingToggle) String() string {
|
|
|
|
return fmt.Sprintf("```%s", string(l))
|
|
|
|
}
|
|
|
|
func (l LinePreformattedText) String() string {
|
|
|
|
return string(l)
|
|
|
|
}
|
|
|
|
func (l LineHeading1) String() string {
|
|
|
|
return fmt.Sprintf("# %s", string(l))
|
|
|
|
}
|
|
|
|
func (l LineHeading2) String() string {
|
|
|
|
return fmt.Sprintf("## %s", string(l))
|
|
|
|
}
|
|
|
|
func (l LineHeading3) String() string {
|
|
|
|
return fmt.Sprintf("### %s", string(l))
|
|
|
|
}
|
|
|
|
func (l LineListItem) String() string {
|
|
|
|
return fmt.Sprintf("* %s", string(l))
|
|
|
|
}
|
|
|
|
func (l LineQuote) String() string {
|
|
|
|
return fmt.Sprintf("> %s", string(l))
|
|
|
|
}
|
|
|
|
func (l LineText) String() string {
|
|
|
|
return string(l)
|
|
|
|
}
|
|
|
|
|
2020-10-14 02:33:38 +02:00
|
|
|
func (l LineLink) line() {}
|
|
|
|
func (l LinePreformattingToggle) line() {}
|
|
|
|
func (l LinePreformattedText) line() {}
|
|
|
|
func (l LineHeading1) line() {}
|
|
|
|
func (l LineHeading2) line() {}
|
|
|
|
func (l LineHeading3) line() {}
|
|
|
|
func (l LineListItem) line() {}
|
|
|
|
func (l LineQuote) line() {}
|
|
|
|
func (l LineText) line() {}
|
|
|
|
|
2020-09-29 16:13:57 +02:00
|
|
|
// Text represents a Gemini text response.
|
|
|
|
type Text []Line
|
|
|
|
|
2020-10-29 14:42:53 +01:00
|
|
|
// ParseText parses Gemini text from the provided io.Reader.
|
2021-01-07 23:08:50 +01:00
|
|
|
func ParseText(r io.Reader) (Text, error) {
|
2020-09-29 16:13:57 +02:00
|
|
|
var t Text
|
2021-01-07 23:08:50 +01:00
|
|
|
err := ParseLines(r, func(line Line) {
|
2020-10-29 14:42:53 +01:00
|
|
|
t = append(t, line)
|
|
|
|
})
|
2021-01-07 23:08:50 +01:00
|
|
|
return t, err
|
2020-10-29 14:42:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// ParseLines parses Gemini text from the provided io.Reader.
|
|
|
|
// It calls handler with each line that it parses.
|
2021-01-07 23:08:50 +01:00
|
|
|
func ParseLines(r io.Reader, handler func(Line)) error {
|
2020-10-29 14:42:53 +01:00
|
|
|
const spacetab = " \t"
|
2020-09-29 16:13:57 +02:00
|
|
|
var pre bool
|
|
|
|
scanner := bufio.NewScanner(r)
|
|
|
|
for scanner.Scan() {
|
2020-10-29 14:42:53 +01:00
|
|
|
var line Line
|
|
|
|
text := scanner.Text()
|
|
|
|
if strings.HasPrefix(text, "```") {
|
2020-09-29 16:13:57 +02:00
|
|
|
pre = !pre
|
2020-10-29 14:42:53 +01:00
|
|
|
text = text[3:]
|
|
|
|
line = LinePreformattingToggle(text)
|
2020-09-29 16:13:57 +02:00
|
|
|
} else if pre {
|
2020-10-29 14:42:53 +01:00
|
|
|
line = LinePreformattedText(text)
|
|
|
|
} else if strings.HasPrefix(text, "=>") {
|
|
|
|
text = text[2:]
|
|
|
|
text = strings.TrimLeft(text, spacetab)
|
|
|
|
split := strings.IndexAny(text, spacetab)
|
2020-09-30 03:27:16 +02:00
|
|
|
if split == -1 {
|
2020-10-29 14:42:53 +01:00
|
|
|
// text is a URL
|
|
|
|
line = LineLink{URL: text}
|
2020-09-30 03:27:16 +02:00
|
|
|
} else {
|
2020-10-29 14:42:53 +01:00
|
|
|
url := text[:split]
|
|
|
|
name := text[split:]
|
2020-09-30 03:27:16 +02:00
|
|
|
name = strings.TrimLeft(name, spacetab)
|
2020-10-29 14:42:53 +01:00
|
|
|
line = LineLink{url, name}
|
2020-09-30 03:27:16 +02:00
|
|
|
}
|
2020-10-29 14:42:53 +01:00
|
|
|
} else if strings.HasPrefix(text, "*") {
|
|
|
|
text = text[1:]
|
|
|
|
text = strings.TrimLeft(text, spacetab)
|
|
|
|
line = LineListItem(text)
|
|
|
|
} else if strings.HasPrefix(text, "###") {
|
|
|
|
text = text[3:]
|
|
|
|
text = strings.TrimLeft(text, spacetab)
|
|
|
|
line = LineHeading3(text)
|
|
|
|
} else if strings.HasPrefix(text, "##") {
|
|
|
|
text = text[2:]
|
|
|
|
text = strings.TrimLeft(text, spacetab)
|
|
|
|
line = LineHeading2(text)
|
|
|
|
} else if strings.HasPrefix(text, "#") {
|
|
|
|
text = text[1:]
|
|
|
|
text = strings.TrimLeft(text, spacetab)
|
|
|
|
line = LineHeading1(text)
|
|
|
|
} else if strings.HasPrefix(text, ">") {
|
|
|
|
text = text[1:]
|
|
|
|
text = strings.TrimLeft(text, spacetab)
|
|
|
|
line = LineQuote(text)
|
2020-09-29 16:13:57 +02:00
|
|
|
} else {
|
2020-10-29 14:42:53 +01:00
|
|
|
line = LineText(text)
|
2020-09-29 16:13:57 +02:00
|
|
|
}
|
2020-10-29 14:42:53 +01:00
|
|
|
handler(line)
|
2020-09-29 16:13:57 +02:00
|
|
|
}
|
2021-01-07 23:08:50 +01:00
|
|
|
|
|
|
|
return scanner.Err()
|
2020-09-29 16:13:57 +02:00
|
|
|
}
|
|
|
|
|
2020-10-12 01:41:44 +02:00
|
|
|
// String writes the Gemini text response to a string and returns it.
|
2020-09-29 16:13:57 +02:00
|
|
|
func (t Text) String() string {
|
|
|
|
var b strings.Builder
|
|
|
|
for _, l := range t {
|
|
|
|
b.WriteString(l.String())
|
2020-09-29 16:16:55 +02:00
|
|
|
b.WriteByte('\n')
|
2020-09-29 16:13:57 +02:00
|
|
|
}
|
|
|
|
return b.String()
|
|
|
|
}
|