mirror of
https://github.com/gohugoio/hugo.git
synced 2025-08-28 22:19:59 +02:00
Section is determined by the source, not the url
This change allows for top level html content to exists.
This commit is contained in:
@@ -35,5 +35,4 @@ type UrlPath struct {
|
||||
Permalink template.HTML
|
||||
Slug string
|
||||
Section string
|
||||
Path string
|
||||
}
|
||||
|
@@ -52,7 +52,7 @@ type Page struct {
|
||||
}
|
||||
|
||||
type File struct {
|
||||
FileName, OutFile, Extension string
|
||||
FileName, OutFile, Extension, Dir string
|
||||
}
|
||||
|
||||
type PageMeta struct {
|
||||
|
@@ -37,10 +37,6 @@ func MakePermalink(domain string, path string) string {
|
||||
return strings.TrimRight(domain, "/") + "/" + strings.TrimLeft(path, "/")
|
||||
}
|
||||
|
||||
func mkdirIf(path string) error {
|
||||
return os.MkdirAll(path, 0777)
|
||||
}
|
||||
|
||||
func FatalErr(str string) {
|
||||
fmt.Println(str)
|
||||
os.Exit(1)
|
||||
@@ -150,6 +146,18 @@ func (s *Site) Process() (err error) {
|
||||
return
|
||||
}
|
||||
|
||||
func (s *Site) setupPrevNext() {
|
||||
for i, page := range s.Pages {
|
||||
if i < len(s.Pages)-1 {
|
||||
page.Next = s.Pages[i+1]
|
||||
}
|
||||
|
||||
if i > 0 {
|
||||
page.Prev = s.Pages[i-1]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Site) Render() (err error) {
|
||||
if err = s.RenderAliases(); err != nil {
|
||||
return
|
||||
@@ -239,7 +247,6 @@ func (s *Site) checkDirectories() {
|
||||
if b, _ := dirExists(s.absContentDir()); !b {
|
||||
FatalErr("No source directory found, expecting to find it at " + s.absContentDir())
|
||||
}
|
||||
mkdirIf(s.absPublishDir())
|
||||
}
|
||||
|
||||
func (s *Site) ProcessShortcodes() {
|
||||
@@ -250,16 +257,17 @@ func (s *Site) ProcessShortcodes() {
|
||||
|
||||
func (s *Site) CreatePages() (err error) {
|
||||
for _, file := range s.Source.Files() {
|
||||
page, err := ReadFrom(file.Contents, file.Name)
|
||||
page, err := ReadFrom(file.Contents, file.LogicalName)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
page.Site = s.Info
|
||||
page.Tmpl = s.Tmpl
|
||||
page.Section = file.Section
|
||||
page.Dir = file.Dir
|
||||
if err = s.setUrlPath(page); err != nil {
|
||||
return err
|
||||
}
|
||||
s.setOutFile(page)
|
||||
if s.Config.BuildDrafts || !page.Draft {
|
||||
s.Pages = append(s.Pages, page)
|
||||
}
|
||||
@@ -269,36 +277,11 @@ func (s *Site) CreatePages() (err error) {
|
||||
return
|
||||
}
|
||||
|
||||
func (s *Site) setupPrevNext() {
|
||||
for i, page := range s.Pages {
|
||||
if i < len(s.Pages)-1 {
|
||||
page.Next = s.Pages[i+1]
|
||||
}
|
||||
// Set p.Section and p.OutFile relying on p.FileName as the source.
|
||||
// Filename is broken apart for a "Section" which basically equates to
|
||||
// the folder the file exists in.
|
||||
func (s *Site) setUrlPath(p *Page) (err error) {
|
||||
|
||||
if i > 0 {
|
||||
page.Prev = s.Pages[i-1]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Site) setUrlPath(p *Page) error {
|
||||
y := strings.TrimPrefix(p.FileName, s.absContentDir())
|
||||
x := strings.Split(y, "/")
|
||||
|
||||
if len(x) <= 1 {
|
||||
return fmt.Errorf("Zero length page name. filename: %s", y)
|
||||
}
|
||||
|
||||
p.Section = strings.Trim(x[1], "/")
|
||||
p.Path = path.Join(x[:len(x)-1]...)
|
||||
return nil
|
||||
}
|
||||
|
||||
// If Url is provided it is assumed to be the complete relative path
|
||||
// and will override everything
|
||||
// Otherwise path + slug is used if provided
|
||||
// Lastly path + filename is used if provided
|
||||
func (s *Site) setOutFile(p *Page) {
|
||||
// Always use Url if it's specified
|
||||
if len(strings.TrimSpace(p.Url)) > 2 {
|
||||
p.OutFile = strings.TrimSpace(p.Url)
|
||||
@@ -318,7 +301,8 @@ func (s *Site) setOutFile(p *Page) {
|
||||
outfile = replaceExtension(strings.TrimSpace(t), p.Extension)
|
||||
}
|
||||
|
||||
p.OutFile = p.Path + "/" + strings.TrimSpace(outfile)
|
||||
p.OutFile = p.Dir + "/" + strings.TrimSpace(outfile)
|
||||
return
|
||||
}
|
||||
|
||||
func (s *Site) BuildSiteMeta() (err error) {
|
||||
|
@@ -9,30 +9,19 @@ import (
|
||||
|
||||
const ALIAS_DOC_1 = "---\ntitle: alias doc\naliases:\n - \"alias1/\"\n - \"alias-2/\"\n---\naliases\n"
|
||||
|
||||
type byteSource struct {
|
||||
name string
|
||||
content []byte
|
||||
}
|
||||
|
||||
var fakeSource = []byteSource{
|
||||
{"foo/bar/file.md", []byte(SIMPLE_PAGE)},
|
||||
{"alias/test/file1.md", []byte(ALIAS_DOC_1)},
|
||||
{"section/somecontent.html", []byte(RENDER_NO_FRONT_MATTER)},
|
||||
}
|
||||
|
||||
type inMemorySource struct {
|
||||
byteSource []byteSource
|
||||
}
|
||||
|
||||
func (i *inMemorySource) Files() (files []*source.File) {
|
||||
files = make([]*source.File, len(i.byteSource))
|
||||
for i, fake := range i.byteSource {
|
||||
files[i] = &source.File{
|
||||
Name: fake.name,
|
||||
Contents: bytes.NewReader(fake.content),
|
||||
}
|
||||
}
|
||||
return
|
||||
var fakeSource = []source.ByteSource{
|
||||
{
|
||||
Name: "foo/bar/file.md",
|
||||
Content: []byte(SIMPLE_PAGE),
|
||||
},
|
||||
{
|
||||
Name: "alias/test/file1.md",
|
||||
Content: []byte(ALIAS_DOC_1),
|
||||
},
|
||||
{
|
||||
Name: "section/somecontent.html",
|
||||
Content: []byte(RENDER_NO_FRONT_MATTER),
|
||||
},
|
||||
}
|
||||
|
||||
func checkShowPlanExpected(t *testing.T, s *Site, expected string) {
|
||||
@@ -52,7 +41,7 @@ func TestDegenerateNoFiles(t *testing.T) {
|
||||
|
||||
func TestDegenerateNoTarget(t *testing.T) {
|
||||
s := &Site{
|
||||
Source: &inMemorySource{fakeSource},
|
||||
Source: &source.InMemorySource{fakeSource},
|
||||
}
|
||||
must(s.CreatePages())
|
||||
expected := "foo/bar/file.md (renderer: markdown)\n canonical => !no target specified!\n\n" +
|
||||
@@ -63,7 +52,7 @@ func TestDegenerateNoTarget(t *testing.T) {
|
||||
|
||||
func TestFileTarget(t *testing.T) {
|
||||
s := &Site{
|
||||
Source: &inMemorySource{fakeSource},
|
||||
Source: &source.InMemorySource{fakeSource},
|
||||
Target: new(target.Filesystem),
|
||||
Alias: new(target.HTMLRedirectAlias),
|
||||
}
|
||||
@@ -81,7 +70,7 @@ func TestFileTarget(t *testing.T) {
|
||||
func TestFileTargetUgly(t *testing.T) {
|
||||
s := &Site{
|
||||
Target: &target.Filesystem{UglyUrls: true},
|
||||
Source: &inMemorySource{fakeSource},
|
||||
Source: &source.InMemorySource{fakeSource},
|
||||
Alias: new(target.HTMLRedirectAlias),
|
||||
}
|
||||
s.CreatePages()
|
||||
@@ -97,7 +86,7 @@ func TestFileTargetUgly(t *testing.T) {
|
||||
func TestFileTargetPublishDir(t *testing.T) {
|
||||
s := &Site{
|
||||
Target: &target.Filesystem{PublishDir: "../public"},
|
||||
Source: &inMemorySource{fakeSource},
|
||||
Source: &source.InMemorySource{fakeSource},
|
||||
Alias: &target.HTMLRedirectAlias{PublishDir: "../public"},
|
||||
}
|
||||
|
||||
|
@@ -3,6 +3,7 @@ package hugolib
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"github.com/spf13/hugo/source"
|
||||
"html/template"
|
||||
"strings"
|
||||
"testing"
|
||||
@@ -74,25 +75,6 @@ func matchRender(t *testing.T, s *Site, p *Page, tmplName string, expected strin
|
||||
}
|
||||
}
|
||||
|
||||
func _TestAddSameTemplateTwice(t *testing.T) {
|
||||
p := pageMust(ReadFrom(strings.NewReader(PAGE_SIMPLE_TITLE), "content/a/file.md"))
|
||||
s := new(Site)
|
||||
s.prepTemplates()
|
||||
err := s.addTemplate("foo", TEMPLATE_TITLE)
|
||||
if err != nil {
|
||||
t.Fatalf("Unable to add template foo")
|
||||
}
|
||||
|
||||
matchRender(t, s, p, "foo", "simple template")
|
||||
|
||||
err = s.addTemplate("foo", "NEW {{ .Title }}")
|
||||
if err != nil {
|
||||
t.Fatalf("Unable to add template foo: %s", err)
|
||||
}
|
||||
|
||||
matchRender(t, s, p, "foo", "NEW simple template")
|
||||
}
|
||||
|
||||
func TestRenderThing(t *testing.T) {
|
||||
tests := []struct {
|
||||
content string
|
||||
@@ -177,32 +159,59 @@ func TestRenderThingOrDefault(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestSetOutFile(t *testing.T) {
|
||||
s := new(Site)
|
||||
p := pageMust(ReadFrom(strings.NewReader(PAGE_URL_SPECIFIED), "content/a/file.md"))
|
||||
s.setOutFile(p)
|
||||
tests := []struct {
|
||||
doc string
|
||||
content string
|
||||
expectedOutFile string
|
||||
expectedSection string
|
||||
}{
|
||||
{"content/a/file.md", PAGE_URL_SPECIFIED, "mycategory/my-whatever-content/index.html", "a"},
|
||||
{"content/b/file.md", SIMPLE_PAGE, "b/file.html", "b"},
|
||||
{"a/file.md", SIMPLE_PAGE, "a/file.html", "a"},
|
||||
{"file.md", SIMPLE_PAGE, "file.html", ""},
|
||||
}
|
||||
|
||||
expected := "mycategory/my-whatever-content/index.html"
|
||||
if true {
|
||||
return
|
||||
}
|
||||
for _, test := range tests {
|
||||
var err error
|
||||
s := &Site{
|
||||
Config: Config{ContentDir: "content"},
|
||||
}
|
||||
p := pageMust(ReadFrom(strings.NewReader(test.content), s.Config.GetAbsPath(test.doc)))
|
||||
if err = s.setUrlPath(p); err != nil {
|
||||
t.Fatalf("Unable to set urlpath: %s", err)
|
||||
}
|
||||
|
||||
if p.OutFile != "mycategory/my-whatever-content/index.html" {
|
||||
t.Errorf("Outfile does not match. Expected '%s', got '%s'", expected, p.OutFile)
|
||||
expected := test.expectedOutFile
|
||||
|
||||
if p.OutFile != expected {
|
||||
t.Errorf("%s => p.OutFile expected: '%s', got: '%s'", test.doc, expected, p.OutFile)
|
||||
}
|
||||
|
||||
if p.Section != test.expectedSection {
|
||||
t.Errorf("%s => p.Section expected: %s, got: %s", test.doc, test.expectedSection, p.Section)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestSkipRender(t *testing.T) {
|
||||
files := make(map[string][]byte)
|
||||
target := &InMemoryTarget{files: files}
|
||||
sources := []byteSource{
|
||||
{"sect/doc1.html", []byte("---\nmarkup: markdown\n---\n# title\nsome *content*")},
|
||||
{"sect/doc2.html", []byte("<!doctype html><html><body>more content</body></html>")},
|
||||
{"sect/doc3.md", []byte("# doc3\n*some* content")},
|
||||
{"sect/doc4.md", []byte("---\ntitle: doc4\n---\n# doc4\n*some content*")},
|
||||
{"sect/doc5.html", []byte("<!doctype html><html>{{ template \"head\" }}<body>body5</body></html>")},
|
||||
sources := []source.ByteSource{
|
||||
{"sect/doc1.html", []byte("---\nmarkup: markdown\n---\n# title\nsome *content*"), "sect"},
|
||||
{"sect/doc2.html", []byte("<!doctype html><html><body>more content</body></html>"), "sect"},
|
||||
{"sect/doc3.md", []byte("# doc3\n*some* content"), "sect"},
|
||||
{"sect/doc4.md", []byte("---\ntitle: doc4\n---\n# doc4\n*some content*"), "sect"},
|
||||
{"sect/doc5.html", []byte("<!doctype html><html>{{ template \"head\" }}<body>body5</body></html>"), "sect"},
|
||||
{"doc7.html", []byte("<html><body>doc7 content</body></html>"), ""},
|
||||
}
|
||||
|
||||
s := &Site{
|
||||
Target: target,
|
||||
Config: Config{BaseUrl: "http://auth/bub/"},
|
||||
Source: &inMemorySource{sources},
|
||||
Source: &source.InMemorySource{sources},
|
||||
}
|
||||
s.initializeSiteInfo()
|
||||
s.prepTemplates()
|
||||
@@ -231,6 +240,7 @@ func TestSkipRender(t *testing.T) {
|
||||
{"sect/doc3.html", "<html><head></head><body><h1>doc3</h1>\n\n<p><em>some</em> content</p>\n</body></html>"},
|
||||
{"sect/doc4.html", "<html><head></head><body><h1>doc4</h1>\n\n<p><em>some content</em></p>\n</body></html>"},
|
||||
{"sect/doc5.html", "<!DOCTYPE html><html><head><script src=\"http://auth/bub/script.js\"></script></head><body>body5</body></html>"},
|
||||
{"./doc7.html", "<html><head></head><body>doc7 content</body></html>"},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
@@ -248,14 +258,14 @@ func TestSkipRender(t *testing.T) {
|
||||
func TestAbsUrlify(t *testing.T) {
|
||||
files := make(map[string][]byte)
|
||||
target := &InMemoryTarget{files: files}
|
||||
sources := []byteSource{
|
||||
{"sect/doc1.html", []byte("<!doctype html><html><head></head><body><a href=\"#frag1\">link</a></body></html>")},
|
||||
{"content/blue/doc2.html", []byte("---\nf: t\n---\n<!doctype html><html><body>more content</body></html>")},
|
||||
sources := []source.ByteSource{
|
||||
{"sect/doc1.html", []byte("<!doctype html><html><head></head><body><a href=\"#frag1\">link</a></body></html>"), "sect"},
|
||||
{"content/blue/doc2.html", []byte("---\nf: t\n---\n<!doctype html><html><body>more content</body></html>"), "blue"},
|
||||
}
|
||||
s := &Site{
|
||||
Target: target,
|
||||
Config: Config{BaseUrl: "http://auth/bub/"},
|
||||
Source: &inMemorySource{sources},
|
||||
Source: &source.InMemorySource{sources},
|
||||
}
|
||||
s.initializeSiteInfo()
|
||||
s.prepTemplates()
|
||||
@@ -281,14 +291,14 @@ func TestAbsUrlify(t *testing.T) {
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
content, ok := target.files[test.file]
|
||||
if !ok {
|
||||
t.Fatalf("Unable to locate rendered content: %s", test.file)
|
||||
}
|
||||
content, ok := target.files[test.file]
|
||||
if !ok {
|
||||
t.Fatalf("Unable to locate rendered content: %s", test.file)
|
||||
}
|
||||
|
||||
expected := test.expected
|
||||
if string(content) != expected {
|
||||
t.Errorf("AbsUrlify content expected:\n%q\ngot\n%q", expected, string(content))
|
||||
expected := test.expected
|
||||
if string(content) != expected {
|
||||
t.Errorf("AbsUrlify content expected:\n%q\ngot\n%q", expected, string(content))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -2,6 +2,7 @@ package hugolib
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"github.com/spf13/hugo/source"
|
||||
"github.com/spf13/hugo/target"
|
||||
"html/template"
|
||||
"io"
|
||||
@@ -61,9 +62,9 @@ func (t *InMemoryAliasTarget) Publish(label string, permalink template.HTML) (er
|
||||
return
|
||||
}
|
||||
|
||||
var urlFakeSource = []byteSource{
|
||||
{"content/blue/doc1.md", []byte(SLUG_DOC_1)},
|
||||
{"content/blue/doc2.md", []byte(SLUG_DOC_2)},
|
||||
var urlFakeSource = []source.ByteSource{
|
||||
{"content/blue/doc1.md", []byte(SLUG_DOC_1), "blue"},
|
||||
{"content/blue/doc2.md", []byte(SLUG_DOC_2), "blue"},
|
||||
}
|
||||
|
||||
func TestPageCount(t *testing.T) {
|
||||
@@ -74,7 +75,7 @@ func TestPageCount(t *testing.T) {
|
||||
Target: target,
|
||||
Alias: alias,
|
||||
Config: Config{UglyUrls: false},
|
||||
Source: &inMemorySource{urlFakeSource},
|
||||
Source: &source.InMemorySource{urlFakeSource},
|
||||
}
|
||||
s.initializeSiteInfo()
|
||||
s.prepTemplates()
|
||||
|
Reference in New Issue
Block a user