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 { func (fp *FilePages) Get(title string) Page {
var sw stopwatch var sw stopwatch
sw.Start("Get " + title) sw.Start("Get " + title)
// TODO: cleanup loading of pages defer sw.Stop()
// TODO: convert all pages to blocks
name := title
refs, err := getBackrefs(fp, name) refs, err := getBackrefs(fp, title)
if err != nil { if err != nil {
refs = nil refs = nil
} }
blocks, err := loadBlocks(fp.dirname, name) blocks, err := loadBlocks(fp.dirname, title)
if err != nil { 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) title = strings.Replace(title, "_", " ", -1)
f, err := os.Open(filepath.Join(fp.dirname, name)) f, err := os.Open(filepath.Join(fp.dirname, name))
if err != nil { if err != nil {
log.Printf("while opening file in oldPagesBackend: %s", err)
return Page{ return Page{
Title: title, Title: title,
Name: name, Name: name,
@ -181,9 +211,10 @@ func (fp *FilePages) Get(title string) Page {
defer f.Close() defer f.Close()
body, err := ioutil.ReadAll(f) body, err := ioutil.ReadAll(f)
if err != nil { if err != nil {
log.Printf("while reading file in oldPagesBackend: %s", err)
return Page{ return Page{
Title: title,
Name: name, Name: name,
Title: title,
Content: "", Content: "",
Refs: refs, Refs: refs,
Blocks: blocks, Blocks: blocks,
@ -199,27 +230,6 @@ func (fp *FilePages) Get(title string) Page {
} }
} }
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 { func (fp *FilePages) Save(p string, page Page, summary, author string) error {
fp.saveC <- saveMessage{p, page, summary, author} fp.saveC <- saveMessage{p, page, summary, author}
return nil return nil