Rename Path fields to Permalink

This commit is contained in:
Adnan Maolood 2021-05-21 17:08:36 -04:00
parent 376237c000
commit b0b15146f9
6 changed files with 48 additions and 48 deletions

View file

@ -5,7 +5,7 @@ urls = ["gemini://example.com"]
"/" = "Example feed" "/" = "Example feed"
[permalinks] [permalinks]
"/" = '/{{ .Date.Format "2006/01/02" }}/{{ path.Base .Path }}/' "/" = "/{{ .Date.Format `2006/01/02` }}/{{ path.Base .Permalink }}/"
[[tasks]] [[tasks]]
input = [".gmi"] input = [".gmi"]

34
dir.go
View file

@ -18,7 +18,7 @@ import (
// Dir represents a directory. // Dir represents a directory.
type Dir struct { type Dir struct {
Path string Permalink string
Pages []*Page Pages []*Page
Dirs []*Dir Dirs []*Dir
index *Page // The index page. index *Page // The index page.
@ -30,7 +30,7 @@ type Page struct {
Title string Title string
Date time.Time Date time.Time
Weight int Weight int
Path string `yaml:"-"` Permalink string `yaml:"-"`
FilePath string `yaml:"-"` FilePath string `yaml:"-"`
Content string `yaml:"-"` Content string `yaml:"-"`
Params map[string]interface{} Params map[string]interface{}
@ -57,7 +57,7 @@ func (d *Dir) _read(srcDir, path string, task *Task, cfg *Site) error {
continue continue
} }
// Gather directory data // Gather directory data
dir := &Dir{Path: "/" + path + "/"} dir := &Dir{Permalink: "/" + path + "/"}
if err := dir._read(srcDir, path, task, cfg); err != nil { if err := dir._read(srcDir, path, task, cfg); err != nil {
return err return err
} }
@ -121,7 +121,7 @@ func (d *Dir) _read(srcDir, path string, task *Task, cfg *Site) error {
page.FilePath = path page.FilePath = path
if namePrefix == "_index" { if namePrefix == "_index" {
page.Path = d.Path page.Permalink = d.Permalink
d.index = page d.index = page
} else { } else {
if namePrefix == "index" { if namePrefix == "index" {
@ -134,11 +134,11 @@ func (d *Dir) _read(srcDir, path string, task *Task, cfg *Site) error {
path += "/" path += "/"
} }
} }
page.Path = path page.Permalink = path
if permalink, ok := cfg.permalinks[d.Path]; ok { if permalink, ok := cfg.permalinks[d.Permalink]; ok {
var b strings.Builder var b strings.Builder
permalink.Execute(&b, page) permalink.Execute(&b, page)
page.Path = b.String() page.Permalink = b.String()
} }
d.Pages = append(d.Pages, page) d.Pages = append(d.Pages, page)
} }
@ -152,7 +152,7 @@ func (d *Dir) process(cfg *Site, task *Task) error {
if task.TemplateExt != "" { if task.TemplateExt != "" {
// Create index // Create index
if d.index != nil { if d.index != nil {
tmpl, ok := cfg.templates.FindTemplate(d.Path, "index"+task.TemplateExt) tmpl, ok := cfg.templates.FindTemplate(d.Permalink, "index"+task.TemplateExt)
if ok { if ok {
var b strings.Builder var b strings.Builder
if err := tmpl.Execute(&b, d); err != nil { if err := tmpl.Execute(&b, d); err != nil {
@ -165,7 +165,7 @@ func (d *Dir) process(cfg *Site, task *Task) error {
// Process pages // Process pages
for i := range d.Pages { for i := range d.Pages {
var b strings.Builder var b strings.Builder
tmpl, ok := cfg.templates.FindTemplate(d.Path, "page"+task.TemplateExt) tmpl, ok := cfg.templates.FindTemplate(d.Permalink, "page"+task.TemplateExt)
if ok { if ok {
if err := tmpl.Execute(&b, d.Pages[i]); err != nil { if err := tmpl.Execute(&b, d.Pages[i]); err != nil {
return err return err
@ -178,21 +178,21 @@ func (d *Dir) process(cfg *Site, task *Task) 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. Permalink string // Feed permalink.
Updated time.Time // Last updated time. Updated time.Time // Last updated time.
Entries []*Page // Feed entries. Entries []*Page // Feed entries.
} }
// Create feeds // Create feeds
if title, ok := cfg.Feeds[d.Path]; ok { if title, ok := cfg.Feeds[d.Permalink]; ok {
var b bytes.Buffer var b bytes.Buffer
feed := &Feed{ feed := &Feed{
Title: title, Title: title,
Path: d.Path, Permalink: d.Permalink,
Updated: time.Now(), Updated: time.Now(),
Entries: d.Pages, Entries: d.Pages,
} }
tmpl, ok := cfg.templates.FindTemplate(d.Path, "atom.xml") tmpl, ok := cfg.templates.FindTemplate(d.Permalink, "atom.xml")
if ok { if ok {
if err := tmpl.Execute(&b, feed); err != nil { if err := tmpl.Execute(&b, feed); err != nil {
return err return err
@ -214,7 +214,7 @@ func (d *Dir) process(cfg *Site, task *Task) error {
// write writes the directory's contents to the provided destination path. // write writes the directory's contents to the provided destination path.
func (d *Dir) write(dstDir string, task *Task) error { func (d *Dir) write(dstDir string, task *Task) error {
dirPath := pathpkg.Join(dstDir, d.Path) dirPath := pathpkg.Join(dstDir, d.Permalink)
// Write pages // Write pages
pages := d.Pages pages := d.Pages
@ -222,7 +222,7 @@ 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 := page.Path path := page.Permalink
if !task.UglyURLs || page == d.index { if !task.UglyURLs || page == d.index {
path = pathpkg.Join(path, "index"+task.OutputExt) path = pathpkg.Join(path, "index"+task.OutputExt)
} }
@ -321,11 +321,11 @@ func (d *Dir) Params() map[string]interface{} {
func (d *Dir) getDir(path string) *Dir { func (d *Dir) getDir(path string) *Dir {
// XXX: This is inefficient // XXX: This is inefficient
if d.Path == path { if d.Permalink == path {
return d return d
} }
for _, dir := range d.Dirs { for _, dir := range d.Dirs {
if dir.Path == path { if dir.Permalink == path {
return dir return dir
} }
} }

View file

@ -214,7 +214,7 @@ content/blog/2021-05-12-hello-world.gmi will have a path of
``` ```
[permalinks] [permalinks]
"/blog/" = "/{{ .Date.Format `2006/01/02` }}/{{ path.Base .Path }}" "/blog/" = "/{{ .Date.Format `2006/01/02` }}/{{ path.Base .Permalink }}"
``` ```
For more information on templates, see *TEMPLATES*. For more information on templates, see *TEMPLATES*.
@ -627,8 +627,8 @@ Page templates are provided with the following data:
: The title of the page : The title of the page
| Date | Date
: The date of the page : The date of the page
| Path | Permalink
: Path to the page : The permanent link to this page
| Content | Content
: The contents of the page : The contents of the page
| Params | Params
@ -654,8 +654,8 @@ Feed templates are provided with the following data:
:[ *Description* :[ *Description*
| Title | Title
: Title of the feed : Title of the feed
| Path | Permalink
: Path to the feed directory : The permanent link to the feed directory
| Entries | Entries
: List of pages in this feed : List of pages in this feed

View file

@ -71,7 +71,7 @@ func (site *Site) run() error {
func (s *Site) runTask(task *Task) error { func (s *Site) runTask(task *Task) error {
// Read content // Read content
s.root = &Dir{Path: "/"} s.root = &Dir{Permalink: "/"}
if err := s.root.read("content", task, s); err != nil { if err := s.root.read("content", task, s); err != nil {
return err return err
} }

View file

@ -1,16 +1,16 @@
{{ `<?xml version="1.0" encoding="utf-8"?>` | safeHTML }} {{ `<?xml version="1.0" encoding="utf-8"?>` | safeHTML }}
<feed xmlns="http://www.w3.org/2005/Atom"> <feed xmlns="http://www.w3.org/2005/Atom">
<id>{{ index site.URLs 0 }}{{ .Path }}</id> <id>{{ index site.URLs 0 }}{{ .Permalink }}</id>
<title>{{ .Title }}</title> <title>{{ .Title }}</title>
<updated>{{ .Updated.Format "2006-01-02T15:04:05Z07:00" }}</updated> <updated>{{ .Updated.Format "2006-01-02T15:04:05Z07:00" }}</updated>
<link href="{{ index site.URLs 0 | safeURL }}{{ .Path }}" rel="alternate"/> <link href="{{ index site.URLs 0 | safeURL }}{{ .Permalink }}" rel="alternate"/>
{{ range .Entries }}<entry> {{ range .Entries }}<entry>
<id>{{ index site.URLs 0 }}{{ .Path }}</id> <id>{{ index site.URLs 0 }}{{ .Permalink }}</id>
<title>{{ .Title }}</title> <title>{{ .Title }}</title>
<updated>{{ .Date.Format "2006-01-02T15:04:05Z07:00" }}</updated> <updated>{{ .Date.Format "2006-01-02T15:04:05Z07:00" }}</updated>
{{- $path := .Path }} {{- $permalink := .Permalink }}
{{- range site.URLs }} {{- range site.URLs }}
<link href="{{ . | safeURL }}{{ $path }}" rel="alternate"/> <link href="{{ . | safeURL }}{{ $permalink }}" rel="alternate"/>
{{- end }} {{- end }}
</entry> </entry>
{{ end -}} {{ end -}}

View file

@ -1,6 +1,6 @@
# {{ .Title }} # {{ .Title }}
{{ if .Content }} {{ if .Content }}
{{ .Content }}{{ end }} {{ .Content }}{{ end }}
{{ range .Pages }}=> {{ .Path }} {{ if not .Date.IsZero -}} {{ range .Pages }}=> {{ .Permalink }} {{ if not .Date.IsZero -}}
{{.Date.Format "2006-01-02"}} {{end}}{{.Title}} {{.Date.Format "2006-01-02"}} {{end}}{{.Title}}
{{ end -}} {{ end -}}