Skip to content

Commit

Permalink
Add support for rendering extra files.
Browse files Browse the repository at this point in the history
  • Loading branch information
roblillack committed Jan 6, 2022
1 parent 5e7fa1e commit c6dc263
Showing 1 changed file with 39 additions and 11 deletions.
50 changes: 39 additions & 11 deletions core/page.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,17 +259,19 @@ func (p *Page) Generate() error {

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

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

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

for i := range p.Assets {
p.Tacker.Log("Copying ...%s", i)
p.Tacker.Log(" - copying %s", i)
if err := os.MkdirAll(filepath.Dir(filepath.Join(destDir, i)), 0755); err != nil {
return err
}
Expand All @@ -300,5 +302,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.Log(" - 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
}

0 comments on commit c6dc263

Please sign in to comment.