mirror of
https://git.sr.ht/~adnano/kiln
synced 2024-10-30 01:13:08 +00:00
67 lines
1.4 KiB
Go
67 lines
1.4 KiB
Go
package main
|
|
|
|
import (
|
|
"path/filepath"
|
|
"strings"
|
|
"text/template"
|
|
)
|
|
|
|
type Manipulator struct {
|
|
Pages map[string]*Page
|
|
|
|
// Templates
|
|
Templates *template.Template
|
|
}
|
|
|
|
func NewManipulator() (*Manipulator, error) {
|
|
tmpl, err := template.New("templates").ParseGlob("templates/*.gmi")
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &Manipulator{
|
|
Pages: map[string]*Page{},
|
|
Templates: tmpl,
|
|
}, nil
|
|
}
|
|
|
|
func (m *Manipulator) Manipulate(index *Index) error {
|
|
for path := range index.Files {
|
|
switch filepath.Ext(path) {
|
|
case ".gmi":
|
|
// Gather page data
|
|
content := string(index.Files[path])
|
|
page := NewPage(path, content)
|
|
|
|
builder := &strings.Builder{}
|
|
tmpl := m.Templates.Lookup("page.gmi")
|
|
if err := tmpl.Execute(builder, page); err != nil {
|
|
return err
|
|
}
|
|
page.Content = builder.String()
|
|
m.Pages[path] = page
|
|
index.Files[path] = []byte(page.Content)
|
|
}
|
|
}
|
|
|
|
for dir := range index.Dirs {
|
|
// Load section data
|
|
section := NewSection(dir)
|
|
for _, path := range index.Dirs[dir] {
|
|
switch filepath.Ext(path) {
|
|
case ".gmi":
|
|
section.Pages = append(section.Pages, m.Pages[path])
|
|
}
|
|
}
|
|
|
|
dstPath := filepath.Join(dir, "index.gmi")
|
|
builder := &strings.Builder{}
|
|
tmpl := m.Templates.Lookup("section.gmi")
|
|
if err := tmpl.Execute(builder, section); err != nil {
|
|
return err
|
|
}
|
|
index.Files[dstPath] = []byte(builder.String())
|
|
}
|
|
|
|
return nil
|
|
}
|