diff options
Diffstat (limited to 'page/page.go')
| -rw-r--r-- | page/page.go | 59 |
1 files changed, 37 insertions, 22 deletions
diff --git a/page/page.go b/page/page.go index c926bed..fa4ce6c 100644 --- a/page/page.go +++ b/page/page.go @@ -1,4 +1,4 @@ -// page implements the website backed by a local filesystem. +// Package page implements the website backed by a local filesystem. // // Reading the base template off the disk, then any markdown files which are // split into two sections by the DocumentSplit global variable. The first @@ -25,6 +25,7 @@ package page import ( "bufio" "bytes" + "errors" "fmt" "io" "log" @@ -56,27 +57,28 @@ type Page struct { } // Global is meant to be supplied by external users of this package to populate -// globally accessable information across all of the templates accessiable via -// .Global care must be taken when utilizing this functionality +// globally accessible information across all of the templates accessiable via +// .Global care must be taken when utilizing this functionality. var Global interface{} // CacheIndex determines whether or not the index will be cached in memory -// or rebuilt on each call +// or rebuilt on each call. var CacheIndex = true -// BaseTemplate can be adjusted to change the base template used in rendering +// BaseTemplate can be adjusted to change the base template used in rendering. var BaseTemplate = "inc/base.html" -// Suffix is applied to all pages for reading off of the disk +// Suffix is applied to all pages for reading off of the disk. var Suffix = ".md" -// DocumentSplit is used to split the .md files into yaml and markdown +// DocumentSplit is used to split the .md files into yaml and markdown. var DocumentSplit = "|---\n" -// Default logger -var Logger = log.New(os.Stderr, "", log.LstdFlags) +// Logger is the default logger used throughout this package, feel +// free to override. +var Logger = log.New(os.Stderr, "PAGE: ", log.LstdFlags) -// NewPage returns a page struct with the path populated +// NewPage returns a page struct with the path populated. func NewPage(pth string) *Page { return &Page{path: filepath.FromSlash(filepath.Clean(pth))} } @@ -95,12 +97,12 @@ func (p Page) Path() string { } // Global is specifically for use inside of a page markdown file or -// in a base template. This simply returns the package Global variable +// in a base template. This simply returns the package Global variable. func (p *Page) Global() interface{} { return Global } -// Renders a page +// Render a page. func (p *Page) Render(wr io.Writer) error { if err := p.Read(); err != nil { return err @@ -108,38 +110,46 @@ func (p *Page) Render(wr io.Writer) error { t, err := template.ParseFiles(BaseTemplate) if err != nil { - return err + return fmt.Errorf("rendering: %w", err) } return t.Execute(wr, p) } -// Reads in the special markdown file format for the website off of the disk +// Read 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.path + Suffix) if err != nil { - return err + return fmt.Errorf("opening markdown: %w", err) } - defer fh.Close() + + defer func() { + err := fh.Close() + if err != nil { + Logger.Println(err) + } + }() + rdr := bufio.NewReader(fh) // Read in the file and split between markdown and yaml buffers yamlDone := false - for { + for { bytes, err := rdr.ReadBytes('\n') - if err == io.EOF { + if errors.Is(err, io.EOF) { break } else if err != nil { - return err + return fmt.Errorf("reading markdown: %w", err) } // Is this the line where we stop reading the yaml and start reading markdown? if DocumentSplit == string(bytes) && !yamlDone { yamlDone = true + continue } @@ -152,23 +162,28 @@ func (p *Page) Read() error { err = yaml.Unmarshal(yamlBuf.Bytes(), p) if err != nil { - return err + return fmt.Errorf("reading yaml: %w", err) } p.markdown = markdownBuf.Bytes() + return nil } +// RenderBody renders and executes a template from the body of the +// markdown file, then runs it through the markdown parser. func (p *Page) RenderBody() (string, error) { buf := &bytes.Buffer{} + t, err := template.New("Body").Parse(string(p.markdown)) if err != nil { - return "", err + return "", fmt.Errorf("render body: %w", err) } err = t.Execute(buf, p) + if err != nil { - return "", err + return "", fmt.Errorf("template execute; %w", err) } return string(blackfriday.Run(buf.Bytes())), nil |
