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.
This commit is contained in:
Noah Campbell 2013-09-24 21:24:49 -07:00
parent 4250bf8e30
commit db50154e75
4 changed files with 49 additions and 28 deletions

View 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) {

View 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 {

View 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
}

View 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"},