Provide index file metadata to index templates

This commit is contained in:
adnano 2020-11-20 15:03:41 -05:00
parent f14999d88a
commit afacbfafa1
2 changed files with 27 additions and 15 deletions

25
dir.go
View file

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

View file

@ -93,6 +93,10 @@ Index templates are provided with the following information:
[[ *Variable* [[ *Variable*
:[ *Description* :[ *Description*
| Title
: Title of the directory
| Content
: The contents of the directory index file
| Path | Path
: Path to the directory : Path to the directory
| Pages | Pages
@ -100,6 +104,9 @@ Index templates are provided with the following information:
| Dirs | Dirs
: List of subdirectories : 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
Feed templates are provided with the following information: Feed templates are provided with the following information: