summaryrefslogtreecommitdiff
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
parentf2318c4946db3ffd60a2b955e47660c2f77007df (diff)
initial inline to file path extraction
-rw-r--r--internal/entry/entry.go32
-rw-r--r--internal/entry/entry_test.go37
-rw-r--r--internal/oi/oi.go15
-rw-r--r--internal/queue/queue.go8
4 files changed, 20 insertions, 72 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")
diff --git a/internal/oi/oi.go b/internal/oi/oi.go
index b2a0953..824c0bd 100644
--- a/internal/oi/oi.go
+++ b/internal/oi/oi.go
@@ -147,3 +147,18 @@ func SlurpAndTrim(filePath string) (string, error) {
}
return strings.TrimSpace(string(bytes)), nil
}
+
+func WriteFile(filePath, content string) error {
+ tmpFilePath := fmt.Sprintf("%s.tmp", filePath)
+ file, err := os.OpenFile(tmpFilePath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
+ if err != nil {
+ return err
+ }
+ defer file.Close()
+
+ _, err = file.WriteString(content)
+ if err != nil {
+ return err
+ }
+ return os.Rename(tmpFilePath, filePath)
+}
diff --git a/internal/queue/queue.go b/internal/queue/queue.go
index de238f0..40cc084 100644
--- a/internal/queue/queue.go
+++ b/internal/queue/queue.go
@@ -35,12 +35,11 @@ func queueEntries(args config.Args) error {
}
for filePath := range ch {
- en, err := entry.New(filePath)
- if err != nil {
+ if filePath, err = extractInlineTags(filePath); err != nil {
return err
}
- // Extract any inline tags, if any!
- if err := en.ExtractInlineTags(); err != nil {
+ en, err := entry.New(filePath)
+ if err != nil {
return err
}
if en.HasTag("ask") {
@@ -48,6 +47,7 @@ func queueEntries(args config.Args) error {
return err
}
}
+
destPath := fmt.Sprintf("%s/db/%s.%s.queued", args.GosDir, filepath.Base(en.Path), timestamp.Now())
if args.DryRun {
colour.Infoln("Not queueing entry", en.Path, "to", destPath, "as dry-run mode enabled")