Implement support for ugly URLs

This commit is contained in:
adnano 2021-05-10 00:44:25 -04:00
parent ce0b2cff1e
commit d7567ca69f
2 changed files with 17 additions and 15 deletions

View file

@ -4,7 +4,6 @@ import (
"fmt" "fmt"
"html/template" "html/template"
"os" "os"
"path"
"strings" "strings"
"github.com/BurntSushi/toml" "github.com/BurntSushi/toml"
@ -12,11 +11,11 @@ import (
// Config contains site configuration. // Config contains site configuration.
type Config struct { type Config struct {
Title string Title string `toml:"title"`
URLs []string URLs []string `toml:"urls"`
Feeds map[string]string Feeds map[string]string `toml:"feeds"`
Tasks map[string]*Task Tasks map[string]*Task `toml:"tasks"`
Permalinks map[string]string Permalinks map[string]string `toml:"permalinks"`
templates *Templates templates *Templates
} }
@ -29,10 +28,7 @@ type Task struct {
PostProcess string `toml:"postprocess"` // postprocess command PostProcess string `toml:"postprocess"` // postprocess command
StaticDir string `toml:"static_dir"` // static file directory StaticDir string `toml:"static_dir"` // static file directory
OutputDir string `toml:"output_dir"` // output directory OutputDir string `toml:"output_dir"` // output directory
} UglyURLs bool `toml:"ugly_urls"` // whether to use ugly URLs
func (t Task) OutputPath(pagePath string) string {
return path.Join(pagePath, "index"+t.OutputExt)
} }
// LoadConfig loads the configuration from the provided path. // 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 page.Path = d.Path
d.index = page d.index = page
} else { } else {
// Remove extension from path path = "/" + strings.TrimSuffix(path, task.InputExt)
// TODO: Allow using ugly URLs if task.UglyURLs {
path = strings.TrimSuffix(path, pathpkg.Ext(path)) path += task.OutputExt
} else {
path += "/"
}
page.Name = pathpkg.Base(path) page.Name = pathpkg.Base(path)
page.Path = "/" + path + "/" page.Path = path
d.Pages = append(d.Pages, page) d.Pages = append(d.Pages, page)
} }
} }
@ -216,7 +219,10 @@ func (d *Dir) Write(dstDir string, task *Task) error {
pages = append(pages, d.index) pages = append(pages, d.index)
} }
for _, page := range pages { 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 var content []byte
if cmd := task.PostProcess; cmd != "" { if cmd := task.PostProcess; cmd != "" {
content = RunProcessCmd(cmd, strings.NewReader(page.Content)) content = RunProcessCmd(cmd, strings.NewReader(page.Content))