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 | |
| 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
| -rw-r--r-- | LICENSE | 2 | ||||
| -rwxr-xr-x | build.sh | 4 | ||||
| -rw-r--r-- | go.mod | 2 | ||||
| -rw-r--r-- | go.sum | 4 | ||||
| -rw-r--r-- | page/http.go | 6 | ||||
| -rw-r--r-- | page/page.go | 83 | ||||
| -rw-r--r-- | page/pagelist.go | 19 | ||||
| -rw-r--r-- | page/time.go | 21 |
8 files changed, 119 insertions, 22 deletions
@@ -1,4 +1,4 @@ -Copyright 2019 Mitchell Riedstra +Copyright 2020 Mitchell Riedstra Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above @@ -5,7 +5,7 @@ LICENSE="$(cat LICENSE)" version="$(git log --format="%h %d" -1) Source code can be found here: -https://git.riedstra.us/mitch/go-website.git +https://git.riedstra.dev/mitch/go-website $LICENSE" @@ -13,4 +13,6 @@ if ! git diff-index --quiet HEAD ; then version="dirty: $version" fi +export CGO_ENABLED=0 + go build -ldflags="-X 'main.VersionString=$version'" ./cmd/server @@ -7,6 +7,6 @@ require ( github.com/russross/blackfriday v2.0.0+incompatible github.com/shurcooL/sanitized_anchor_name v1.0.0 // indirect gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect - gopkg.in/russross/blackfriday.v2 v2.0.0 gopkg.in/yaml.v2 v2.2.7 + gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776 ) @@ -11,7 +11,7 @@ gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+ gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo= gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/russross/blackfriday.v2 v2.0.0 h1:+FlnIV8DSQnT7NZ43hcVKcdJdzZoeCmJj4Ql8gq5keA= -gopkg.in/russross/blackfriday.v2 v2.0.0/go.mod h1:6sSBNz/GtOm/pJTuh5UmBK2ZHfmnxGbl2NZg1UliSOI= gopkg.in/yaml.v2 v2.2.7 h1:VUgggvou5XRW9mHwD/yXxIYSMtY0zoKQf/v226p2nyo= gopkg.in/yaml.v2 v2.2.7/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776 h1:tQIYjPdBoyREyB9XMu+nnTclpTYkz2zFM+lzLJFO4gQ= +gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/page/http.go b/page/http.go index 98546f6..7e1d780 100644 --- a/page/http.go +++ b/page/http.go @@ -20,12 +20,12 @@ func PageHandler(w http.ResponseWriter, r *http.Request) { u = filepath.Join(".", u) log.Println(u) - p := &Page{Name: u} + p := &Page{Path: u} err := p.Render(w) if err != nil { if strings.HasSuffix(err.Error(), "no such file or directory") { - log.Printf("Page '%s' not found, trying 404", p.Name) - p.Name = "404" + log.Printf("Page '%s' not found, trying 404", p.Path) + p.Path = "404" w.WriteHeader(404) err := p.Render(w) if err != nil { 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 } diff --git a/page/pagelist.go b/page/pagelist.go new file mode 100644 index 0000000..e326ac9 --- /dev/null +++ b/page/pagelist.go @@ -0,0 +1,19 @@ +package page + +import "sort" + +type PageList []*Page + +func (p PageList) SortDate() PageList { + sort.Slice(p, func(i, j int) bool { + return p[i].Date.Time.After(p[j].Date.Time) + }) + return p +} + +func (p PageList) SortDateReverse() PageList { + sort.Slice(p, func(i, j int) bool { + return p[i].Date.Time.Before(p[j].Date.Time) + }) + return p +} diff --git a/page/time.go b/page/time.go new file mode 100644 index 0000000..0ac29ab --- /dev/null +++ b/page/time.go @@ -0,0 +1,21 @@ +package page + +import ( + "gopkg.in/yaml.v3" + "time" +) + +type PageTime struct { + time.Time +} + +var TimeFormat = "01.02.2006 15:04:05 MST" + +func (pt *PageTime) UnmarshalYAML(n *yaml.Node) error { + t, err := time.Parse(TimeFormat, n.Value) + if err != nil { + return err + } + pt.Time = t + return nil +} |
