From 839b8f3ef95c1aae4cf55df2d9e77a411107d46b Mon Sep 17 00:00:00 2001 From: Mitch Riedstra Date: Sat, 4 Jan 2020 23:58:30 -0500 Subject: Incomptiable change to move to the further refined website format --- page/page.go | 64 +++++++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 46 insertions(+), 18 deletions(-) (limited to 'page') 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 } -- cgit v1.2.3