Try two date formats as block name

This commit is contained in:
Peter Stuifzand 2021-01-17 15:38:48 +01:00
parent 36dab44a24
commit 8f65d459ff
2 changed files with 33 additions and 8 deletions

35
file.go
View File

@ -41,7 +41,6 @@ type Block struct {
Parent string Parent string
} }
// ListItemV2 is way to convert from old structure to new structure // ListItemV2 is way to convert from old structure to new structure
type ListItemV2 struct { type ListItemV2 struct {
ID ID ID ID
@ -127,24 +126,38 @@ func convertBlocksToListItems(current string, blocks BlockResponse, indent int)
return listItems return listItems
} }
type titleOption struct {
date bool
timeObj time.Time
}
// 1_januari_2021
// 2021-01-01
func (fp *FilePages) Get(name string) Page { func (fp *FilePages) Get(name string) Page {
var sw stopwatch var sw stopwatch
sw.Start("Get " + name) sw.Start("Get " + name)
defer sw.Stop() defer sw.Stop()
names := []string{name} var names []string
date := false
t, err := time.Parse("2006-01-02", name)
if err == nil {
date = true
names = append(names, formatDateTitle(t), name)
}
for _, name := range names { for _, name := range names {
blocks, err := loadBlocks(fp.dirname, name) blocks, err := loadBlocks(fp.dirname, name)
if err != nil && errors.Is(err, BlockNotFound) { if err != nil && errors.Is(err, BlockNotFound) {
continue continue
} }
return fp.blocksBackendGet(name, blocks) return fp.blocksBackendGet(name, blocks, titleOption{date, t})
} }
return fp.oldPagesBackend(name) return fp.oldPagesBackend(name)
} }
func (fp *FilePages) blocksBackendGet(name string, blocks BlockResponse) Page { func (fp *FilePages) blocksBackendGet(name string, blocks BlockResponse, option titleOption) Page {
buf := bytes.Buffer{} buf := bytes.Buffer{}
current := blocks.PageID current := blocks.PageID
listItems := convertBlocksToListItems(current, blocks, 0) listItems := convertBlocksToListItems(current, blocks, 0)
@ -162,9 +175,11 @@ func (fp *FilePages) blocksBackendGet(name string, blocks BlockResponse) Page {
refs = nil refs = nil
} }
title := formatTitle(blocks.Texts[name], option)
return Page{ return Page{
Name: name, Name: name,
Title: blocks.Texts[name], Title: title,
Content: buf.String(), Content: buf.String(),
Refs: refs, Refs: refs,
Blocks: blocks, Blocks: blocks,
@ -172,6 +187,13 @@ func (fp *FilePages) blocksBackendGet(name string, blocks BlockResponse) Page {
} }
} }
func formatTitle(title string, option titleOption) string {
if option.date {
return formatDateTitle(option.timeObj)
}
return title
}
func (fp *FilePages) oldPagesBackend(title string) Page { func (fp *FilePages) oldPagesBackend(title string) Page {
name := strings.Replace(title, " ", "_", -1) name := strings.Replace(title, " ", "_", -1)
title = strings.Replace(title, "_", " ", -1) title = strings.Replace(title, "_", " ", -1)
@ -418,7 +440,6 @@ func saveBlocksFromPage(dirname string, page Page) error {
return err return err
} }
func loadBlocks(dirname, rootBlockID string) (BlockResponse, error) { func loadBlocks(dirname, rootBlockID string) (BlockResponse, error) {
resp := BlockResponse{ resp := BlockResponse{
rootBlockID, rootBlockID,

View File

@ -99,6 +99,10 @@ func (sw *stopwatch) Stop() {
func todayPage() string { func todayPage() string {
now := time.Now() now := time.Now()
return formatDateTitle(now)
}
func formatDateTitle(date time.Time) string {
months := []string{ months := []string{
"", "",
"januari", "januari",
@ -114,7 +118,7 @@ func todayPage() string {
"november", "november",
"december", "december",
} }
return fmt.Sprintf("%d_%s_%d", now.Day(), months[now.Month()], now.Year()) return fmt.Sprintf("%d_%s_%d", date.Day(), months[date.Month()], date.Year())
} }
func PageTitle(pageText string) (string, error) { func PageTitle(pageText string) (string, error) {