From 235b8f871fdfa35f9595268d194d28a3de655ec0 Mon Sep 17 00:00:00 2001 From: Mitchell Riedstra Date: Sun, 24 Oct 2021 15:57:32 -0400 Subject: Unix sockets for redis. Page use FS interface. Clear redis func. Additionally, Funcs can be passed in. --- page/checkup.go | 7 +++---- page/index.go | 7 +++---- page/page.go | 29 ++++++++++++++++++++++++----- 3 files changed, 30 insertions(+), 13 deletions(-) (limited to 'page') diff --git a/page/checkup.go b/page/checkup.go index 9f721b7..6c3664f 100644 --- a/page/checkup.go +++ b/page/checkup.go @@ -2,8 +2,7 @@ package page import ( "fmt" - "os" - "path/filepath" + "io/fs" "reflect" "strings" ) @@ -15,8 +14,8 @@ func (p *Page) Checkup() (map[string]PageList, error) { out := make(map[string]PageList) - _ = filepath.Walk(filepath.Dir("."), - func(path string, info os.FileInfo, err error) error { + _ = fs.WalkDir(FileSystem, ".", + func(path string, info fs.DirEntry, err error) error { if err != nil { return err } diff --git a/page/index.go b/page/index.go index 425bf04..00cab4b 100644 --- a/page/index.go +++ b/page/index.go @@ -1,8 +1,7 @@ package page import ( - "os" - "path/filepath" + "io/fs" "strings" "sync" "time" @@ -41,8 +40,8 @@ func (p *Page) Index() (map[string]PageList, error) { out := make(map[string]PageList) - _ = filepath.Walk(filepath.Dir("."), - func(path string, info os.FileInfo, err error) error { + _ = fs.WalkDir(FileSystem, ".", + func(path string, info fs.DirEntry, err error) error { if err != nil { return err } diff --git a/page/page.go b/page/page.go index fa4ce6c..b2cc3e1 100644 --- a/page/page.go +++ b/page/page.go @@ -6,6 +6,8 @@ // markdown, first executed as part of the text/template then rendered by // blackfriday. // +// This package is designed to only be run with ***TRUSTED INPUT*** +// // Usage: // // import ( @@ -28,6 +30,7 @@ import ( "errors" "fmt" "io" + "io/fs" "log" "os" "path/filepath" @@ -61,10 +64,18 @@ type Page struct { // .Global care must be taken when utilizing this functionality. var Global interface{} +// Funcs accessible to the templates. +var Funcs template.FuncMap + // CacheIndex determines whether or not the index will be cached in memory // or rebuilt on each call. var CacheIndex = true +// FileSystem can be replaced with anything that implements the fs.FS interface +// and the website should be able to continue working with a different +// backend. +var FileSystem = os.DirFS(".") + // BaseTemplate can be adjusted to change the base template used in rendering. var BaseTemplate = "inc/base.html" @@ -108,11 +119,18 @@ func (p *Page) Render(wr io.Writer) error { return err } - t, err := template.ParseFiles(BaseTemplate) + templateContent, err := fs.ReadFile(FileSystem, BaseTemplate) if err != nil { - return fmt.Errorf("rendering: %w", err) + return fmt.Errorf("reading base template: %w", err) } + t, err := template.New("baseTemplate").Funcs(Funcs).Parse(string(templateContent)) + if err != nil { + return fmt.Errorf("parsing: %w", err) + } + + t = t.Funcs(Funcs) + return t.Execute(wr, p) } @@ -121,7 +139,7 @@ func (p *Page) Read() error { yamlBuf := bytes.NewBuffer(nil) markdownBuf := bytes.NewBuffer(nil) - fh, err := os.Open(p.path + Suffix) + fh, err := FileSystem.Open(p.path + Suffix) if err != nil { return fmt.Errorf("opening markdown: %w", err) } @@ -171,11 +189,12 @@ func (p *Page) Read() error { } // RenderBody renders and executes a template from the body of the -// markdown file, then runs it through the markdown parser. +// markdown file, then runs it through the markdown parser. Typically +// this is called in the base template. func (p *Page) RenderBody() (string, error) { buf := &bytes.Buffer{} - t, err := template.New("Body").Parse(string(p.markdown)) + t, err := template.New("Body").Funcs(Funcs).Parse(string(p.markdown)) if err != nil { return "", fmt.Errorf("render body: %w", err) } -- cgit v1.2.3