diff options
| author | Paul Buetow <paul@buetow.org> | 2024-11-27 21:21:29 +0200 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2024-11-27 21:21:29 +0200 |
| commit | f06a04e0f003c556c9273b2c4660b201d179598e (patch) | |
| tree | bd68daf994f29ab40304aa0661dc9d378170b1f1 /internal/queue | |
| parent | c07d0ae41c7a90628550bcbb79ff684f4dfc5a33 (diff) | |
refactoring tags
Diffstat (limited to 'internal/queue')
| -rw-r--r-- | internal/queue/inlinetags.go | 81 | ||||
| -rw-r--r-- | internal/queue/inlinetags_test.go | 75 | ||||
| -rw-r--r-- | internal/queue/queue.go | 8 |
3 files changed, 5 insertions, 159 deletions
diff --git a/internal/queue/inlinetags.go b/internal/queue/inlinetags.go deleted file mode 100644 index 82ee844..0000000 --- a/internal/queue/inlinetags.go +++ /dev/null @@ -1,81 +0,0 @@ -package queue - -import ( - "fmt" - "os" - "path/filepath" - "regexp" - "strings" - - "codeberg.org/snonux/gos/internal/colour" - "codeberg.org/snonux/gos/internal/oi" - "codeberg.org/snonux/gos/internal/platforms" -) - -var inlineTagRE = regexp.MustCompile(`^[a-z\.,:]*$`) - -// Extracts the inline tags into the filepath and removes them from the content. -func extractInlineTags(filePath string) (string, error) { - content, err := oi.SlurpAndTrim(filePath) - if err != nil { - return "", err - } - - newFilePath, newContent, err := extractInlineTagsToFilePath(filePath, content) - if err != nil { - return "", err - } - if newFilePath == filePath { - return filePath, nil - } - - colour.Infof("Rewriting path '%s' to '%s' (inline tag extraction)", filePath, newFilePath) - fmt.Print("\n") - if err := oi.WriteFile(newFilePath, newContent); err != nil { - return "", err - } - return newFilePath, os.Remove(filePath) -} - -func extractInlineTagsToFilePath(filePath, content string) (string, string, error) { - tags, newContent, err := extractInlineTagsFromContent(content) - if err != nil { - return filePath, content, err - } - if len(tags) == 0 { - return filePath, content, nil - } - - parts := strings.Split(strings.TrimSuffix(filePath, filepath.Ext(filePath)), ".") - parts = append(parts, tags...) - parts = append(parts, "extracted") - parts = append(parts, "txt") - - newFilePath := strings.Join(parts, ".") - return newFilePath, newContent, nil -} - -func extractInlineTagsFromContent(content string) ([]string, string, error) { - parts := strings.Split(content, " ") - if inlineTagRE.MatchString(parts[0]) { - var tags []string - for _, elem := range strings.Split(parts[0], ".") { - tags = append(tags, strings.Split(elem, ",")...) - } - if len(tags) == 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 - } - } - return []string{}, content, nil -} diff --git a/internal/queue/inlinetags_test.go b/internal/queue/inlinetags_test.go deleted file mode 100644 index a0d71b6..0000000 --- a/internal/queue/inlinetags_test.go +++ /dev/null @@ -1,75 +0,0 @@ -package queue - -import ( - "slices" - "strings" - "testing" -) - -func TestExtractInlineTagsToFilePath(t *testing.T) { - const filePath = "./gosdir/foo.golang.rox.txt" - - table := map[string]string{ - "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", - "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: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 { - t.Run(content, func(t *testing.T) { - newFilePath, _, err := extractInlineTagsToFilePath(filePath, content) - if err != nil { - t.Error(err) - } - if newFilePath != expectedFilePath { - t.Errorf("expected file path '%s' but got '%s'", expectedFilePath, newFilePath) - } - }) - } -} - -func TestExtractInlineTagsFromContent(t *testing.T) { - table := map[string][]string{ - "foo,bar,baz blablablabla...": {"foo", "bar", "baz"}, - "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:linkedin"}, - "share:li,foo this is the main content": {"share:linkedin", "foo"}, - "shar()e:li,foo this is the main content": {}, - } - - for input, expectedTags := range table { - t.Run(input, func(t *testing.T) { - tags, contentWithoutTags, err := extractInlineTagsFromContent(input) - if err != nil { - t.Error(err) - } - if len(tags) != len(expectedTags) { - t.Errorf("expected %d inline tags (%v) but got %d (%v)", - len(expectedTags), expectedTags, len(tags), tags) - } - for _, expectedTag := range expectedTags { - if !slices.Contains(tags, expectedTag) { - t.Errorf("expected '%s' to be an inline tag but got '%v'", - expectedTag, tags) - } - } - - expectedMainContent := input - parts := strings.Split(input, " ") - if inlineTagRE.MatchString(parts[0]) { - expectedMainContent = strings.Join(parts[1:], " ") - } - - if contentWithoutTags != expectedMainContent { - t.Errorf("expected the main content to be '%s' but got '%s'", - expectedMainContent, contentWithoutTags) - } - }) - } -} diff --git a/internal/queue/queue.go b/internal/queue/queue.go index 832b877..80f6dcd 100644 --- a/internal/queue/queue.go +++ b/internal/queue/queue.go @@ -12,6 +12,7 @@ import ( "codeberg.org/snonux/gos/internal/entry" "codeberg.org/snonux/gos/internal/oi" "codeberg.org/snonux/gos/internal/platforms" + "codeberg.org/snonux/gos/internal/tags" "codeberg.org/snonux/gos/internal/timestamp" ) @@ -36,7 +37,7 @@ func queueEntries(args config.Args) error { } for filePath := range ch { - if filePath, err = extractInlineTags(filePath); err != nil { + if filePath, err = tags.InlineExtract(filePath); err != nil { return err } en, err := entry.New(filePath) @@ -82,11 +83,12 @@ func queuePlatforms(args config.Args) error { if err != nil { return err } - excluded, err := en.PlatformExcluded(args, platform.String()) + // func NewShare(args config.Args, tags map[string]struct{}) (Share, error) { + share, err := tags.NewShare(args, en.Tags) if err != nil { return err } - if excluded { + if share.Excluded(platform.String()) { colour.Infoln("Not queueing entry", en, "to platform", platform, "as it is excluded") continue } |
