Support index.html indexes in content directory
authorNoah Campbell <noahcampbell@gmail.com>
Wed, 25 Sep 2013 04:24:49 +0000 (21:24 -0700)
committerNoah Campbell <noahcampbell@gmail.com>
Wed, 25 Sep 2013 04:27:25 +0000 (21:27 -0700)
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.

hugolib/page.go
hugolib/site.go
target/file.go
target/file_test.go

index e93984bcfa6736ceb8ead503454bc63c6cf8d21f..9d978c984eb8f03e7e863198e4e08af57053e162 100644 (file)
@@ -30,6 +30,7 @@ import (
        "sort"
        "strings"
        "time"
+       "net/url"
 )
 
 type Page struct {
@@ -191,7 +192,7 @@ func (p *Page) analyzePage() {
        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 @@ func (p *Page) Permalink() template.HTML {
                        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) {
index 10aa8c9850f8e3282e63b2a663b36b8c975f498f..2e16094b9fb88cb122feec765d88874501f1c9fe 100644 (file)
@@ -28,21 +28,13 @@ import (
        "os"
        "strings"
        "time"
+       "net/url"
 )
 
 var DefaultTimer = nitro.Initalize()
 
-func MakePermalink(domain string, path string) string {
-       return strings.TrimRight(domain, "/") + "/" + strings.TrimLeft(path, "/")
-}
-
-func FatalErr(str string) {
-       fmt.Println(str)
-       os.Exit(1)
-}
-
-func PrintErr(str string, a ...interface{}) {
-       fmt.Fprintln(os.Stderr, str, a)
+func MakePermalink(base *url.URL, path *url.URL) (*url.URL) {
+       return base.ResolveReference(path)
 }
 
 // Site contains all the information relevent for constructing a static
@@ -86,10 +78,6 @@ type SiteInfo struct {
        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) checkDescriptions() {
        }
 }
 
-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 @@ func (s *Site) initialize() {
        s.initializeSiteInfo()
 
        s.Shortcodes = make(map[string]ShortcodeFunc)
+       return
 }
 
 func (s *Site) initializeSiteInfo() {
@@ -239,13 +230,14 @@ func (s *Site) absPublishDir() string {
        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 @@ func (s *Site) BuildSiteMeta() (err error) {
                                                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,7 +338,11 @@ func inStringArray(arr []string, el string) bool {
 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
                        }
                }
@@ -538,8 +536,18 @@ func (s *Site) Stats() {
        }
 }
 
-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 {
index 59281633a32826fa7379ac78e074a4720cd09ec6..0e48a93e522158c89faf7c123fd96489fb8a1974 100644 (file)
@@ -73,7 +73,7 @@ func (fs *Filesystem) Translate(src string) (dest string, err error) {
                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
        }
 
index 1ac1f3c7986690d95f18177b6dd9564cb6bc43c6..258eae41ca3d284014145fdbec2ad2f197c2cc89 100644 (file)
@@ -10,7 +10,8 @@ func TestFileTranslator(t *testing.T) {
                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"},