~adnano/go-gemini

ref: v0.2.2 go-gemini/text.go -rw-r--r-- 4.1 KiB
3d2110d9Adnan Maolood mux: Tweak documentation 6 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package gemini

import (
	"bufio"
	"fmt"
	"io"
	"strings"
)

// Line represents a line of a Gemini text response.
type Line interface {
	// String formats the line for use in a Gemini text response.
	String() string
	line() // private function to prevent other packages from implementing Line
}

// LineLink is a link line.
type LineLink struct {
	URL  string
	Name string
}

// LinePreformattingToggle is a preformatting toggle line.
type LinePreformattingToggle string

// LinePreformattedText is a preformatted text line.
type LinePreformattedText string

// LineHeading1 is a first-level heading line.
type LineHeading1 string

// LineHeading2 is a second-level heading line.
type LineHeading2 string

// LineHeading3 is a third-level heading line.
type LineHeading3 string

// LineListItem is an unordered list item line.
type LineListItem string

// LineQuote is a quote line.
type LineQuote string

// LineText is a text line.
type LineText string

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)
}

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()                {}

// Text represents a Gemini text response.
type Text []Line

// ParseText parses Gemini text from the provided io.Reader.
func ParseText(r io.Reader) (Text, error) {
	var t Text
	err := ParseLines(r, func(line Line) {
		t = append(t, line)
	})
	return t, err
}

// ParseLines parses Gemini text from the provided io.Reader.
// It calls handler with each line that it parses.
func ParseLines(r io.Reader, handler func(Line)) error {
	const spacetab = " \t"
	var pre bool
	scanner := bufio.NewScanner(r)
	for scanner.Scan() {
		var line Line
		text := scanner.Text()
		if strings.HasPrefix(text, "```") {
			pre = !pre
			text = text[3:]
			line = LinePreformattingToggle(text)
		} else if pre {
			line = LinePreformattedText(text)
		} else if strings.HasPrefix(text, "=>") {
			text = text[2:]
			text = strings.TrimLeft(text, spacetab)
			split := strings.IndexAny(text, spacetab)
			if split == -1 {
				// text is a URL
				line = LineLink{URL: text}
			} else {
				url := text[:split]
				name := text[split:]
				name = strings.TrimLeft(name, spacetab)
				line = LineLink{url, name}
			}
		} 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)
		} else {
			line = LineText(text)
		}
		handler(line)
	}

	return scanner.Err()
}

// String writes the Gemini text response to a string and returns it.
func (t Text) String() string {
	var b strings.Builder
	for _, l := range t {
		b.WriteString(l.String())
		b.WriteByte('\n')
	}
	return b.String()
}