From c4520f2c84c0d4555bdb02f4ec7b2d1a8bdefca2 Mon Sep 17 00:00:00 2001 From: Brian Picciano Date: Mon, 23 Jan 2023 16:02:35 +0100 Subject: Automatically bridge gemini links to a gateway on http site --- src/gmi/gemtext.go | 27 +++++++++++++++++++++------ src/gmi/gemtext_test.go | 17 ++++++++++++++++- 2 files changed, 37 insertions(+), 7 deletions(-) (limited to 'src/gmi') diff --git a/src/gmi/gemtext.go b/src/gmi/gemtext.go index a1136bd..3e5f6fc 100644 --- a/src/gmi/gemtext.go +++ b/src/gmi/gemtext.go @@ -5,8 +5,9 @@ import ( "errors" "fmt" "io" - "log" + "net/url" "path" + "path/filepath" "regexp" "strings" ) @@ -25,7 +26,10 @@ 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. -func GemtextToMarkdown(dst io.Writer, src io.Reader) error { +// +// 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) @@ -40,7 +44,20 @@ func GemtextToMarkdown(dst io.Writer, src io.Reader) error { if match := linkRegexp.FindStringSubmatch(line); len(match) > 0 { - isImg := hasImgExt(match[1]) + 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 { + + newU := *gmiGateway + newU.Path = filepath.Join(newU.Path, u.Host, u.Path) + newU.RawQuery = u.RawQuery + u = &newU + } + + isImg := hasImgExt(u.Path) descr := match[2] @@ -52,9 +69,7 @@ func GemtextToMarkdown(dst io.Writer, src io.Reader) error { descr = "Link" } - log.Printf("descr:%q", descr) - - line = fmt.Sprintf("[%s](%s)\n", descr, match[1]) + line = fmt.Sprintf("[%s](%s)\n", descr, u.String()) if isImg { line = "!" + line diff --git a/src/gmi/gemtext_test.go b/src/gmi/gemtext_test.go index 23cb97f..75da9df 100644 --- a/src/gmi/gemtext_test.go +++ b/src/gmi/gemtext_test.go @@ -2,6 +2,7 @@ package gmi import ( "bytes" + "net/url" "strconv" "testing" @@ -10,6 +11,8 @@ import ( func TestGemtextToMarkdown(t *testing.T) { + gmiGateway, _ := url.Parse("https://gateway.com/x/") + tests := []struct { in, exp string }{ @@ -37,13 +40,25 @@ func TestGemtextToMarkdown(t *testing.T) { in: "=> img.png description is here ", exp: "![description is here](img.png)\n", }, + { + in: "=> gemini://somewhere.com/foo Somewhere", + exp: "[Somewhere](https://gateway.com/x/somewhere.com/foo)\n", + }, + { + in: "=> gemini://somewhere.com:420/foo Somewhere", + exp: "[Somewhere](https://gateway.com/x/somewhere.com:420/foo)\n", + }, + { + in: "=> gemini://somewhere.com:420/foo?bar=baz Somewhere", + exp: "[Somewhere](https://gateway.com/x/somewhere.com:420/foo?bar=baz)\n", + }, } for i, test := range tests { t.Run(strconv.Itoa(i), func(t *testing.T) { got := new(bytes.Buffer) - err := GemtextToMarkdown(got, bytes.NewBufferString(test.in)) + err := GemtextToMarkdown(got, bytes.NewBufferString(test.in), gmiGateway) assert.NoError(t, err) assert.Equal(t, test.exp, got.String()) }) -- cgit v1.2.3