aboutsummaryrefslogtreecommitdiff
path: root/local/page.go
diff options
context:
space:
mode:
authorMitch Riedstra <mitch@riedstra.us>2020-11-24 23:20:54 -0500
committerMitch Riedstra <mitch@riedstra.us>2020-11-24 23:20:54 -0500
commitd83f4bca3f7026696a41225caac11807ed06fc2f (patch)
tree634552e0253407785f81cb28cf136fadec4d65e4 /local/page.go
parent2203a437cc7ba0ca087a47d6e99476ba5e09ae71 (diff)
downloadgo-website-0.0.11.tar.gz
go-website-0.0.11.tar.xz
Add more comments. Expand the interface. Allow templates to more easily be rendered with external variables.v0.0.11
Diffstat (limited to 'local/page.go')
-rw-r--r--local/page.go88
1 files changed, 74 insertions, 14 deletions
diff --git a/local/page.go b/local/page.go
index f514b4a..0ccaf2e 100644
--- a/local/page.go
+++ b/local/page.go
@@ -1,3 +1,25 @@
+// local 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
+// section is parsed as yaml to populate the Page struct. The second portion is
+// markdown, first executed as part of the text/template then rendered by
+// blackfriday.
+//
+// Usage:
+//
+// import (
+// "fmt"
+// "os"
+// site "riedstra.dev/mitch/go-website/local"
+// )
+// // Where some/path.md exists
+// p := site.NewPage("/some/path")
+// // Dump the rendered HTML to stdout
+// err := p.Render(os.Stdout)
+// if err != nil {
+// fmt.Fprintln(os.Stderr, err)
+// }
package local
import (
@@ -12,27 +34,65 @@ import (
"gopkg.in/yaml.v3"
)
+// Page should not be created directly as it will not set the interal path
+// properly. Use NewPage instead.
+//
+// The exported fields can be filled in the yaml at the top of a page and
+// utilized within.
type Page struct {
- Path string
+ path string
Title string
Head string
Description string
- Tags map[string]interface{}
- Date *PageTime
- Published bool
- Vars map[string]interface{}
- Markdown []byte
+ // Tags to apply to the page in question. Useful for Index()
+ 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
+// 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
+var Global interface{}
+
+// BaseTemplate 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
+// 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
var DocumentSplit = "|---\n"
-// Allow for the creation of a new page from the page
+// NewPage returns a page struct with the path populated
+func NewPage(pth string) *Page {
+ return &Page{path: pth}
+}
+
+// NewPage Allow for the creation of a new page from the current page, does
+// not inlcude any information about the current page.
func (p Page) NewPage(pth string) *Page {
- return &Page{Path: pth}
+ return NewPage(pth)
+}
+
+// Path gets the current path set on the struct for the page in question
+// Useful if you're say iterating across tags to print out a list of
+// relevant posts on a blog or so by topic.
+func (p Page) Path() string {
+ return p.path
+}
+
+// Global is specifically for use inside of a page markdown file or
+// in a base template. This simply returns the package Global variable
+func (p *Page) Global() interface{} {
+ return Global
+}
+
+// SetVars Will set to `nil` if provided
+func (p *Page) SetVars(vars map[string]interface{}) {
+ p.Vars = vars
}
// Renders a page
@@ -79,7 +139,7 @@ func (p *Page) Read() error {
yamlBuf := bytes.NewBuffer(nil)
markdownBuf := bytes.NewBuffer(nil)
- fh, err := os.Open(p.Path + ".md")
+ fh, err := os.Open(p.path + Suffix)
if err != nil {
return err
}
@@ -115,13 +175,13 @@ func (p *Page) Read() error {
return err
}
- p.Markdown = markdownBuf.Bytes()
+ p.markdown = markdownBuf.Bytes()
return nil
}
func (p *Page) RenderBody() (string, error) {
buf := &bytes.Buffer{}
- t, err := template.New("Body").Parse(string(p.Markdown))
+ t, err := template.New("Body").Parse(string(p.markdown))
if err != nil {
return "", err
}
@@ -135,5 +195,5 @@ func (p *Page) RenderBody() (string, error) {
}
func (p Page) String() string {
- return fmt.Sprintf("Page: %s", p.Path)
+ return fmt.Sprintf("Page: %s", p.path)
}