commit d68ff22267c76ce4c2bf45990da0ddaf04b45df2 (tree)
parent 84ad3ab63fab39746952d21a753f08789418fb5f
Author: Motiejus Jakštys <desired.mta@gmail.com>
Date: Mon, 8 Jun 2020 09:46:39 +0300
rename data structures
Diffstat:
3 files changed, 32 insertions(+), 21 deletions(-)
diff --git a/src/joplin2site/internal/note/note.go b/src/joplin2site/internal/note/note.go
@@ -85,7 +85,16 @@ func Parse(in string) (Note, error) {
return note, nil
}
-type Notes map[string]*Note
+type (
+ // Notes is a list of notes by ID.
+ Notes map[string]*Note
+
+ // NoteTree is a note hierarchy by ID.
+ NoteTree map[string]NoteChildren
+
+ // NoteChildren is a string set of node's children.
+ NoteChildren map[string]struct{}
+)
func ListNotes(dir string) (Notes, error) {
files, err := ioutil.ReadDir(dir)
@@ -116,7 +125,7 @@ func (notes Notes) Shake(tld string) (Notes, error) {
return nil, fmt.Errorf("tld %q not found", tld)
}
- children := make(children)
+ children := make(NoteChildren)
buildTree(notes).flatten(topID, children)
ret := make(Notes, len(children))
@@ -142,14 +151,11 @@ func (notes Notes) GetFolderID(title string) string {
return parentID
}
-type tree map[string]children
-type children map[string]struct{}
-
-func buildTree(notes Notes) tree {
- ret := make(tree)
+func buildTree(notes Notes) NoteTree {
+ ret := make(NoteTree)
for _, note := range notes {
if _, ok := ret[note.ParentID]; !ok {
- ret[note.ParentID] = make(children)
+ ret[note.ParentID] = make(NoteChildren)
}
ret[note.ParentID][note.ID] = struct{}{}
}
@@ -157,7 +163,7 @@ func buildTree(notes Notes) tree {
}
// flatten returns all children of a tree
-func (t tree) flatten(id string, acc children) {
+func (t NoteTree) flatten(id string, acc NoteChildren) {
acc[id] = struct{}{}
for child := range t {
t.flatten(child, acc)
diff --git a/src/joplin2site/internal/page/page.go b/src/joplin2site/internal/page/page.go
@@ -11,17 +11,23 @@ import (
"gopkg.in/yaml.v2"
)
-// Page contains everything that's necessary to render a page.
-type Page struct {
- ID string
- Title string
- URL string
- Body string
- CreatedAt time.Time
- PublishedAt time.Time
-}
+type (
+ // Page contains everything that's necessary to render a page.
+ Page struct {
+ ID string
+ Title string
+ URL string
+ Body string
+ CreatedAt time.Time
+ PublishedAt time.Time
+ }
-type Pages []Page
+ // Pages is a slice of pages.
+ Pages []Page
+
+ // PageHierarchy is a map of: folderID -> ordered list of pages.
+ PageHierarchy map[string][]*Page
+)
type userMeta struct {
URL string `yaml:"url"`
diff --git a/src/joplin2site/internal/page/render.go b/src/joplin2site/internal/page/render.go
@@ -11,8 +11,7 @@ import (
// Render() renders the concrete page
func (p *Page) Render(notes note.Notes) ([]byte, error) {
- tplName := p.Title + "-" + p.ID
- tpl, err := template.New(tplName).Parse(p.Body)
+ tpl, err := template.New(p.ID).Parse(p.Body)
if err != nil {
return nil, fmt.Errorf("failed to parse user's page: %w", err)
}