01
2022-01-08
op
package main
10
2022-01-08
op
gemini "git.sr.ht/~adnano/go-gemini"
14
2022-01-08
op
ErrNoTitle = errors.New(`no title found`)
16
2022-01-08
op
title1 = regexp.MustCompile(`^#[^#]`)
17
2022-01-08
op
title2 = regexp.MustCompile(`^##[^#]`)
18
2022-01-08
op
title3 = regexp.MustCompile(`^###`)
21
2022-01-08
op
func geminiTitle(url string) (string, error) {
22
2022-01-08
op
client := gemini.Client{}
23
2022-01-08
op
ctx := context.Background()
24
2022-01-08
op
resp, err := client.Get(ctx, url)
25
2022-01-08
op
if err != nil {
26
2022-01-08
op
return "", err
28
2022-01-08
op
defer resp.Body.Close()
30
2022-01-08
op
if resp.Status != 20 || !strings.HasPrefix(resp.Meta, "text/gemini") {
31
2022-01-08
op
return "", ErrNoTitle
34
2022-01-08
op
var t2, t3 string
36
2022-01-08
op
scanner := bufio.NewScanner(resp.Body)
37
2022-01-08
op
for scanner.Scan() {
38
2022-01-08
op
line := scanner.Text()
41
2022-01-08
op
case title1.MatchString(line):
42
2022-01-08
op
return line, nil
43
2022-01-08
op
case title2.MatchString(line) && t2 == "":
45
2022-01-08
op
case title3.MatchString(line) && t3 == "":
50
2022-01-08
op
if t2 != "" {
51
2022-01-08
op
return t2, nil
54
2022-01-08
op
if t3 != "" {
55
2022-01-08
op
return t3, nil
58
2022-01-08
op
return "", ErrNoTitle