Extract old and new pages backend functions

This commit is contained in:
Peter Stuifzand 2021-01-17 14:12:17 +01:00
parent 31ff5555d8
commit aa663484bc

66
file.go
View File

@ -153,22 +153,52 @@ 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)
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)
if listItems == nil {
listItems = []ActualListItem{}
}
err := json.NewEncoder(&buf).Encode(&listItems)
if err != nil {
log.Printf("while encoding blocks: %s", err)
}
return Page{
Name: name,
Title: blocks.Texts[name],
Content: buf.String(),
Refs: refs,
Blocks: blocks,
Parent: blocks.ParentID,
}
}
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,
@ -181,9 +211,10 @@ func (fp *FilePages) Get(title string) Page {
defer f.Close()
body, err := ioutil.ReadAll(f)
if err != nil {
log.Printf("while reading file in oldPagesBackend: %s", err)
return Page{
Title: title,
Name: name,
Title: title,
Content: "",
Refs: refs,
Blocks: blocks,
@ -197,27 +228,6 @@ func (fp *FilePages) Get(title string) Page {
Refs: refs,
Blocks: blocks,
}
}
buf := bytes.Buffer{}
current := blocks.PageID
listItems := convertBlocksToListItems(current, blocks, 0)
if listItems == nil {
listItems = []ActualListItem{}
}
err = json.NewEncoder(&buf).Encode(&listItems)
sw.Stop()
return Page{
Name: name,
Title: blocks.Texts[name],
Content: buf.String(),
Refs: refs,
Blocks: blocks,
Parent: blocks.ParentID,
}
}
func (fp *FilePages) Save(p string, page Page, summary, author string) error {