aboutsummaryrefslogtreecommitdiff
path: root/page
diff options
context:
space:
mode:
authorMitch Riedstra <mitch@riedstra.us>2019-12-01 14:54:54 -0500
committerMitch Riedstra <mitch@riedstra.us>2019-12-01 14:54:54 -0500
commitc19536be6e4f8733af329204a50ffc254a679ee5 (patch)
tree0b464d70bbd1631c42c904ccf275a9aee50480d1 /page
downloadgo-website-c19536be6e4f8733af329204a50ffc254a679ee5.tar.gz
go-website-c19536be6e4f8733af329204a50ffc254a679ee5.tar.xz
Initial Commit. Small command to convert my old setup and basic server that reads off of the disk for the new setup
Diffstat (limited to 'page')
-rw-r--r--page/page.go69
1 files changed, 69 insertions, 0 deletions
diff --git a/page/page.go b/page/page.go
new file mode 100644
index 0000000..83e3c9e
--- /dev/null
+++ b/page/page.go
@@ -0,0 +1,69 @@
+package page
+
+import (
+ "fmt"
+ "io"
+ "io/ioutil"
+ "text/template"
+ "time"
+
+ "gopkg.in/russross/blackfriday.v2"
+ "gopkg.in/yaml.v2"
+)
+
+type Page struct {
+ Name string
+ Head string
+ Body string
+ Date *time.Time
+ Published bool
+}
+
+// Can be adjusted to change the base template used in rendering
+var BaseTemplate = "inc/base.html"
+
+func (p *Page) Render(wr io.Writer) error {
+ if err := p.readYaml(); err != nil {
+ return err
+ }
+ if err := p.readMarkdown(); err != nil {
+ return err
+ }
+
+ t, err := template.ParseFiles(BaseTemplate)
+ if err != nil {
+ return err
+ }
+
+ // Automatically pull from the yml file if applicable
+ if p.Head != "" {
+ t, err = t.Parse(`
+ {{define "head"}}
+ {{.Head}}
+ {{end}}
+ `)
+ if err != nil {
+ return err
+ }
+ }
+
+ return t.Execute(wr, p)
+}
+
+func (p *Page) readYaml() error {
+ fname := p.Name + ".yml"
+ b, err := ioutil.ReadFile(fname)
+ if err != nil {
+ return fmt.Errorf("While unmarshaling file '%s': %v", fname, err)
+ }
+ return yaml.Unmarshal(b, p)
+}
+
+func (p *Page) readMarkdown() error {
+ b, err := ioutil.ReadFile(p.Name + ".md")
+ if err != nil {
+ return err
+ }
+ p.Body = string(blackfriday.Run(b))
+ return nil
+}