summaryrefslogtreecommitdiff
path: root/internal/entry
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2024-11-19 10:56:44 +0200
committerPaul Buetow <paul@buetow.org>2024-11-19 10:56:44 +0200
commit744c3b70fa590345f6c1de715e2a572f184091c4 (patch)
treed198cf86b5afe2fbc7af42fdb0b1262c2f5bc30c /internal/entry
parentf2318c4946db3ffd60a2b955e47660c2f77007df (diff)
initial inline to file path extraction
Diffstat (limited to 'internal/entry')
-rw-r--r--internal/entry/entry.go32
-rw-r--r--internal/entry/entry_test.go37
2 files changed, 1 insertions, 68 deletions
diff --git a/internal/entry/entry.go b/internal/entry/entry.go
index 1237c3e..7c574a0 100644
--- a/internal/entry/entry.go
+++ b/internal/entry/entry.go
@@ -106,14 +106,12 @@ func (en *Entry) Content() (string, []string, error) {
return content, extractURLs(content), err
}
-// Returns the content and also checks for the size limit and also removes any
-// inline tags.
+// Returns the content and also checks for the size limit
func (en Entry) ContentWithLimit(sizeLimit int) (string, []string, error) {
content, urls, err := en.Content()
if err != nil {
return "", urls, err
}
- _, content, _ = extractInlineTags(content)
if len(content) > sizeLimit {
err := fmt.Errorf("%w (%d > %d): %v", ErrSizeLimitExceeded, len(content), sizeLimit, en)
if err2 := prompt.Acknowledge("You need to shorten the content as "+err.Error(), content); err2 != nil {
@@ -190,36 +188,8 @@ func (en Entry) extractTags(parts []string) {
}
}
-func (en Entry) ExtractInlineTags() error {
- content, _, err := en.Content()
- if err != nil {
- return err
- }
- if tags, _, ok := extractInlineTags(content); ok {
- en.extractTags(tags)
- }
- return nil
-}
-
func extractURLs(input string) []string {
urlPattern := `(http://|https://|ftp://)[^\s]+`
re := regexp.MustCompile(urlPattern)
return re.FindAllString(input, -1)
}
-
-// Returns inline tags, real content. And true when there were inline tags.
-func extractInlineTags(content string) ([]string, string, bool) {
- 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:], " "), true
- }
- }
- return []string{}, content, false
-}
diff --git a/internal/entry/entry_test.go b/internal/entry/entry_test.go
index f88d848..6a54b05 100644
--- a/internal/entry/entry_test.go
+++ b/internal/entry/entry_test.go
@@ -126,43 +126,6 @@ func TestHasTag(t *testing.T) {
}
}
-func TestExtractInlineTags(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, ok := extractInlineTags(input)
- if !ok {
- t.Error("expected inline tags but none were found")
- }
- 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)
- }
-
- })
- }
-
-}
-
func FuzzExtractURLs(f *testing.F) {
f.Add("/path?myjfa=lwsr4imj&dgqeg=m3uwwsak")
f.Add("/?amfbm=bwzqu46m&xheuh=nv588d98")