commit a2bf753f537481b38a1ccc5809a84c59fbc6d57f (tree)
parent 33c88d76d8cd54e75a26c09465804adca4649ff2
Author: Motiejus Jakštys <desired.mta@gmail.com>
Date: Sun, 7 Jun 2020 19:00:47 +0300
wip trees
Diffstat:
2 files changed, 104 insertions(+), 77 deletions(-)
diff --git a/src/joplin2site/internal/note/tree.go b/src/joplin2site/internal/note/tree.go
@@ -0,0 +1,104 @@
+package note
+
+import (
+ "fmt"
+ "io/ioutil"
+ "time"
+)
+
+type Notes map[string]Note
+
+func ListNotes(dir string) (Notes, error) {
+ files, err := ioutil.ReadDir(dir)
+ if err != nil {
+ return nil, err
+ }
+ notes := make(Notes, len(files))
+ for _, file := range files {
+ body, err := ioutil.ReadFile(file.Name())
+ if err != nil {
+ return nil, fmt.Errorf("failed to read %q: %w", file.Name(), err)
+ }
+ note, err := Parse(string(body))
+ if err != nil {
+ return nil, fmt.Errorf("failed to parse %q: %w", file.Name(), err)
+ }
+
+ notes[note.ID] = note
+ }
+
+ return notes, nil
+}
+
+// SubPages returns immediate Pages of a tree
+func (t *Tree) SubPages(name string) ([]Page, error) {
+ // Find the parent note that will be the parent of the sub-notebook.
+ var parentID string
+ for _, inote := range t.Notes {
+ if inote.Type != note.ItemTypeFolder {
+ continue
+ }
+ if inote.Title == name {
+ parentID = inote.ID
+ }
+ }
+
+ if parentID == "" {
+ return nil, fmt.Errorf("sub-page %q not found", name)
+ }
+
+ var retPages []Page
+ for _, inote := range t.Notes {
+ if inote.ParentID != parentID {
+ continue
+ }
+ if inote.Type != note.ItemTypeNote {
+ continue
+ }
+
+ page, err := FromNote(inote)
+ if err != nil {
+ return nil, fmt.Errorf("failed to convert note to page: %w", err)
+ }
+ if page.PublishedAt.After(time.Now()) {
+ continue
+ }
+
+ retPages = append(retPages, page)
+ }
+ return retPages, nil
+}
+
+// Shake returns a sub-set of notes which eventually parent to "tld"
+func (n Notes) Shake(tld string) (Notes, error) {
+ tree := buildTree(n)
+ topID := n.getFolderID(tld)
+ if topID == "" {
+ return nil, fmt.Errorf("tld %q not found", tld)
+ }
+
+ children := make(children)
+ tree.flatten(topID, children)
+
+ ret := make(map[string]note.Note, len(children))
+ for noteID := range children {
+ ret[noteID] = n[noteID]
+ }
+
+ return ret, nil
+}
+
+// getFolderID returns a folder ID for a particular title
+func (n Notes) getFolderID(title string) string {
+ var parentID string
+ for _, note := range n {
+ if note.Type != ItemTypeFolder {
+ continue
+ }
+ if note.Title == title {
+ parentID = note.ID
+ }
+ }
+
+ return parentID
+}
diff --git a/src/joplin2site/internal/page/tree.go b/src/joplin2site/internal/page/tree.go
@@ -1,77 +0,0 @@
-package page
-
-import (
- "fmt"
- "io/ioutil"
- "time"
-
- "github.com/motiejus/dotfiles/joplin2site/internal/note"
-)
-
-type Tree struct {
- Notes []note.Note
-}
-
-// Buildtree builds a tree from the directory
-func BuildTree(dir string) (Tree, error) {
- files, err := ioutil.ReadDir(dir)
- if err != nil {
- return Tree{}, err
- }
- notes := make([]note.Note, 0, len(files))
- for _, file := range files {
- body, err := ioutil.ReadFile(file.Name())
- if err != nil {
- return Tree{}, fmt.Errorf("failed to read %q: %w", file.Name(), err)
- }
- subnote, err := note.Parse(string(body))
- if err != nil {
- return Tree{}, fmt.Errorf("failed to parse %q: %w", file.Name(), err)
- }
-
- notes = append(notes, subnote)
- }
-
- return Tree{
- Notes: notes,
- }, nil
-}
-
-// SubPages returns immediate Pages of a tree
-func (t *Tree) SubPages(name string) ([]Page, error) {
- // Find the parent note that will be the parent of the sub-notebook.
- var parentID string
- for _, inote := range t.Notes {
- if inote.Type != note.ItemTypeFolder {
- continue
- }
- if inote.Title == name {
- parentID = inote.ID
- }
- }
-
- if parentID == "" {
- return nil, fmt.Errorf("sub-page %q not found", name)
- }
-
- var retPages []Page
- for _, inote := range t.Notes {
- if inote.ParentID != parentID {
- continue
- }
- if inote.Type != note.ItemTypeNote {
- continue
- }
-
- page, err := FromNote(inote)
- if err != nil {
- return nil, fmt.Errorf("failed to convert note to page: %w", err)
- }
- if page.PublishedAt.After(time.Now()) {
- continue
- }
-
- retPages = append(retPages, page)
- }
- return retPages, nil
-}