|
| 1 | +package gobookmarks |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "strings" |
| 6 | +) |
| 7 | + |
| 8 | +// ExtractTab returns the text for a tab by name including the 'Tab:' line. |
| 9 | +func ExtractTab(bookmarks, name string) (string, error) { |
| 10 | + lines := strings.Split(bookmarks, "\n") |
| 11 | + start := -1 |
| 12 | + end := len(lines) |
| 13 | + lower := strings.ToLower(name) |
| 14 | + for i, line := range lines { |
| 15 | + trimmed := strings.TrimSpace(line) |
| 16 | + if strings.HasPrefix(strings.ToLower(trimmed), "tab:") { |
| 17 | + tabName := strings.TrimSpace(line[4:]) |
| 18 | + if start == -1 && strings.EqualFold(tabName, lower) { |
| 19 | + start = i |
| 20 | + } else if start != -1 { |
| 21 | + end = i |
| 22 | + break |
| 23 | + } |
| 24 | + } |
| 25 | + } |
| 26 | + if start == -1 { |
| 27 | + return "", fmt.Errorf("tab %s not found", name) |
| 28 | + } |
| 29 | + return strings.Join(lines[start:end], "\n"), nil |
| 30 | +} |
| 31 | + |
| 32 | +// ReplaceTab replaces the tab with name with newName and newText. |
| 33 | +// newText should not include the leading 'Tab:' line. |
| 34 | +func ReplaceTab(bookmarks, name, newName, newText string) (string, error) { |
| 35 | + lines := strings.Split(bookmarks, "\n") |
| 36 | + start := -1 |
| 37 | + end := len(lines) |
| 38 | + lower := strings.ToLower(name) |
| 39 | + for i, line := range lines { |
| 40 | + trimmed := strings.TrimSpace(line) |
| 41 | + if strings.HasPrefix(strings.ToLower(trimmed), "tab:") { |
| 42 | + tabName := strings.TrimSpace(line[4:]) |
| 43 | + if start == -1 && strings.EqualFold(tabName, lower) { |
| 44 | + start = i |
| 45 | + } else if start != -1 { |
| 46 | + end = i |
| 47 | + break |
| 48 | + } |
| 49 | + } |
| 50 | + } |
| 51 | + if start == -1 { |
| 52 | + return "", fmt.Errorf("tab %s not found", name) |
| 53 | + } |
| 54 | + var result []string |
| 55 | + result = append(result, lines[:start]...) |
| 56 | + result = append(result, "Tab: "+newName) |
| 57 | + if newText != "" { |
| 58 | + newLines := strings.Split(strings.TrimSuffix(newText, "\n"), "\n") |
| 59 | + result = append(result, newLines...) |
| 60 | + } |
| 61 | + result = append(result, lines[end:]...) |
| 62 | + return strings.Join(result, "\n"), nil |
| 63 | +} |
| 64 | + |
| 65 | +// AppendTab appends a new tab with name and text to bookmarks. |
| 66 | +func AppendTab(bookmarks, name, text string) string { |
| 67 | + if !strings.HasSuffix(bookmarks, "\n") { |
| 68 | + bookmarks += "\n" |
| 69 | + } |
| 70 | + bookmarks += "Tab: " + name |
| 71 | + if text != "" { |
| 72 | + if !strings.HasSuffix(text, "\n") { |
| 73 | + text += "\n" |
| 74 | + } |
| 75 | + bookmarks += "\n" + strings.TrimSuffix(text, "\n") |
| 76 | + } |
| 77 | + if !strings.HasSuffix(bookmarks, "\n") { |
| 78 | + bookmarks += "\n" |
| 79 | + } else { |
| 80 | + if !strings.HasSuffix(bookmarks, "\n\n") { |
| 81 | + bookmarks += "" |
| 82 | + } |
| 83 | + } |
| 84 | + return bookmarks |
| 85 | +} |
0 commit comments