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