This repository was archived by the owner on Jun 24, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbookmarkTabEdit.go
More file actions
85 lines (81 loc) · 2.16 KB
/
bookmarkTabEdit.go
File metadata and controls
85 lines (81 loc) · 2.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package a4webbm
import (
"fmt"
"strings"
)
// ExtractTab returns the text for a tab by name including the 'Tab:' line.
func ExtractTab(bookmarks, name string) (string, error) {
lines := strings.Split(bookmarks, "\n")
start := -1
end := len(lines)
lower := strings.ToLower(name)
for i, line := range lines {
trimmed := strings.TrimSpace(line)
if strings.HasPrefix(strings.ToLower(trimmed), "tab:") {
tabName := strings.TrimSpace(line[4:])
if start == -1 && strings.EqualFold(tabName, lower) {
start = i
} else if start != -1 {
end = i
break
}
}
}
if start == -1 {
return "", fmt.Errorf("tab %s not found", name)
}
return strings.Join(lines[start:end], "\n"), nil
}
// ReplaceTab replaces the tab with name with newName and newText.
// newText should not include the leading 'Tab:' line.
func ReplaceTab(bookmarks, name, newName, newText string) (string, error) {
lines := strings.Split(bookmarks, "\n")
start := -1
end := len(lines)
lower := strings.ToLower(name)
for i, line := range lines {
trimmed := strings.TrimSpace(line)
if strings.HasPrefix(strings.ToLower(trimmed), "tab:") {
tabName := strings.TrimSpace(line[4:])
if start == -1 && strings.EqualFold(tabName, lower) {
start = i
} else if start != -1 {
end = i
break
}
}
}
if start == -1 {
return "", fmt.Errorf("tab %s not found", name)
}
var result []string
result = append(result, lines[:start]...)
result = append(result, "Tab: "+newName)
if newText != "" {
newLines := strings.Split(strings.TrimSuffix(newText, "\n"), "\n")
result = append(result, newLines...)
}
result = append(result, lines[end:]...)
return strings.Join(result, "\n"), nil
}
// AppendTab appends a new tab with name and text to bookmarks.
func AppendTab(bookmarks, name, text string) string {
if !strings.HasSuffix(bookmarks, "\n") {
bookmarks += "\n"
}
bookmarks += "Tab: " + name
if text != "" {
if !strings.HasSuffix(text, "\n") {
text += "\n"
}
bookmarks += "\n" + strings.TrimSuffix(text, "\n")
}
if !strings.HasSuffix(bookmarks, "\n") {
bookmarks += "\n"
} else {
if !strings.HasSuffix(bookmarks, "\n\n") {
bookmarks += ""
}
}
return bookmarks
}