diff options
| author | Mitchell Riedstra <mitch@riedstra.dev> | 2021-02-15 15:31:37 -0500 |
|---|---|---|
| committer | Mitchell Riedstra <mitch@riedstra.dev> | 2021-02-15 15:32:05 -0500 |
| commit | fe9ec7a0b45c9fd23a615a8b95ade3e9c1ea2d12 (patch) | |
| tree | 80844a62d5d18b30862cfdc610aae88713fe97d9 /page/index.go | |
| parent | d83f4bca3f7026696a41225caac11807ed06fc2f (diff) | |
| download | go-website-fe9ec7a0b45c9fd23a615a8b95ade3e9c1ea2d12.tar.gz go-website-fe9ec7a0b45c9fd23a615a8b95ade3e9c1ea2d12.tar.xz | |
Another re-structure. Deleting code is wonderful.v0.0.12
Diffstat (limited to 'page/index.go')
| -rw-r--r-- | page/index.go | 78 |
1 files changed, 78 insertions, 0 deletions
diff --git a/page/index.go b/page/index.go new file mode 100644 index 0000000..a453335 --- /dev/null +++ b/page/index.go @@ -0,0 +1,78 @@ +package page + +import ( + "fmt" + "os" + "path/filepath" + "strings" + "sync" + "time" +) + +var index map[string]PageList +var indexMu sync.RWMutex + +// RebuildIndex can be called in order to rebuild the entire website +// index +func (p *Page) RebuildIndex() error { + indexMu.Lock() + index = nil + indexMu.Unlock() + _, err := p.Index() + return err +} + +// Index returns a map of all pages in the current directory seperated into +// their respective tags If a Page has multiple tags it will be listed under +// each. +// Pages are located by their Suffix, default being ".md" +func (p *Page) Index() (map[string]PageList, error) { + indexMu.RLock() + if index != nil && CacheIndex { + indexMu.RUnlock() + return index, nil + } + indexMu.RUnlock() + Logger.Println("Rebuilding index...") + + out := make(map[string]PageList) + + filepath.Walk(filepath.Dir("."), + func(path string, info os.FileInfo, err error) error { + if err != nil { + return err + } + + if !info.IsDir() && strings.HasSuffix(info.Name(), Suffix) { + + p2 := NewPage(strings.ReplaceAll(path, Suffix, "")) + err = p2.Read() + + if err != nil { + 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 + }) + + indexMu.Lock() + index = out + indexMu.Unlock() + + return out, nil +} + +func (p *Page) Time() time.Time { + return p.Date.Time +} |
