1
0
mirror of https://github.com/danog/gojekyll.git synced 2024-11-30 08:08:59 +01:00

Match collection permalinks to witnessed behavior

This commit is contained in:
Oliver Steele 2017-06-23 10:12:23 -04:00
parent 31edc49b77
commit 11e8e4c94a
8 changed files with 25 additions and 18 deletions

View File

@ -6,6 +6,7 @@ import (
"path/filepath"
"strings"
"github.com/osteele/gojekyll/constants"
"github.com/osteele/gojekyll/pages"
"github.com/osteele/gojekyll/templates"
)
@ -25,12 +26,8 @@ func NewCollection(ctx pages.Context, name string, metadata templates.VariableMa
}
}
// DefaultPermalink returns the default Permalink for pages in a collection
// that doesn't specify a permalink in the site config.
func (c *Collection) DefaultPermalink() string { return "/:categories/:year/:month/:day/:title.html" }
// IsPosts returns true if the collection is the special "posts" collection.
func (c *Collection) IsPosts() bool { return c.Name == "posts" }
func (c *Collection) IsPostsCollection() bool { return c.Name == "posts" }
// Output returns a bool indicating whether files in this collection should be written.
func (c *Collection) Output() bool { return c.Metadata.Bool("output", false) }
@ -57,13 +54,17 @@ func (c *Collection) TemplateVariable() []templates.VariableMap {
func (c *Collection) ReadPages(ctx pages.Context, sitePath string, frontMatterDefaults func(string, string) templates.VariableMap) error {
pageDefaults := templates.VariableMap{
"collection": c.Name,
"permalink": constants.DefaultCollectionPermalinkPattern,
}
if c.IsPostsCollection() {
pageDefaults["permalink"] = constants.DefaultPostsCollectionPermalinkPattern
}
walkFn := func(filename string, info os.FileInfo, err error) error {
if err != nil {
// if the issue is simply that the directory doesn't exist, warn instead of error
if os.IsNotExist(err) {
if !c.IsPosts() {
if !c.IsPostsCollection() {
fmt.Printf("Missing collection directory: _%s\n", c.Name)
}
return nil

10
constants/permalinks.go Normal file
View File

@ -0,0 +1,10 @@
package constants
// DefaultPermalinkPattern is the default permalink pattern for pages that aren't in a collection
const DefaultPermalinkPattern = "/:path:output_ext"
// DefaultCollectionPermalinkPattern is the default permalink pattern for pages in the posts collection
const DefaultCollectionPermalinkPattern = "/:collection/:path:output_ext"
// DefaultPostPermalinkPattern is the default collection permalink pattern
const DefaultPostsCollectionPermalinkPattern = "/:categories/:year/:month/:day/:title.html"

View File

@ -1,7 +1,6 @@
---
layout: default
---
--- layout: default ---
<!-- archive layout -->
<h2>Archive Layout</h2>
{{content}}
<!-- archive layout end -->
<!-- archive layout end -->

View File

@ -1,4 +1,4 @@
<!-- default layout -->
<h1>{{page.title}}</h1>
<h1>Default Layout: {{page.title}}</h1>
{{content}}
<!-- default layout end -->
<!-- default layout end -->

View File

@ -1,5 +1,4 @@
---
permalink: /:name
layout: archive
---

View File

@ -55,7 +55,6 @@ type Context interface {
// Container is the Page container
type Container interface {
DefaultPermalink() string
PathPrefix() string
Output() bool
}

View File

@ -8,6 +8,7 @@ import (
"strconv"
"strings"
"github.com/osteele/gojekyll/constants"
"github.com/osteele/gojekyll/helpers"
)
@ -64,7 +65,8 @@ func (p *pageFields) permalinkTemplateVariables() map[string]string {
}
func (p *pageFields) expandPermalink() (s string, err error) {
pattern := p.frontMatter.String("permalink", p.container.DefaultPermalink())
pattern := p.frontMatter.String("permalink", constants.DefaultPermalinkPattern)
if p, found := PermalinkStyles[pattern]; found {
pattern = p
}

View File

@ -34,9 +34,6 @@ type Site struct {
// SourceDir returns the sites source directory.
func (s *Site) SourceDir() string { return s.Source }
// DefaultPermalink returns the default Permalink for pages not in a collection.
func (s *Site) DefaultPermalink() string { return "/:path:output_ext" }
// Output returns true, indicating that the files in the site should be written.
func (s *Site) Output() bool { return true }