-
Notifications
You must be signed in to change notification settings - Fork 0
/
split.go
75 lines (62 loc) · 1.79 KB
/
split.go
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
package zet
import (
"errors"
"fmt"
"path/filepath"
"strconv"
"strings"
"github.com/ericstrs/zet/internal/meta"
"github.com/ericstrs/zet/internal/storage"
)
// SplitZettel splits zettel content from stdin into sub-zettels.
func SplitZettel(zetDir, zettelDir, b string) error {
if b == "" {
return errors.New("zettel content is empty")
}
currLink, err := meta.Link(zettelDir)
if err != nil {
return fmt.Errorf("Error getting current link: %v", err)
}
zettels := makeZettels(strings.Split(b, "\n"))
i := Isosec()
iso, err := strconv.Atoi(i)
if err != nil {
return fmt.Errorf("Error converting isosec string to int: %v", err)
}
for _, z := range zettels {
iso++
newDirPath := filepath.Join(zetDir, fmt.Sprintf("%d", iso))
if err := dir(newDirPath); err != nil {
return fmt.Errorf("Error creating new zettel directory: %v", err)
}
if err := Add(newDirPath, "", z.Title, z.Body, "", currLink, false); err != nil {
return fmt.Errorf("Error adding sub-zettels: %v", err)
}
}
return nil
}
// makeZettels construct sub-zettels from a list of strings.
func makeZettels(bodyLines []string) []storage.Zettel {
var zettels []storage.Zettel
var currZettel storage.Zettel
var isInsideZettel bool
for i, line := range bodyLines {
if strings.HasPrefix(line, `## `) || i == len(bodyLines)-1 {
if isInsideZettel {
zettels = append(zettels, currZettel)
currZettel = storage.Zettel{}
}
currZettel.Title = strings.TrimPrefix(line, `## `)
isInsideZettel = true
continue
}
// Check if the line starts with more than two hash symbols
if strings.HasPrefix(line, "###") {
// Remove one hash symbol
line = "#" + strings.TrimPrefix(line, "##")
}
// If line starts with more than two hash tags, then remove one.
currZettel.Body += line + "\n"
}
return zettels
}