From 36dab44a24512009b9462a4b8c7ec0faa1c0af01 Mon Sep 17 00:00:00 2001 From: Peter Stuifzand Date: Sun, 17 Jan 2021 14:55:10 +0100 Subject: [PATCH] Prepare for trying multiple names --- file.go | 55 +++++++++++++++++++++++++++++-------------------------- 1 file changed, 29 insertions(+), 26 deletions(-) diff --git a/file.go b/file.go index c321140..a0d483e 100644 --- a/file.go +++ b/file.go @@ -4,16 +4,15 @@ import ( "bufio" "bytes" "encoding/json" + "errors" "fmt" "html" "html/template" "io/ioutil" "log" - "math/rand" "os" "os/exec" "path/filepath" - "strconv" "strings" "time" @@ -27,6 +26,8 @@ const ( BlocksDirectory = "_blocks" ) +var BlockNotFound = errors.New("block not found") + type saveMessage struct { p string page Page @@ -80,6 +81,14 @@ type FilePages struct { index bleve.Index } +type BlockResponse struct { + PageID string + ParentID string + Texts map[string]string + Children map[string][]string + Parents []string +} + func NewFilePages(dirname string, index bleve.Index) PagesRepository { err := os.MkdirAll(filepath.Join(dirname, "_blocks"), 0777) if err != nil { @@ -118,18 +127,21 @@ func convertBlocksToListItems(current string, blocks BlockResponse, indent int) return listItems } -func (fp *FilePages) Get(title string) Page { +func (fp *FilePages) Get(name string) Page { var sw stopwatch - sw.Start("Get " + title) + sw.Start("Get " + name) defer sw.Stop() - blocks, err := loadBlocks(fp.dirname, title) - if err != nil { - return fp.oldPagesBackend(title, blocks) - } + names := []string{name} - // Blocks based pages - return fp.blocksBackendGet(title, blocks) + for _, name := range names { + blocks, err := loadBlocks(fp.dirname, name) + if err != nil && errors.Is(err, BlockNotFound) { + continue + } + return fp.blocksBackendGet(name, blocks) + } + return fp.oldPagesBackend(name) } func (fp *FilePages) blocksBackendGet(name string, blocks BlockResponse) Page { @@ -160,7 +172,7 @@ func (fp *FilePages) blocksBackendGet(name string, blocks BlockResponse) Page { } } -func (fp *FilePages) oldPagesBackend(title string, blocks BlockResponse) Page { +func (fp *FilePages) oldPagesBackend(title string) Page { name := strings.Replace(title, " ", "_", -1) title = strings.Replace(title, "_", " ", -1) @@ -177,7 +189,6 @@ func (fp *FilePages) oldPagesBackend(title string, blocks BlockResponse) Page { Name: name, Content: "", Refs: refs, - Blocks: blocks, } } @@ -190,7 +201,6 @@ func (fp *FilePages) oldPagesBackend(title string, blocks BlockResponse) Page { Title: title, Content: "", Refs: refs, - Blocks: blocks, } } @@ -199,7 +209,6 @@ func (fp *FilePages) oldPagesBackend(title string, blocks BlockResponse) Page { Title: title, Content: string(body), Refs: refs, - Blocks: blocks, } } @@ -409,13 +418,6 @@ func saveBlocksFromPage(dirname string, page Page) error { return err } -type BlockResponse struct { - PageID string - ParentID string - Texts map[string]string - Children map[string][]string - Parents []string -} func loadBlocks(dirname, rootBlockID string) (BlockResponse, error) { resp := BlockResponse{ @@ -433,10 +435,11 @@ func loadBlocks(dirname, rootBlockID string) (BlockResponse, error) { block, err := loadBlock(dirname, rootBlockID) if err != nil { - return BlockResponse{}, fmt.Errorf("while loading current block (%s): %w", rootBlockID, err) + return BlockResponse{}, err } + // NOTE: what does this do? if rootBlockID[0] != '_' && block.Children == nil { - return BlockResponse{}, fmt.Errorf("while loading current block (%s): not a block and no children", rootBlockID) + return BlockResponse{}, fmt.Errorf("not a block and has no children: %w", BlockNotFound) } prevID := rootBlockID @@ -469,7 +472,7 @@ func loadBlocks(dirname, rootBlockID string) (BlockResponse, error) { queue = queue[1:] block, err := loadBlock(dirname, current) if err != nil { - return BlockResponse{}, fmt.Errorf("while loading block (%s): %w", current, err) + return BlockResponse{}, err } resp.Texts[current] = block.Text resp.Children[current] = block.Children @@ -482,13 +485,13 @@ func loadBlocks(dirname, rootBlockID string) (BlockResponse, error) { func loadBlock(dirname, blockID string) (Block, error) { f, err := os.Open(filepath.Join(dirname, BlocksDirectory, blockID)) if err != nil { - return Block{}, err + return Block{}, fmt.Errorf("%q: %w", BlockNotFound) } defer f.Close() var block Block err = json.NewDecoder(f).Decode(&block) if err != nil { - return Block{}, err + return Block{}, fmt.Errorf("%q: %v", err) } return block, nil }