From aa663484bc88ce4cda576994e573be23b2cb64b9 Mon Sep 17 00:00:00 2001 From: Peter Stuifzand Date: Sun, 17 Jan 2021 14:12:17 +0100 Subject: [PATCH] Extract old and new pages backend functions --- file.go | 92 ++++++++++++++++++++++++++++++++------------------------- 1 file changed, 51 insertions(+), 41 deletions(-) diff --git a/file.go b/file.go index a9420c7..0396b5e 100644 --- a/file.go +++ b/file.go @@ -153,52 +153,23 @@ func convertBlocksToListItems(current string, blocks BlockResponse, indent int) func (fp *FilePages) Get(title string) Page { var sw stopwatch sw.Start("Get " + title) - // TODO: cleanup loading of pages - // TODO: convert all pages to blocks - name := title + defer sw.Stop() - refs, err := getBackrefs(fp, name) + refs, err := getBackrefs(fp, title) if err != nil { refs = nil } - blocks, err := loadBlocks(fp.dirname, name) + blocks, err := loadBlocks(fp.dirname, title) if err != nil { - name = strings.Replace(title, " ", "_", -1) - title = strings.Replace(title, "_", " ", -1) - - f, err := os.Open(filepath.Join(fp.dirname, name)) - if err != nil { - return Page{ - Title: title, - Name: name, - Content: "", - Refs: refs, - Blocks: blocks, - } - } - - defer f.Close() - body, err := ioutil.ReadAll(f) - if err != nil { - return Page{ - Title: title, - Name: name, - Content: "", - Refs: refs, - Blocks: blocks, - } - } - - return Page{ - Name: name, - Title: title, - Content: string(body), - Refs: refs, - Blocks: blocks, - } + return fp.oldPagesBackend(title, blocks, refs) } + // Blocks based pages + return fp.blocksBackendGet(title, blocks, refs) +} + +func (fp *FilePages) blocksBackendGet(name string, blocks BlockResponse, refs map[string][]Backref) Page { buf := bytes.Buffer{} current := blocks.PageID listItems := convertBlocksToListItems(current, blocks, 0) @@ -206,9 +177,10 @@ func (fp *FilePages) Get(title string) Page { listItems = []ActualListItem{} } - err = json.NewEncoder(&buf).Encode(&listItems) - - sw.Stop() + err := json.NewEncoder(&buf).Encode(&listItems) + if err != nil { + log.Printf("while encoding blocks: %s", err) + } return Page{ Name: name, @@ -220,6 +192,44 @@ func (fp *FilePages) Get(title string) Page { } } +func (fp *FilePages) oldPagesBackend(title string, blocks BlockResponse, refs map[string][]Backref) Page { + name := strings.Replace(title, " ", "_", -1) + title = strings.Replace(title, "_", " ", -1) + + f, err := os.Open(filepath.Join(fp.dirname, name)) + if err != nil { + log.Printf("while opening file in oldPagesBackend: %s", err) + return Page{ + Title: title, + Name: name, + Content: "", + Refs: refs, + Blocks: blocks, + } + } + + defer f.Close() + body, err := ioutil.ReadAll(f) + if err != nil { + log.Printf("while reading file in oldPagesBackend: %s", err) + return Page{ + Name: name, + Title: title, + Content: "", + Refs: refs, + Blocks: blocks, + } + } + + return Page{ + Name: name, + Title: title, + Content: string(body), + Refs: refs, + Blocks: blocks, + } +} + func (fp *FilePages) Save(p string, page Page, summary, author string) error { fp.saveC <- saveMessage{p, page, summary, author} return nil