aboutsummaryrefslogtreecommitdiff
path: root/page/page.go
diff options
context:
space:
mode:
authorMitchell Riedstra <mitch@riedstra.dev>2021-10-24 15:57:32 -0400
committerMitchell Riedstra <mitch@riedstra.dev>2021-10-24 16:01:05 -0400
commit235b8f871fdfa35f9595268d194d28a3de655ec0 (patch)
tree46f562fddffd38ee10d5e3d858dd80c088879689 /page/page.go
parente0d4a3e50921dc07e23ef9aa107bdc78b3adf176 (diff)
downloadgo-website-235b8f871fdfa35f9595268d194d28a3de655ec0.tar.gz
go-website-235b8f871fdfa35f9595268d194d28a3de655ec0.tar.xz
Unix sockets for redis. Page use FS interface. Clear redis func.v0.0.16
Additionally, Funcs can be passed in.
Diffstat (limited to 'page/page.go')
-rw-r--r--page/page.go29
1 files changed, 24 insertions, 5 deletions
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)
}