diff options
| author | Mitch Riedstra <mitch@riedstra.us> | 2020-09-26 20:59:34 -0400 |
|---|---|---|
| committer | Mitch Riedstra <mitch@riedstra.us> | 2020-09-26 21:01:33 -0400 |
| commit | cddf7ed45147869a9d2eee2e40f59a97c472ea72 (patch) | |
| tree | 551e04c6018a46b4288d9ea2e7ee3ffb666c271b /local/index.go | |
| parent | 09979dcab01643349cee425b71fa3b4db24f8d60 (diff) | |
| download | go-website-cddf7ed45147869a9d2eee2e40f59a97c472ea72.tar.gz go-website-cddf7ed45147869a9d2eee2e40f59a97c472ea72.tar.xz | |
Split up http related stuff out of the page library.
Make it an interface.
Rename page to be 'local' reflecting that it reads the website off the local disk.
Update the build script to include the go version.
Switch to gorilla/mux
Remove the convert command, since we're no longer utilizing that old
layout or have any need to convert from it.
Diffstat (limited to 'local/index.go')
| -rw-r--r-- | local/index.go | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/local/index.go b/local/index.go new file mode 100644 index 0000000..a90276e --- /dev/null +++ b/local/index.go @@ -0,0 +1,69 @@ +package local + +import ( + "fmt" + "os" + "path/filepath" + "strings" + "time" +) + +var pageIndex map[string]PageList + +func (p *Page) RebuildIndex() error { + pageIndex = nil + _, err := p.Index() + return err +} + +// Index returns a map of all pages below the current Page's Path seperated +// into their respective tags If a Page has multiple tags it will be listed +// under each +func (p *Page) Index() (map[string]PageList, error) { + if pageIndex != nil { + return pageIndex, nil + } + fmt.Fprintln(os.Stderr, "Rebuilding index...") + + out := make(map[string]PageList) + + var pageErr error + + filepath.Walk(filepath.Dir(p.Path), + func(path string, info os.FileInfo, err error) error { + if err != nil { + return err + } + + if !info.IsDir() && strings.HasSuffix(info.Name(), ".md") { + + p2 := &Page{Path: strings.ReplaceAll(path, ".md", "")} + err = p2.Read() + + if err != nil { + pageErr = err + fmt.Fprintln(os.Stderr, "Error encountered: ", err) + return err + } + + for tag, _ := range p2.Tags { + if _, ok := out[tag]; !ok { + out[tag] = []*Page{p2} + } else { + out[tag] = append(out[tag], p2) + } + } + + } + + return nil + }) + + pageIndex = out + + return out, nil +} + +func (p *Page) Time() time.Time { + return p.Date.Time +} |
