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

92
file.go
View File

@ -153,52 +153,23 @@ 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)
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,
}
} }
// 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{} buf := bytes.Buffer{}
current := blocks.PageID current := blocks.PageID
listItems := convertBlocksToListItems(current, blocks, 0) listItems := convertBlocksToListItems(current, blocks, 0)
@ -206,9 +177,10 @@ func (fp *FilePages) Get(title string) Page {
listItems = []ActualListItem{} listItems = []ActualListItem{}
} }
err = json.NewEncoder(&buf).Encode(&listItems) err := json.NewEncoder(&buf).Encode(&listItems)
if err != nil {
sw.Stop() log.Printf("while encoding blocks: %s", err)
}
return Page{ return Page{
Name: name, 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 { 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