mirror of
https://github.com/gohugoio/hugo.git
synced 2025-04-27 14:10:31 +03:00
Permalink to include multiple directories levels
This commit is contained in:
parent
6e1268f45b
commit
4993152dda
4 changed files with 16 additions and 15 deletions
|
@ -46,7 +46,7 @@ type Page struct {
|
||||||
Tmpl bundle.Template
|
Tmpl bundle.Template
|
||||||
Markup string
|
Markup string
|
||||||
renderable bool
|
renderable bool
|
||||||
layout string
|
layout string
|
||||||
PageMeta
|
PageMeta
|
||||||
File
|
File
|
||||||
Position
|
Position
|
||||||
|
@ -192,7 +192,7 @@ func (page *Page) Layout(l ...string) []string {
|
||||||
|
|
||||||
func layouts(types string, layout string) (layouts []string) {
|
func layouts(types string, layout string) (layouts []string) {
|
||||||
t := strings.Split(types, "/")
|
t := strings.Split(types, "/")
|
||||||
for i := range t {
|
for i := range t {
|
||||||
search := t[:len(t)-i]
|
search := t[:len(t)-i]
|
||||||
layouts = append(layouts, fmt.Sprintf("%s/%s.html", strings.ToLower(path.Join(search...)), layout))
|
layouts = append(layouts, fmt.Sprintf("%s/%s.html", strings.ToLower(path.Join(search...)), layout))
|
||||||
}
|
}
|
||||||
|
@ -223,15 +223,15 @@ func (p *Page) analyzePage() {
|
||||||
|
|
||||||
func (p *Page) permalink() (*url.URL, error) {
|
func (p *Page) permalink() (*url.URL, error) {
|
||||||
baseUrl := string(p.Site.BaseUrl)
|
baseUrl := string(p.Site.BaseUrl)
|
||||||
section := strings.TrimSpace(p.Section)
|
dir := strings.TrimSpace(p.Dir)
|
||||||
pSlug := strings.TrimSpace(p.Slug)
|
pSlug := strings.TrimSpace(p.Slug)
|
||||||
pUrl := strings.TrimSpace(p.Url)
|
pUrl := strings.TrimSpace(p.Url)
|
||||||
var permalink string
|
var permalink string
|
||||||
if len(pSlug) > 0 {
|
if len(pSlug) > 0 {
|
||||||
if p.Site.Config != nil && p.Site.Config.UglyUrls {
|
if p.Site.Config != nil && p.Site.Config.UglyUrls {
|
||||||
permalink = section + "/" + p.Slug + "." + p.Extension
|
permalink = path.Join(dir, p.Slug, p.Extension)
|
||||||
} else {
|
} else {
|
||||||
permalink = section + "/" + p.Slug + "/"
|
permalink = dir + "/" + p.Slug + "/"
|
||||||
}
|
}
|
||||||
} else if len(pUrl) > 2 {
|
} else if len(pUrl) > 2 {
|
||||||
permalink = pUrl
|
permalink = pUrl
|
||||||
|
@ -239,10 +239,10 @@ func (p *Page) permalink() (*url.URL, error) {
|
||||||
_, t := path.Split(p.FileName)
|
_, t := path.Split(p.FileName)
|
||||||
if p.Site.Config != nil && p.Site.Config.UglyUrls {
|
if p.Site.Config != nil && p.Site.Config.UglyUrls {
|
||||||
x := replaceExtension(strings.TrimSpace(t), p.Extension)
|
x := replaceExtension(strings.TrimSpace(t), p.Extension)
|
||||||
permalink = section + "/" + x
|
permalink = path.Join(dir, x)
|
||||||
} else {
|
} else {
|
||||||
file, _ := fileExt(strings.TrimSpace(t))
|
file, _ := fileExt(strings.TrimSpace(t))
|
||||||
permalink = section + "/" + file
|
permalink = path.Join(dir, file)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -7,7 +7,7 @@ import (
|
||||||
|
|
||||||
func TestPermalink(t *testing.T) {
|
func TestPermalink(t *testing.T) {
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
base template.URL
|
base template.URL
|
||||||
expectedAbs string
|
expectedAbs string
|
||||||
expectedRel string
|
expectedRel string
|
||||||
}{
|
}{
|
||||||
|
@ -18,10 +18,10 @@ func TestPermalink(t *testing.T) {
|
||||||
for _, test := range tests {
|
for _, test := range tests {
|
||||||
p := &Page{
|
p := &Page{
|
||||||
Node: Node{
|
Node: Node{
|
||||||
UrlPath: UrlPath{Section: "x/y/z"},
|
UrlPath: UrlPath{Section: "z"},
|
||||||
Site: SiteInfo{BaseUrl: test.base},
|
Site: SiteInfo{BaseUrl: test.base},
|
||||||
},
|
},
|
||||||
File: File{FileName: "x/y/z/boofar.md"},
|
File: File{FileName: "x/y/z/boofar.md", Dir: "x/y/z"},
|
||||||
}
|
}
|
||||||
|
|
||||||
u, err := p.Permalink()
|
u, err := p.Permalink()
|
||||||
|
|
|
@ -67,7 +67,7 @@ type Site struct {
|
||||||
Transformer transform.Transformer
|
Transformer transform.Transformer
|
||||||
Target target.Output
|
Target target.Output
|
||||||
Alias target.AliasPublisher
|
Alias target.AliasPublisher
|
||||||
Completed chan bool
|
Completed chan bool
|
||||||
}
|
}
|
||||||
|
|
||||||
type SiteInfo struct {
|
type SiteInfo struct {
|
||||||
|
@ -487,7 +487,7 @@ func (s *Site) RenderHomePage() error {
|
||||||
n.Data["Pages"] = s.Pages[:9]
|
n.Data["Pages"] = s.Pages[:9]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
err := s.render(n, "/", "index.html", "_default/single.html")
|
err := s.render(n, "/", "index.html")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -552,8 +552,7 @@ func (s *Site) render(d interface{}, out string, layouts ...string) (err error)
|
||||||
}
|
}
|
||||||
|
|
||||||
section := ""
|
section := ""
|
||||||
page, ok := d.(*Page)
|
if page, ok := d.(*Page); ok {
|
||||||
if ok {
|
|
||||||
section, _ = page.RelPermalink()
|
section, _ = page.RelPermalink()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -2,11 +2,11 @@ package hugolib
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"io"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/spf13/hugo/source"
|
"github.com/spf13/hugo/source"
|
||||||
"github.com/spf13/hugo/target"
|
"github.com/spf13/hugo/target"
|
||||||
"html/template"
|
"html/template"
|
||||||
|
"io"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
@ -187,6 +187,8 @@ func TestTargetPath(t *testing.T) {
|
||||||
expectedSection string
|
expectedSection string
|
||||||
}{
|
}{
|
||||||
{"content/a/file.md", PAGE_URL_SPECIFIED, "mycategory/my-whatever-content/index.html", "a"},
|
{"content/a/file.md", PAGE_URL_SPECIFIED, "mycategory/my-whatever-content/index.html", "a"},
|
||||||
|
{"content/x/y/deepfile.md", SIMPLE_PAGE, "x/y/deepfile.html", "x/y"},
|
||||||
|
{"content/x/y/z/deeperfile.md", SIMPLE_PAGE, "x/y/z/deeperfile.html", "x/y/z"},
|
||||||
{"content/b/file.md", SIMPLE_PAGE, "b/file.html", "b"},
|
{"content/b/file.md", SIMPLE_PAGE, "b/file.html", "b"},
|
||||||
{"a/file.md", SIMPLE_PAGE, "a/file.html", "a"},
|
{"a/file.md", SIMPLE_PAGE, "a/file.html", "a"},
|
||||||
{"file.md", SIMPLE_PAGE, "file.html", ""},
|
{"file.md", SIMPLE_PAGE, "file.html", ""},
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue