Provide index file metadata to index templates

This commit is contained in:
Adnan Maolood 2020-11-20 15:03:41 -05:00
parent 804de9b94d
commit b4c3d7f442
2 changed files with 27 additions and 15 deletions

25
dir.go
View file

@ -11,6 +11,8 @@ import (
// Dir represents a directory.
type Dir struct {
Title string // Directory title.
Content string // Directory index content.
Path string // Directory path.
Pages []*Page // Pages in this directory.
Dirs []*Dir // Subdirectories.
@ -59,11 +61,12 @@ func (d *Dir) read(srcDir string, path string) error {
}
if pathpkg.Ext(name) == ".gmi" {
// Gather page data
page := NewPage(path, content)
if name == "index.gmi" {
d.index = page
d.index = NewPage(d.Path, content)
d.Title = d.index.Title
d.Content = d.index.Content
} else {
d.Pages = append(d.Pages, page)
d.Pages = append(d.Pages, NewPage(path, content))
}
} else {
// Static file
@ -76,17 +79,14 @@ func (d *Dir) read(srcDir string, path string) error {
// manipulate processes and manipulates the directory's contents.
func (d *Dir) manipulate(cfg *Config) error {
// Create the directory index file, if it doesn't exist
if d.index == nil {
// Create index
if d.index != nil {
var b strings.Builder
tmpl := cfg.Templates.FindTemplate(d.Path, "index.gmi")
if err := tmpl.Execute(&b, d); err != nil {
return err
}
d.index = &Page{
Path: d.Path,
Content: b.String(),
}
d.index.Content = b.String()
}
// Manipulate pages
@ -102,6 +102,7 @@ func (d *Dir) manipulate(cfg *Config) error {
// Feed represents a feed.
type Feed struct {
Title string // Feed title.
Path string // Feed path.
Updated time.Time // Last updated time.
Entries []*Page // Feed entries.
}
@ -111,6 +112,7 @@ func (d *Dir) manipulate(cfg *Config) error {
var b strings.Builder
feed := &Feed{
Title: title,
Path: d.Path,
Updated: time.Now(),
Entries: d.Pages,
}
@ -119,6 +121,7 @@ func (d *Dir) manipulate(cfg *Config) error {
return err
}
d.Pages = append(d.Pages, &Page{
Title: title,
Path: pathpkg.Join(d.Path, "feed"),
Content: b.String(),
})
@ -154,7 +157,7 @@ func (d *Dir) write(dstDir string, format outputFormat, cfg *Config) error {
}
}
// Write the pages
// Write pages
for _, page := range d.Pages {
path, content := format(page, cfg)
dstPath := pathpkg.Join(dstDir, path)
@ -173,6 +176,8 @@ func (d *Dir) write(dstDir string, format outputFormat, cfg *Config) error {
if d.index != nil {
path, content := format(d.index, cfg)
dstPath := pathpkg.Join(dstDir, path)
dir := pathpkg.Dir(dstPath)
os.MkdirAll(dir, 0755)
f, err := os.Create(dstPath)
if err != nil {
return err

View file

@ -93,6 +93,10 @@ Index templates are provided with the following information:
[[ *Variable*
:[ *Description*
| Title
: Title of the directory
| Content
: The contents of the directory index file
| Path
: Path to the directory
| Pages
@ -100,6 +104,9 @@ Index templates are provided with the following information:
| Dirs
: List of subdirectories
The title and content are taken from the index.gmi file in the directory.
If no index.gmi file exists, then the index template will not be rendered.
## FEED TEMPLATES
Feed templates are provided with the following information: