Use _index for index pages

Pages with the name "index" will use the page template, and pages with
the name "_index" will use the index template.
This commit is contained in:
adnano 2021-05-12 15:20:14 -04:00
parent 5fc28c9b78
commit 540181648b

18
dir.go
View file

@ -60,12 +60,12 @@ func (d *Dir) _read(srcDir, path string, task *Task, cfg *Config) error {
} }
for _, entry := range entries { for _, entry := range entries {
name := entry.Name() name := entry.Name()
// Ignore names that start with "_" path := pathpkg.Join(path, name)
if entry.IsDir() {
// Ignore directories beginning with "_"
if strings.HasPrefix(name, "_") { if strings.HasPrefix(name, "_") {
continue continue
} }
path := pathpkg.Join(path, name)
if entry.IsDir() {
// Gather directory data // Gather directory data
dir := NewDir(path) dir := NewDir(path)
if err := dir._read(srcDir, path, task, cfg); err != nil { if err := dir._read(srcDir, path, task, cfg); err != nil {
@ -73,6 +73,12 @@ func (d *Dir) _read(srcDir, path string, task *Task, cfg *Config) error {
} }
d.Dirs = append(d.Dirs, dir) d.Dirs = append(d.Dirs, dir)
} else if ext := pathpkg.Ext(name); task.Match(ext) { } else if ext := pathpkg.Ext(name); task.Match(ext) {
// Ignore pages beginning with "_" with the exception of _index pages
namePrefix := strings.TrimSuffix(name, ext)
if strings.HasPrefix(name, "_") && namePrefix != "_index" {
continue
}
srcPath := pathpkg.Join(srcDir, path) srcPath := pathpkg.Join(srcDir, path)
content, err := ioutil.ReadFile(srcPath) content, err := ioutil.ReadFile(srcPath)
if err != nil { if err != nil {
@ -122,11 +128,15 @@ func (d *Dir) _read(srcDir, path string, task *Task, cfg *Config) error {
} }
page.Content = string(content) page.Content = string(content)
if strings.TrimSuffix(name, ext) == "index" { if namePrefix == "_index" {
page.Path = d.Path page.Path = d.Path
d.index = page d.index = page
} else {
if namePrefix == "index" {
path = "/" + strings.TrimSuffix(path, name)
} else { } else {
path = "/" + strings.TrimSuffix(path, ext) path = "/" + strings.TrimSuffix(path, ext)
}
if task.UglyURLs { if task.UglyURLs {
path += task.OutputExt path += task.OutputExt
} else { } else {