shithub: hugo

Download patch

ref: db50154e75cfb1100651b7521370f54a0695872e
parent: 4250bf8e3052da8178d5406390c4b455e5592148
author: Noah Campbell <[email protected]>
date: Tue Sep 24 17:24:49 EDT 2013

Support index.html indexes in content directory

If a file named index.html exists in a directory, or root, it will be
rendered as if ugly urls are turned on.  This allows for top level
content to not need a supporting layout file and content in content.
This change should not affect anyone who is using the perscribed way.

I also cleaned up a bunch of one off functions in site.go.

--- a/hugolib/page.go
+++ b/hugolib/page.go
@@ -30,6 +30,7 @@
 	"sort"
 	"strings"
 	"time"
+	"net/url"
 )
 
 type Page struct {
@@ -191,7 +192,7 @@
 	p.FuzzyWordCount = int((p.WordCount+100)/100) * 100
 }
 
-func (p *Page) Permalink() template.HTML {
+func (p *Page) Permalink() (string, error) {
 	baseUrl := string(p.Site.BaseUrl)
 	section := strings.TrimSpace(p.Section)
 	pSlug := strings.TrimSpace(p.Slug)
@@ -215,7 +216,18 @@
 			permalink = section + "/" + file
 		}
 	}
-	return template.HTML(MakePermalink(baseUrl, permalink))
+
+	base, err := url.Parse(baseUrl)
+	if err != nil {
+		return "", err
+	}
+
+	path, err := url.Parse(permalink)
+	if err != nil {
+		return "", err
+	}
+
+	return MakePermalink(base, path).String(), nil
 }
 
 func (page *Page) handleTomlMetaData(datum []byte) (interface{}, error) {
--- a/hugolib/site.go
+++ b/hugolib/site.go
@@ -28,23 +28,15 @@
 	"os"
 	"strings"
 	"time"
+	"net/url"
 )
 
 var DefaultTimer = nitro.Initalize()
 
-func MakePermalink(domain string, path string) string {
-	return strings.TrimRight(domain, "/") + "/" + strings.TrimLeft(path, "/")
+func MakePermalink(base *url.URL, path *url.URL) (*url.URL) {
+	return base.ResolveReference(path)
 }
 
-func FatalErr(str string) {
-	fmt.Println(str)
-	os.Exit(1)
-}
-
-func PrintErr(str string, a ...interface{}) {
-	fmt.Fprintln(os.Stderr, str, a)
-}
-
 // Site contains all the information relevent for constructing a static
 // site.  The basic flow of information is as follows:
 //
@@ -86,10 +78,6 @@
 	Config     *Config
 }
 
-func (s *Site) getFromIndex(kind string, name string) Pages {
-	return s.Indexes[kind][name]
-}
-
 func (s *Site) timerStep(step string) {
 	if s.timer == nil {
 		s.timer = DefaultTimer
@@ -191,8 +179,10 @@
 	}
 }
 
-func (s *Site) initialize() {
-	s.checkDirectories()
+func (s *Site) initialize() (err error) {
+	if err = s.checkDirectories(); err != nil {
+		return err
+	}
 
 	staticDir := s.Config.GetAbsPath(s.Config.StaticDir + "/")
 
@@ -204,6 +194,7 @@
 	s.initializeSiteInfo()
 
 	s.Shortcodes = make(map[string]ShortcodeFunc)
+	return
 }
 
 func (s *Site) initializeSiteInfo() {
@@ -239,13 +230,14 @@
 	return s.Config.GetAbsPath(s.Config.PublishDir)
 }
 
-func (s *Site) checkDirectories() {
+func (s *Site) checkDirectories() (err error) {
 	if b, _ := dirExists(s.absLayoutDir()); !b {
-		FatalErr("No layout directory found, expecting to find it at " + s.absLayoutDir())
+		return fmt.Errorf("No layout directory found, expecting to find it at " + s.absLayoutDir())
 	}
 	if b, _ := dirExists(s.absContentDir()); !b {
-		FatalErr("No source directory found, expecting to find it at " + s.absContentDir())
+		return fmt.Errorf("No source directory found, expecting to find it at " + s.absContentDir())
 	}
+	return
 }
 
 func (s *Site) ProcessShortcodes() {
@@ -289,7 +281,9 @@
 						s.Indexes[plural].Add(idx, p)
 					}
 				} else {
-					PrintErr("Invalid " + plural + " in " + p.File.FileName)
+					if s.Config.Verbose {
+						fmt.Fprintf(os.Stderr, "Invalid %s in %s\n", plural, p.File.FileName)
+					}
 				}
 			}
 		}
@@ -344,9 +338,13 @@
 func (s *Site) RenderAliases() error {
 	for _, p := range s.Pages {
 		for _, a := range p.Aliases {
-			if err := s.WriteAlias(a, p.Permalink()); err != nil {
+			plink, err := p.Permalink()
+			if err != nil {
 				return err
 			}
+			if err := s.WriteAlias(a, template.HTML(plink)); err != nil {
+				return err
+			}
 		}
 	}
 	return nil
@@ -538,8 +536,18 @@
 	}
 }
 
-func permalink(s *Site, plink string) template.HTML {
-	return template.HTML(MakePermalink(string(s.Info.BaseUrl), plink))
+func permalink(s *Site, plink string) (template.HTML) {
+	base, err := url.Parse(string(s.Config.BaseUrl))
+	if err != nil {
+		panic(err)
+	}
+
+	path, err := url.Parse(plink)
+	if err != nil {
+		panic(err)
+	}
+
+	return template.HTML(MakePermalink(base, path).String())
 }
 
 func (s *Site) NewNode() *Node {
--- a/target/file.go
+++ b/target/file.go
@@ -73,7 +73,7 @@
 		dir = path.Join(fs.PublishDir, dir)
 	}
 
-	if fs.UglyUrls {
+	if fs.UglyUrls || file == "index.html" {
 		return path.Join(dir, fmt.Sprintf("%s%s", name, ext)), nil
 	}
 
--- a/target/file_test.go
+++ b/target/file_test.go
@@ -10,7 +10,8 @@
 		expected string
 	}{
 		{"/", "index.html"},
-		{"index.html", "index/index.html"},
+		{"index.html", "index.html"},
+		{"bar/index.html", "bar/index.html"},
 		{"foo", "foo/index.html"},
 		{"foo.html", "foo/index.html"},
 		{"foo.xhtml", "foo/index.xhtml"},