diff options
| author | Paul Buetow <paul@buetow.org> | 2024-11-27 17:05:43 +0200 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2024-11-27 17:05:43 +0200 |
| commit | 89d391bd37e27bc264e92911df12a78354be9a5f (patch) | |
| tree | de464115de03929b924342ffffe64f82522c1099 | |
| parent | 5266c37d3e70fa8f3bb683cd91ffbfeeaac8bc79 (diff) | |
more fixes
| -rw-r--r-- | internal/platforms/platform.go | 22 | ||||
| -rw-r--r-- | internal/queue/inlinetags.go | 11 | ||||
| -rw-r--r-- | internal/queue/inlinetags_test.go | 10 |
3 files changed, 37 insertions, 6 deletions
diff --git a/internal/platforms/platform.go b/internal/platforms/platform.go index 3eae4a8..e5bbd22 100644 --- a/internal/platforms/platform.go +++ b/internal/platforms/platform.go @@ -60,3 +60,25 @@ func (p Platform) Post(ctx context.Context, args config.Args, sizeLimit int, en fmt.Print("\n") return nil } + +func ExpandAliases(shareTag string) (string, error) { + a := make(map[string]struct{}, len(aliases)) + parts := strings.Split(shareTag, ":") + if parts[0] != "share" { + return "", fmt.Errorf("expected share tag, but got '%s' in '%s'", parts[0], shareTag) + } + + elems := []string{"share"} + // Dedup + for _, alias := range parts[1:] { + a[alias] = struct{}{} + } + for alias := range a { + platformStr, ok := aliases[alias] + if !ok { + return "", fmt.Errorf("invalid platform alias '%s' in '%s'", alias, shareTag) + } + elems = append(elems, platformStr) + } + return strings.Join(elems, ":"), nil +} diff --git a/internal/queue/inlinetags.go b/internal/queue/inlinetags.go index 7925151..82ee844 100644 --- a/internal/queue/inlinetags.go +++ b/internal/queue/inlinetags.go @@ -9,6 +9,7 @@ import ( "codeberg.org/snonux/gos/internal/colour" "codeberg.org/snonux/gos/internal/oi" + "codeberg.org/snonux/gos/internal/platforms" ) var inlineTagRE = regexp.MustCompile(`^[a-z\.,:]*$`) @@ -62,9 +63,17 @@ func extractInlineTagsFromContent(content string) ([]string, string, error) { tags = append(tags, strings.Split(elem, ",")...) } if len(tags) == 0 { - tags = []string{parts[0]} + tags = parts[:1] } if len(tags) > 0 { + for i := range len(tags) { + if strings.HasPrefix(tags[i], "share:") { + var err error + if tags[i], err = platforms.ExpandAliases(tags[i]); err != nil { + return []string{}, content, err + } + } + } return tags, strings.Join(parts[1:], " "), nil } } diff --git a/internal/queue/inlinetags_test.go b/internal/queue/inlinetags_test.go index ee07cc9..a0d71b6 100644 --- a/internal/queue/inlinetags_test.go +++ b/internal/queue/inlinetags_test.go @@ -14,9 +14,9 @@ func TestExtractInlineTagsToFilePath(t *testing.T) { "foo.bar.baz blablablabla...": "./gosdir/foo.golang.rox.foo.bar.baz.extracted.txt", "foo.bar,baz blablablabla...": "./gosdir/foo.golang.rox.foo.bar.baz.extracted.txt", "foo,bar.baz blablablabla...": "./gosdir/foo.golang.rox.foo.bar.baz.extracted.txt", - "share:li,foo this is the main content": "./gosdir/foo.golang.rox.share:li.foo.extracted.txt", - "share:li:ma this is the main content": "./gosdir/foo.golang.rox.share:li:ma.extracted.txt", - "share:li:ma,now this is the main content": "./gosdir/foo.golang.rox.share:li:ma.now.extracted.txt", + "share:li,foo this is the main content": "./gosdir/foo.golang.rox.share:linkedin.foo.extracted.txt", + "share:li:ma this is the main content": "./gosdir/foo.golang.rox.share:linkedin:mastodon.extracted.txt", + "share:li:ma,now this is the main content": "./gosdir/foo.golang.rox.share:linkedin:mastodon.now.extracted.txt", } for content, expectedFilePath := range table { @@ -38,8 +38,8 @@ func TestExtractInlineTagsFromContent(t *testing.T) { "foo.bar.baz blablablabla...": {"foo", "bar", "baz"}, "foo.bar,baz blablablabla...": {"foo", "bar", "baz"}, "foo,bar.baz blablablabla...": {"foo", "bar", "baz"}, - "share:li this is the main content": {"share:li"}, - "share:li,foo this is the main content": {"share:li", "foo"}, + "share:li this is the main content": {"share:linkedin"}, + "share:li,foo this is the main content": {"share:linkedin", "foo"}, "shar()e:li,foo this is the main content": {}, } |
