Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Add support for rendering extra files for individual pages #30

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 39 additions & 11 deletions core/page.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,17 +286,19 @@ func (p *Page) Generate() error {

destDir := filepath.Join(append([]string{p.Tacker.BaseDir, TargetDir}, p.TargetDir()...)...)

p.Tacker.Debug("Generating %s", p.Slug)
par := "-"
if p.Parent != nil {
par = p.Parent.DiskPath
if p.DiskPath != "" {
p.Tacker.Debug("Generating %s", p.Slug)
par := "-"
if p.Parent != nil {
par = p.Parent.DiskPath
}
p.Tacker.Debug(" - disk path: %s", p.DiskPath)
p.Tacker.Debug(" - parent: %s", par)
p.Tacker.Debug(" - permalink: %s", p.Permalink())
p.Tacker.Debug(" - destdir: %s", destDir)
p.Tacker.Debug(" - ancestors: %s", strings.Join(a, " << "))
p.Tacker.Debug(" - siblings: %s", strings.Join(s, ", "))
}
p.Tacker.Debug(" - disk path: %s", p.DiskPath)
p.Tacker.Debug(" - parent: %s", par)
p.Tacker.Debug(" - permalink: %s", p.Permalink())
p.Tacker.Debug(" - destdir: %s", destDir)
p.Tacker.Debug(" - ancestors: %s", strings.Join(a, " << "))
p.Tacker.Debug(" - siblings: %s", strings.Join(s, ", "))

if err := os.MkdirAll(destDir, 0755); err != nil {
return err
Expand All @@ -318,7 +320,7 @@ func (p *Page) Generate() error {
}

for i := range p.Assets {
p.Tacker.Debug("Copying ...%s", i)
p.Tacker.Debug(" - copying %s", i)
if err := os.MkdirAll(filepath.Dir(filepath.Join(destDir, i)), 0755); err != nil {
return err
}
Expand All @@ -327,5 +329,31 @@ func (p *Page) Generate() error {
}
}

if dict, ok := p.Variables["extra_files"].(map[interface{}]interface{}); ok {
for k, v := range dict {
fn := k.(string)
templateName := v.(string)
if fn == "" || templateName == "" {
continue
}

p.Tacker.Debug(" - rendering %s", fn)
tpl, err := p.Tacker.FindTemplate(templateName)
if err != nil {
return fmt.Errorf("unable to load template '%s' for extra file '%s' when rendering '%s': %s", s, fn, p.Permalink(), err)
}

f, err := os.OpenFile(filepath.Join(destDir, fn), os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
if err != nil {
return err
}
defer f.Close()

if err := tpl.Render(p, f); err != nil {
return fmt.Errorf("unable to render template '%s' for extra file '%s' when rendering '%s': %s", s, fn, p.Permalink(), err)
}
}
}

return nil
}
5 changes: 4 additions & 1 deletion core/tacker.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"path/filepath"
"sort"
"strings"
"time"

"github.com/cbroglie/mustache"
yaml "gopkg.in/yaml.v2"
Expand Down Expand Up @@ -39,6 +40,7 @@ type Tacker struct {
Logger *log.Logger
DebugLogger *log.Logger
Strict bool
BuildTime time.Time
}

// NewTacker creates a new tack configuration structure based on the files
Expand Down Expand Up @@ -169,6 +171,8 @@ func (t *Tacker) Debug(format string, args ...interface{}) {
// Tack is the main “tacking” functionality: All pages are rendered into the
// output directory by filling the respective templates with the page content.
func (t *Tacker) Tack() error {
t.BuildTime = time.Now()

mustache.AllowMissingVariables = !t.Strict
strictModeOn := ""
if t.Strict {
Expand All @@ -186,7 +190,6 @@ func (t *Tacker) Tack() error {
}

for _, page := range t.Pages {
t.Debug("%s => %s (template: %s)", page.Permalink(), page.Slug, page.Template)
if err := page.Generate(); err != nil {
return err
}
Expand Down
8 changes: 8 additions & 0 deletions core/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@ import (
"io"
"sort"
"strings"
"time"

"github.com/cbroglie/mustache"
)

const RFC822 string = "02 Jan 2006 15:04:05 -0700"

type Template struct {
*mustache.Template
}
Expand All @@ -32,6 +35,8 @@ func PageValues(p *Page, ctx *Page) map[string]interface{} {
data["root"] = p.Root()
if p.Post() {
data["date"] = p.Date.Format("2006-01-02")
data["date_rfc822"] = p.Date.Format(RFC822)
data["date_rfc3339"] = p.Date.Format(time.RFC3339)
data["year"] = p.Date.Format("2006")
data["month"] = p.Date.Format("January")
}
Expand Down Expand Up @@ -137,6 +142,9 @@ func (t *Template) Render(page *Page, w io.Writer) error {
ctx["posts"] = PageListValues(limitPageList(posts, page, "posts_limit"), page)
}

ctx["TACK_BUILD_DATE"] = page.Tacker.BuildTime.Local().Format(time.RFC3339)
ctx["TACK_BUILD_DATE_RFC822"] = page.Tacker.BuildTime.Local().Format(RFC822)

return t.Template.FRender(w, ctx)
}

Expand Down