Implement support for ugly URLs

This commit is contained in:
Adnan Maolood 2021-05-10 00:44:25 -04:00
parent c285fe4ff8
commit ebd42737c9
2 changed files with 17 additions and 15 deletions

View file

@ -4,7 +4,6 @@ import (
"fmt"
"html/template"
"os"
"path"
"strings"
"github.com/BurntSushi/toml"
@ -12,11 +11,11 @@ import (
// Config contains site configuration.
type Config struct {
Title string
URLs []string
Feeds map[string]string
Tasks map[string]*Task
Permalinks map[string]string
Title string `toml:"title"`
URLs []string `toml:"urls"`
Feeds map[string]string `toml:"feeds"`
Tasks map[string]*Task `toml:"tasks"`
Permalinks map[string]string `toml:"permalinks"`
templates *Templates
}
@ -29,10 +28,7 @@ type Task struct {
PostProcess string `toml:"postprocess"` // postprocess command
StaticDir string `toml:"static_dir"` // static file directory
OutputDir string `toml:"output_dir"` // output directory
}
func (t Task) OutputPath(pagePath string) string {
return path.Join(pagePath, "index"+t.OutputExt)
UglyURLs bool `toml:"ugly_urls"` // whether to use ugly URLs
}
// LoadConfig loads the configuration from the provided path.

16
dir.go
View file

@ -125,11 +125,14 @@ func (d *Dir) read(srcDir, path string, task *Task) error {
page.Path = d.Path
d.index = page
} else {
// Remove extension from path
// TODO: Allow using ugly URLs
path = strings.TrimSuffix(path, pathpkg.Ext(path))
path = "/" + strings.TrimSuffix(path, task.InputExt)
if task.UglyURLs {
path += task.OutputExt
} else {
path += "/"
}
page.Name = pathpkg.Base(path)
page.Path = "/" + path + "/"
page.Path = path
d.Pages = append(d.Pages, page)
}
}
@ -216,7 +219,10 @@ func (d *Dir) Write(dstDir string, task *Task) error {
pages = append(pages, d.index)
}
for _, page := range pages {
path := task.OutputPath(page.Path)
path := page.Path
if !task.UglyURLs || page == d.index {
path = pathpkg.Join(path, "index"+task.OutputExt)
}
var content []byte
if cmd := task.PostProcess; cmd != "" {
content = RunProcessCmd(cmd, strings.NewReader(page.Content))