summaryrefslogtreecommitdiff
path: root/internal
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
parentc0a710cca29fc2c2ed641d8c364ef971f934d61b (diff)
change the gemini prefix procedure
Diffstat (limited to 'internal')
-rw-r--r--internal/config/args.go2
-rw-r--r--internal/main.go4
-rw-r--r--internal/summary/summary.go48
-rw-r--r--internal/summary/summary_test.go15
4 files changed, 46 insertions, 23 deletions
diff --git a/internal/config/args.go b/internal/config/args.go
index 77711b6..cdaed8a 100644
--- a/internal/config/args.go
+++ b/internal/config/args.go
@@ -23,7 +23,7 @@ type Args struct {
OAuth2Browser string
GeminiSummaryFor []string
GemtexterEnable bool
- GeminiCapsule string
+ GeminiCapsules []string
ComposeEntry bool
}
diff --git a/internal/main.go b/internal/main.go
index b9f4b3e..36fc870 100644
--- a/internal/main.go
+++ b/internal/main.go
@@ -28,7 +28,7 @@ func Main(composeEntryDefault bool) {
pauseDays := flag.Int("pauseDays", 3, "How many days until next post can be posted?")
lookback := flag.Int("lookback", 30, "How many days look back in time for posting history")
geminiSummaryFor := flag.String("geminiSummaryFor", "", "Generate a summary in Gemini Gemtext format, format is coma separated string of months, e.g. 202410,202411")
- geminiCapsule := flag.String("geminiCapsule", "foo.zone", "Address of the Gemini capsule. Used by geminiEnable to detect internal links")
+ geminiCapsules := flag.String("geminiCapsules", "foo.zone", "Comma sepaeated list Gemini capsules. Used by geminiEnable to detect Gemtext links")
gemtexterEnable := flag.Bool("gemtexterEnable", false, "Add special Gemtexter (the static site generator) tags to the Gemini Gemtext summary")
flag.Parse()
@@ -50,7 +50,7 @@ func Main(composeEntryDefault bool) {
Secrets: secrets,
OAuth2Browser: *browser,
GemtexterEnable: *gemtexterEnable,
- GeminiCapsule: *geminiCapsule,
+ GeminiCapsules: strings.Split(*geminiCapsules, ","),
ComposeEntry: *composeEntry,
}
if *geminiSummaryFor != "" {
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)
}
}