summaryrefslogtreecommitdiff
path: root/internal/summary
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2025-01-19 13:17:50 +0200
committerPaul Buetow <paul@buetow.org>2025-01-19 13:17:50 +0200
commitc77e07805c98a7a53bc62deeb21acf6f01195ca9 (patch)
tree9a03b12ae6c3e25b553ea14b432594f63a19072b /internal/summary
parentc0a710cca29fc2c2ed641d8c364ef971f934d61b (diff)
change the gemini prefix procedure
Diffstat (limited to 'internal/summary')
-rw-r--r--internal/summary/summary.go48
-rw-r--r--internal/summary/summary_test.go15
2 files changed, 43 insertions, 20 deletions
diff --git a/internal/summary/summary.go b/internal/summary/summary.go
index 16e1db6..4ace908 100644
--- a/internal/summary/summary.go
+++ b/internal/summary/summary.go
@@ -8,6 +8,7 @@ import (
"regexp"
"sort"
"strings"
+ "testing"
"codeberg.org/snonux/gos/internal/config"
"codeberg.org/snonux/gos/internal/entry"
@@ -72,7 +73,7 @@ func generateGemtext(args config.Args, entries []entry.Entry, title string) (str
sb.WriteString("\n")
for _, url := range urls {
sb.WriteString("\n")
- sb.WriteString(gemtextLink(args.GeminiCapsule, url, 30))
+ sb.WriteString(gemtextLink(args.GeminiCapsules, url, 30))
}
}
}
@@ -149,23 +150,44 @@ func prepare(content string) string {
return content
}
-func gemtextLink(geminiCapsule, url string, maxLen int) string {
+var T *testing.T
+
+func gemtextLink(geminiCapsules []string, url string, maxLen int) string {
url = strings.TrimSpace(url)
- urlNoProto := regexp.MustCompile(`^[a-zA-Z]+://`).ReplaceAllString(url, "")
+ var (
+ urlNoProto = regexp.MustCompile(`^[a-zA-Z]+://`).ReplaceAllString(url, "")
+ links []string
+ )
- // Is tttttis an internal link? If so, replace PROTO:// with gemini://
- if strings.HasPrefix(urlNoProto, geminiCapsule) &&
- (strings.HasPrefix(url, "http://") || strings.HasPrefix(url, "https://")) {
- urlNoProto = strings.ReplaceAll(urlNoProto, ".html", ".gmi")
- url = "gemini://" + urlNoProto
+ // Check whether any element of the slice starts with prefix.
+ hasPrefix := func(str string, prefixes []string) bool {
+ for _, element := range prefixes {
+ if strings.HasPrefix(str, element) {
+ return true
+ }
+ }
+ return false
}
- if len(urlNoProto) <= maxLen {
- return "=> " + url + " " + urlNoProto
+ // Shorten the link description if too long.
+ shorten := func(url, urlNoProto string, maxLen int) string {
+ if len(urlNoProto) <= maxLen {
+ return "=> " + url + " " + urlNoProto
+ }
+ halfLen := (maxLen - 3) / 2
+ shortened := urlNoProto[:halfLen] + "..." + urlNoProto[len(urlNoProto)-halfLen:]
+ return "=> " + url + " " + shortened
+ }
+
+ // Is this a Gemini link? If so, add it to the link list.
+ if hasPrefix(urlNoProto, geminiCapsules) && hasPrefix(url, []string{"http://", "https://"}) {
+ urlNoProto := strings.ReplaceAll(urlNoProto, ".html", ".gmi")
+ url := "gemini://" + urlNoProto
+ links = append(links, shorten(url, urlNoProto, maxLen)+" (Gemini)")
}
- halfLen := (maxLen - 3) / 2
- shorten := urlNoProto[:halfLen] + "..." + urlNoProto[len(urlNoProto)-halfLen:]
- return "=> " + url + " " + shorten
+
+ links = append(links, shorten(url, urlNoProto, maxLen))
+ return strings.Join(links, "\n")
}
func firstFewWords(content string, maxLen int) string {
diff --git a/internal/summary/summary_test.go b/internal/summary/summary_test.go
index fffc896..4846878 100644
--- a/internal/summary/summary_test.go
+++ b/internal/summary/summary_test.go
@@ -3,22 +3,23 @@ package summary
import "testing"
func TestGemtextLink(t *testing.T) {
- const geminiCapsule = "foo.zone"
+ T = t
+ geminiCapsules := []string{"foo.zone"}
table := map[string]string{
"http://example.com": "=> http://example.com example.com",
"https://example.org": "=> https://example.org example.org",
"https://example.org/some/very/long/link/here?with=a&free=of&parameters=here": "=> https://example.org/some/very/long/link/here?with=a&free=of&parameters=here example.org/s...rameters=here",
- // TODO: Refactor, so that internal links refer to ./ and that the suffix .gmi and/or .html is hidden if possible
- "beer://foo.zone": "=> beer://foo.zone foo.zone",
- "http://foo.zone": "=> one foo.zone",
- "https://foo.zone/index.html": "=> gemini://foo.zone/index.gmi foo.zone/index.gmi",
- "https://foo.zone/gemtext/this-is-awesome.html": "=> gemini://foo.zone/gemtext/this-is-awesome.gmi foo.zone/gemt...s-awesome.gmi",
+ "beer://foo.zone": "=> beer://foo.zone foo.zone",
+ "http://foo.zone": "=> gemini://foo.zone foo.zone (Gemini)\n=> http://foo.zone foo.zone",
+ "https://foo.zone/index.html": "=> gemini://foo.zone/index.gmi foo.zone/index.gmi (Gemini)\n=> https://foo.zone/index.html foo.zone/index.html",
+
+ "https://foo.zone/gemtext/this-is-awesome.html": "=> gemini://foo.zone/gemtext/this-is-awesome.gmi foo.zone/gemt...s-awesome.gmi (Gemini)\n=> https://foo.zone/gemtext/this-is-awesome.html foo.zone/gemt...-awesome.html",
}
for url, expected := range table {
- if result := gemtextLink(geminiCapsule, url, 30); result != expected {
+ if result := gemtextLink(geminiCapsules, url, 30); result != expected {
t.Errorf("Expected '%s' but got '%s' with input '%s'", expected, result, url)
}
}