summaryrefslogtreecommitdiff
path: root/src/gmi/gemtext.go
blob: 884635c2b3c705d38de49ec5708614ad4279c684 (plain)
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
package gmi

import (
	"bufio"
	"errors"
	"fmt"
	"io"
	"net/url"
	"path"
	"regexp"
	"strings"
)

func hasImgExt(p string) bool {
	switch path.Ext(strings.ToLower(p)) {
	case ".jpg", ".jpeg", ".png", ".gif", ".svg":
		return true
	default:
		return false
	}
}

// matches `=> dstURL [optional description]`
var linkRegexp = regexp.MustCompile(`^=>\s+(\S+)\s*(.*?)\s*$`)

// GemtextToMarkdown reads a gemtext formatted body from the Reader and writes
// the markdown version of that body to the Writer.
//
// gmiGateway, if given, is used for all `gemini://` links. The `gemini://`
// prefix will be stripped, and replaced with the given URL.
func GemtextToMarkdown(dst io.Writer, src io.Reader, gmiGateway *url.URL) error {

	bufSrc := bufio.NewReader(src)

	for {

		line, err := bufSrc.ReadString('\n')
		if err != nil && !errors.Is(err, io.EOF) {
			return fmt.Errorf("reading: %w", err)
		}

		last := err == io.EOF

		if match := linkRegexp.FindStringSubmatch(line); len(match) > 0 {

			u, err := url.Parse(match[1])
			if err != nil {
				return fmt.Errorf("link to invalid url %q: %w", match[1], err)
			}

			if u.Scheme == "gemini" && gmiGateway != nil {
				newUStr := gmiGateway.String() + u.Host + u.Path
				if u, err = url.Parse(newUStr); err != nil {
					return fmt.Errorf("parsing proxied URL %q: %w", newUStr, err)
				}
			}

			isImg := hasImgExt(u.Path)

			descr := match[2]

			if descr != "" {
				// ok
			} else if isImg {
				descr = "Image"
			} else {
				descr = "Link"
			}

			line = fmt.Sprintf("[%s](%s)\n", descr, u.String())

			if isImg {
				line = "!" + line
			}
		}

		if _, err := dst.Write([]byte(line)); err != nil {
			return fmt.Errorf("writing: %w", err)
		}

		if last {
			return nil
		}
	}

}