Prepare for trying multiple names
This commit is contained in:
parent
4d79405e22
commit
36dab44a24
55
file.go
55
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
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user