diff options
| author | Mitch Riedstra <mitch@riedstra.us> | 2020-01-04 23:58:30 -0500 |
|---|---|---|
| committer | Mitch Riedstra <mitch@riedstra.us> | 2020-01-04 23:58:36 -0500 |
| commit | 839b8f3ef95c1aae4cf55df2d9e77a411107d46b (patch) | |
| tree | decf9d64013ff4e2d4ebdf857ed66c09d2366b26 | |
| parent | c79b18d5a7d3b47ea5db495b4d6a6faf0b7dbb11 (diff) | |
| download | go-website-839b8f3ef95c1aae4cf55df2d9e77a411107d46b.tar.gz go-website-839b8f3ef95c1aae4cf55df2d9e77a411107d46b.tar.xz | |
Incomptiable change to move to the further refined website format
| -rw-r--r-- | go.mod | 2 | ||||
| -rw-r--r-- | go.sum | 7 | ||||
| -rw-r--r-- | page/page.go | 64 |
3 files changed, 55 insertions, 18 deletions
@@ -3,7 +3,9 @@ module git.riedstra.us/mitch/go-website go 1.13 require ( + github.com/kr/pretty v0.1.0 // indirect 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 ) @@ -1,7 +1,14 @@ +github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI= +github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= +github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= +github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= +github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/shurcooL/sanitized_anchor_name v1.0.0 h1:PdmoCO6wvbs+7yrJyMORt4/BmY5IYyJwS/kOiWx8mHo= github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= 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= diff --git a/page/page.go b/page/page.go index f5a296c..47e64e3 100644 --- a/page/page.go +++ b/page/page.go @@ -1,9 +1,10 @@ package page import ( - "fmt" + "bufio" + "bytes" "io" - "io/ioutil" + "os" "text/template" "time" @@ -17,16 +18,18 @@ type Page struct { Body string Date *time.Time Published bool + Templates map[string]string } // Can be adjusted to change the base template used in rendering var BaseTemplate = "inc/base.html" +// Used to split the .md files into yaml and markdown +var DocumentSplit = "|---\n" + +// Renders a page func (p *Page) Render(wr io.Writer) error { - if err := p.readYaml(); err != nil { - return err - } - if err := p.readMarkdown(); err != nil { + if err := p.Read(); err != nil { return err } @@ -50,22 +53,47 @@ func (p *Page) Render(wr io.Writer) error { return t.Execute(wr, p) } -func (p *Page) readYaml() error { - fname := p.Name + ".yml" - b, err := ioutil.ReadFile(fname) +// 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") if err != nil { - return fmt.Errorf("While unmarshaling file '%s': %v", fname, err) + return err } - return yaml.Unmarshal(b, p) -} + defer fh.Close() + rdr := bufio.NewReader(fh) + + // Read in the file and split between markdown and yaml buffers + yamlDone := false + for { -func (p *Page) readMarkdown() error { - pth := p.Name + ".md" - b, err := ioutil.ReadFile(pth) + bytes, err := rdr.ReadBytes('\n') + if err == io.EOF { + break + } else if err != nil { + return err + } + + // Is this the line where we stop reading the yaml and start reading markdown? + if DocumentSplit == string(bytes) && !yamlDone { + yamlDone = true + continue + } + + if !yamlDone { + yamlBuf.Write(bytes) + } else { + markdownBuf.Write(bytes) + } + } + + err = yaml.Unmarshal(yamlBuf.Bytes(), p) if err != nil { - return fmt.Errorf("Error while reading markdown for path %s: %v", - pth, err) + return err } - p.Body = string(blackfriday.Run(b)) + + p.Body = string(blackfriday.Run(markdownBuf.Bytes())) return nil } |
