diff options
| author | Paul Buetow <paul@buetow.org> | 2024-11-19 11:02:59 +0200 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2024-11-19 11:02:59 +0200 |
| commit | 1550fbf9c2b0602c4675f25cd92e389593894e58 (patch) | |
| tree | 59c26138b3feed8aba6a14c7d4dae5e51ccacef7 | |
| parent | ba7827c790347c7a3b70f19e2a2d7bb81642de3c (diff) | |
forgot to add this code
| -rw-r--r-- | internal/queue/inlinetags.go | 65 | ||||
| -rw-r--r-- | internal/queue/inlinetags_test.go | 64 |
2 files changed, 129 insertions, 0 deletions
diff --git a/internal/queue/inlinetags.go b/internal/queue/inlinetags.go new file mode 100644 index 0000000..bf147b1 --- /dev/null +++ b/internal/queue/inlinetags.go @@ -0,0 +1,65 @@ +package queue + +import ( + "fmt" + "os" + "path/filepath" + "strings" + + "codeberg.org/snonux/gos/internal/colour" + "codeberg.org/snonux/gos/internal/oi" +) + +// 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 := extractInlineTagsFromContent(content) + 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) { + parts := strings.Split(content, " ") + // If the first word of the content contains a dot or comma and there are + // more than 2 elems, then there are inline tags! + if strings.Contains(parts[0], ".") || strings.Contains(parts[0], ",") { + var tags []string + for _, elem := range strings.Split(parts[0], ".") { + tags = append(tags, strings.Split(elem, ",")...) + } + if len(tags) > 1 { + return tags, strings.Join(parts[1:], " ") + } + } + return []string{}, content +} diff --git a/internal/queue/inlinetags_test.go b/internal/queue/inlinetags_test.go new file mode 100644 index 0000000..5b22fe6 --- /dev/null +++ b/internal/queue/inlinetags_test.go @@ -0,0 +1,64 @@ +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:li.foo.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,foo this is the main content": {"share:li", "foo"}, + } + + for input, expectedTags := range table { + t.Run(input, func(t *testing.T) { + tags, contentWithoutTags := extractInlineTagsFromContent(input) + 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) + } + } + parts := strings.Split(input, " ") + expectedMainContent := strings.Join(parts[1:], " ") + if contentWithoutTags != expectedMainContent { + t.Errorf("expected the main content to be '%s' but got '%s'", + expectedMainContent, contentWithoutTags) + } + + }) + } +} |
