From 8f65d459ff2062fde45e511c96a10b15cebb8f1b Mon Sep 17 00:00:00 2001 From: Peter Stuifzand Date: Sun, 17 Jan 2021 15:38:48 +0100 Subject: [PATCH] Try two date formats as block name --- file.go | 35 ++++++++++++++++++++++++++++------- util.go | 6 +++++- 2 files changed, 33 insertions(+), 8 deletions(-) diff --git a/file.go b/file.go index a0d483e..6d3b455 100644 --- a/file.go +++ b/file.go @@ -41,7 +41,6 @@ type Block struct { Parent string } - // ListItemV2 is way to convert from old structure to new structure type ListItemV2 struct { ID ID @@ -127,24 +126,38 @@ func convertBlocksToListItems(current string, blocks BlockResponse, indent int) return listItems } +type titleOption struct { + date bool + timeObj time.Time +} + + +// 1_januari_2021 +// 2021-01-01 + func (fp *FilePages) Get(name string) Page { var sw stopwatch sw.Start("Get " + name) 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 { blocks, err := loadBlocks(fp.dirname, name) if err != nil && errors.Is(err, BlockNotFound) { continue } - return fp.blocksBackendGet(name, blocks) + return fp.blocksBackendGet(name, blocks, titleOption{date, t}) } 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{} current := blocks.PageID listItems := convertBlocksToListItems(current, blocks, 0) @@ -162,9 +175,11 @@ func (fp *FilePages) blocksBackendGet(name string, blocks BlockResponse) Page { refs = nil } + title := formatTitle(blocks.Texts[name], option) + return Page{ Name: name, - Title: blocks.Texts[name], + Title: title, Content: buf.String(), Refs: refs, 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 { name := strings.Replace(title, " ", "_", -1) title = strings.Replace(title, "_", " ", -1) @@ -418,7 +440,6 @@ func saveBlocksFromPage(dirname string, page Page) error { return err } - func loadBlocks(dirname, rootBlockID string) (BlockResponse, error) { resp := BlockResponse{ rootBlockID, diff --git a/util.go b/util.go index 7644576..0f7cbd6 100644 --- a/util.go +++ b/util.go @@ -99,6 +99,10 @@ func (sw *stopwatch) Stop() { func todayPage() string { now := time.Now() + return formatDateTitle(now) +} + +func formatDateTitle(date time.Time) string { months := []string{ "", "januari", @@ -114,7 +118,7 @@ func todayPage() string { "november", "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) {