diff options
| author | Mitch Riedstra <mitch@riedstra.us> | 2020-09-19 01:49:50 -0400 |
|---|---|---|
| committer | Mitch Riedstra <mitch@riedstra.us> | 2020-09-19 01:52:49 -0400 |
| commit | 3b94493a21f227962a01ec0b11fd855178985604 (patch) | |
| tree | 6fbf6e17667c8848e93031aba1638e897b56eb85 /page/page.go | |
| parent | 9b849e00766b1dd0dfc9d603c18c90be3493b97b (diff) | |
| download | go-website-3b94493a21f227962a01ec0b11fd855178985604.tar.gz go-website-3b94493a21f227962a01ec0b11fd855178985604.tar.xz | |
Allow for setting of the time format in the page library. Support tags. Rename Name to Path to better reflect what it is.v0.0.6
Diffstat (limited to 'page/page.go')
| -rw-r--r-- | page/page.go | 83 |
1 files changed, 69 insertions, 14 deletions
diff --git a/page/page.go b/page/page.go index 3c6a130..6e1d7d5 100644 --- a/page/page.go +++ b/page/page.go @@ -6,20 +6,24 @@ import ( "fmt" "io" "os" + "path/filepath" + "strings" "text/template" - "time" "github.com/russross/blackfriday" - "gopkg.in/yaml.v2" + "gopkg.in/yaml.v3" ) type Page struct { - Name string - Head string - Date *time.Time - Published bool - Vars map[string]interface{} - Markdown []byte + Path string + Title string + Head string + Description string + Tags map[string]interface{} + Date *PageTime + Published bool + Vars map[string]interface{} + Markdown []byte } // Can be adjusted to change the base template used in rendering @@ -43,7 +47,7 @@ func (p *Page) Render(wr io.Writer) error { if p.Head != "" { t, err = t.Parse(` {{define "head"}} - {{.Head}} + {{.RenderHead}} {{end}} `) if err != nil { @@ -54,12 +58,25 @@ func (p *Page) Render(wr io.Writer) error { return t.Execute(wr, p) } +func (p *Page) RenderHead() (string, error) { + buf := &bytes.Buffer{} + t, err := template.New("Head").Parse(p.Head) + if err != nil { + return "", err + } + err = t.Execute(buf, p) + if err != nil { + return "", err + } + return string(buf.Bytes()), nil +} + // Reads in the special markdown file format for the website off of the disk func (p *Page) Read() error { yamlBuf := bytes.NewBuffer(nil) markdownBuf := bytes.NewBuffer(nil) - fh, err := os.Open(p.Name + ".md") + fh, err := os.Open(p.Path + ".md") if err != nil { return err } @@ -95,7 +112,6 @@ func (p *Page) Read() error { return err } - // p.Body = string(blackfriday.Run(markdownBuf.Bytes())) p.Markdown = markdownBuf.Bytes() return nil } @@ -113,10 +129,49 @@ func (p *Page) RenderBody() (string, error) { } return string(blackfriday.Run(buf.Bytes())), nil - - // return buf.String(), nil } func (p Page) String() string { - return fmt.Sprintf("Page: %s", p.Name) + return fmt.Sprintf("Page: %s", p.Path) +} + +// 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) { + 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 + }) + + return out, nil } |
