From a34c18ba4135b14dc6d3502f194b3a1bbc89aae1 Mon Sep 17 00:00:00 2001 From: Eddy Pedroni Date: Sat, 1 Feb 2020 17:24:34 +0100 Subject: Generation is working correctly, without temporary output directory --- entry.go | 22 +++++++++++++++++++--- nav.go | 9 ++++++++- page.go | 2 ++ site.go | 20 ++++++++++---------- 4 files changed, 39 insertions(+), 14 deletions(-) diff --git a/entry.go b/entry.go index 189014f..ddb3bd8 100644 --- a/entry.go +++ b/entry.go @@ -4,8 +4,8 @@ import ( "os" "path/filepath" "regexp" + "sort" "strconv" - "fmt" ) /* @@ -28,7 +28,8 @@ func findEntries(root string) (entries []Renderer) { baseEntryRegex := regexp.MustCompile("([A-z\\-]+)\\.md") logEntryRegex := regexp.MustCompile("(20[0-9]{2})-([0-9]{2})-([0-9]{2})-([A-z\\-]+)\\.md") - fmt.Println(root) + logEntries := make([]logEntry, 0) + err := filepath.Walk(root, func(p string, f os.FileInfo, e error) error { if !f.IsDir() && baseEntryRegex.MatchString(f.Name()) { baseCaptures := baseEntryRegex.FindStringSubmatch(f.Name()) @@ -47,7 +48,7 @@ func findEntries(root string) (entries []Renderer) { day: d, month: m, year: y} - entries = append(entries, newLogEntry) + logEntries = append(logEntries, newLogEntry) } else { entries = append(entries, newBaseEntry) } @@ -58,5 +59,20 @@ func findEntries(root string) (entries []Renderer) { panic(err) } + sort.Slice(logEntries, func(i, j int) bool { + if logEntries[i].year != logEntries[j].year { + return logEntries[i].year > logEntries[j].year + } else if logEntries[i].month != logEntries[j].month { + return logEntries[i].month > logEntries[j].month + } else { + return logEntries[i].day > logEntries[j].day + } + }) + + // must append each manually: https://golang.org/doc/faq#convert_slice_of_interface + for _, e := range logEntries { + entries = append(entries, e) + } + return } diff --git a/nav.go b/nav.go index 39b5b64..153a493 100644 --- a/nav.go +++ b/nav.go @@ -1,6 +1,9 @@ package main -import () +import ( + "sort" + "strings" +) type navItem struct { AbsoluteUrl string @@ -12,4 +15,8 @@ func createNavSlice(site *siteData, cfg config) { newNavItem := navItem{AbsoluteUrl: site.baseUrl + "/" + k, Text: v.pageName} site.nav = append(site.nav, newNavItem) } + + sort.Slice(site.nav, func(i, j int) bool { + return strings.Compare(site.nav[i].Text, site.nav[j].Text) < 0 + }) } diff --git a/page.go b/page.go index 5226df8..8bf656a 100644 --- a/page.go +++ b/page.go @@ -1,5 +1,7 @@ package main +import () + /* * This file contains all the code related to turning entries into HTML pages */ diff --git a/site.go b/site.go index 8a9f08d..4050bf0 100644 --- a/site.go +++ b/site.go @@ -6,13 +6,13 @@ import ( ) type siteData struct { - name string - rawName string - baseUrl string + name string + rawName string + baseUrl string outputPath string - sourcePath string - pages map[string]page - nav []navItem + sourcePath string + pages map[string]page + nav []navItem } func getSites(cfg config) (sites []siteData) { @@ -24,11 +24,11 @@ func getSites(cfg config) (sites []siteData) { for _, f := range files { if f.IsDir() && f.Name() != ".git" { newSite := siteData{ - name: nameify(f.Name()), - rawName: f.Name(), - baseUrl: filepath.Join(cfg.BaseUrl, f.Name()), + name: nameify(f.Name()), + rawName: f.Name(), + baseUrl: filepath.Join(cfg.BaseUrl, f.Name()), outputPath: filepath.Join(cfg.TargetDir, f.Name()), - sourcePath: filepath.Join(cfg.DocsRoot, f.Name())} + sourcePath: filepath.Join(cfg.DocsRoot, f.Name())} sites = append(sites, newSite) } } -- cgit v1.2.3