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 /page | |
| parent | c79b18d5a7d3b47ea5db495b4d6a6faf0b7dbb11 (diff) | |
| download | go-website-839b8f3ef95c1aae4cf55df2d9e77a411107d46b.tar.gz go-website-839b8f3ef95c1aae4cf55df2d9e77a411107d46b.tar.xz | |
Incomptiable change to move to the further refined website format
Diffstat (limited to 'page')
| -rw-r--r-- | page/page.go | 64 |
1 files changed, 46 insertions, 18 deletions
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 } |
