shithub: hugo

Download patch

ref: 2ebfb33fe01d38a1341d6fe3c922dad5e93fd769
parent: 2f10da15707e1db0fab90524a247bc8a2d3ded90
author: Noah Campbell <[email protected]>
date: Thu Sep 12 17:18:13 EDT 2013

Move alias logic to target module

I want to move all logic to writing aliases to target so I can pave the
way for writing aliases specific to other runtimes (like .htaccess for
apache or a script for updating AWS or symlinking on a filesystem).

--- a/hugolib/site.go
+++ b/hugolib/site.go
@@ -76,7 +76,7 @@
 	Shortcodes map[string]ShortcodeFunc
 	timer      *nitro.B
 	Target     target.Output
-	Alias      target.Translator
+	Alias      target.AliasPublisher
 }
 
 type SiteInfo struct {
@@ -405,17 +405,9 @@
 func (s *Site) RenderAliases() error {
 	for _, p := range s.Pages {
 		for _, a := range p.Aliases {
-			t := "alias"
-			if strings.HasSuffix(a, ".xhtml") {
-				t = "alias-xhtml"
-			}
-			content, err := s.RenderThing(p, t)
-			if err != nil {
+			if err := s.WriteAlias(a, p.Permalink()); err != nil {
 				return err
 			}
-			if err = s.WriteAlias(a, content.Bytes()); err != nil {
-				return err
-			}
 		}
 	}
 	return nil
@@ -657,10 +649,12 @@
 	return s.Target.Publish(path, bytes.NewReader(content))
 }
 
-func (s *Site) WriteAlias(path string, content []byte) (err error) {
+func (s *Site) WriteAlias(path string, permalink template.HTML) (err error) {
 	if s.Alias == nil {
 		s.initTarget()
-		s.Alias = new(target.HTMLRedirectAlias)
+		s.Alias = &target.HTMLRedirectAlias{
+			PublishDir: s.absPublishDir(),
+		}
 	}
 
 	if s.Config.Verbose {
@@ -667,8 +661,5 @@
 		fmt.Println(path)
 	}
 
-	if path, err = s.Alias.Translate(path); err != nil {
-		return err
-	}
-	return s.Target.Publish(path, bytes.NewReader(content))
+	return s.Alias.Publish(path, permalink)
 }
--- a/hugolib/site_test.go
+++ b/hugolib/site_test.go
@@ -52,14 +52,6 @@
 	}
 }
 
-func TestPrimeTemplates(t *testing.T) {
-	s := new(Site)
-	s.prepTemplates()
-	if s.Tmpl.Lookup("alias") == nil {
-		t.Fatalf("alias template not created.")
-	}
-}
-
 func TestAddInvalidTemplate(t *testing.T) {
 	s := new(Site)
 	s.prepTemplates()
--- a/hugolib/site_url_test.go
+++ b/hugolib/site_url_test.go
@@ -4,6 +4,8 @@
 	"bytes"
 	"io"
 	"testing"
+	"html/template"
+	"github.com/spf13/hugo/target"
 )
 
 const SLUG_DOC_1 = "---\ntitle: slug doc 1\nslug: slug-doc-1\naliases:\n - sd1/foo/\n - sd2\n - sd3/\n - sd4.php\n---\nslug doc 1 content"
@@ -44,6 +46,17 @@
 	return label, nil
 }
 
+type InMemoryAliasTarget struct {
+	target.HTMLRedirectAlias
+	files map[string][]byte
+}
+
+func (t *InMemoryAliasTarget) Publish(label string, permalink template.HTML) (err error) {
+	f, _ := t.Translate(label)
+	t.files[f] = []byte("--dummy text--")
+	return
+}
+
 var urlFakeSource = []byteSource{
 	{"content/blue/doc1.md", []byte(SLUG_DOC_1)},
 	{"content/blue/doc2.md", []byte(SLUG_DOC_2)},
@@ -50,9 +63,12 @@
 }
 
 func TestPageCount(t *testing.T) {
-	target := new(InMemoryTarget)
+	files := make(map[string][]byte)
+	target := &InMemoryTarget{files: files}
+	alias := &InMemoryAliasTarget{files: files}
 	s := &Site{
 		Target: target,
+		Alias: alias,
 		Config: Config{UglyUrls: false},
 		Source: &inMemorySource{urlFakeSource},
 	}
--- a/target/file.go
+++ b/target/file.go
@@ -34,7 +34,11 @@
 		return
 	}
 
-	path, _ = filepath.Split(translated)
+	return writeToDisk(translated, r)
+}
+
+func writeToDisk(translated string, r io.Reader) (err error) {
+	path, _ := filepath.Split(translated)
 	ospath := filepath.FromSlash(path)
 
 	if ospath != "" {
--- a/target/htmlredirect.go
+++ b/target/htmlredirect.go
@@ -3,11 +3,30 @@
 import (
 	helpers "github.com/spf13/hugo/template"
 	"path"
+	"bytes"
 	"strings"
+	"html/template"
 )
 
+const ALIAS = "<!DOCTYPE html><html><head><link rel=\"canonical\" href=\"{{ .Permalink }}\"/><meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\" /><meta http-equiv=\"refresh\" content=\"0;url={{ .Permalink }}\" /></head></html>"
+const	ALIAS_XHTML = "<!DOCTYPE html><html xmlns=\"http://www.w3.org/1999/xhtml\"><head><link rel=\"canonical\" href=\"{{ .Permalink }}\"/><meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\" /><meta http-equiv=\"refresh\" content=\"0;url={{ .Permalink }}\" /></head></html>"
+
+var DefaultAliasTemplates *template.Template
+
+func init() {
+	DefaultAliasTemplates = template.New("")
+	template.Must(DefaultAliasTemplates.New("alias").Parse(ALIAS))
+	template.Must(DefaultAliasTemplates.New("alias-xhtml").Parse(ALIAS_XHTML))
+}
+
+type AliasPublisher interface {
+	Translator
+	Publish(string, template.HTML) error
+}
+
 type HTMLRedirectAlias struct {
 	PublishDir string
+	Templates *template.Template
 }
 
 func (h *HTMLRedirectAlias) Translate(alias string) (aliasPath string, err error) {
@@ -15,4 +34,32 @@
 		alias = alias + "index.html"
 	}
 	return path.Join(h.PublishDir, helpers.Urlize(alias)), nil
+}
+
+type AliasNode struct {
+	Permalink template.HTML
+}
+
+func (h *HTMLRedirectAlias) Publish(path string, permalink template.HTML) (err error) {
+	if path, err = h.Translate(path); err != nil {
+		return
+	}
+
+	t := "alias"
+	if strings.HasSuffix(path, ".xhtml") {
+		t = "alias-xhtml"
+	}
+
+	template := DefaultAliasTemplates
+	if h.Templates != nil {
+		template = h.Templates
+	}
+
+	buffer := new(bytes.Buffer)
+	err = template.ExecuteTemplate(buffer, t, &AliasNode{permalink})
+	if err != nil {
+		return
+	}
+
+	return writeToDisk(path, buffer)
 }
--- a/template/bundle/bundle_test.go
+++ /dev/null
@@ -1,12 +1,0 @@
-package bundle
-
-import (
-	"testing"
-)
-
-func TestNothing(t *testing.T) {
-	b := NewTemplate()
-	if b.Lookup("alias") == nil {
-		t.Fatalf("Expecting alias to be initialized with new bundle")
-	}
-}
--- a/template/bundle/template.go
+++ b/template/bundle/template.go
@@ -116,7 +116,6 @@
 	}
 
 	templates.Funcs(funcMap)
-	templates.primeTemplates()
 	return templates
 }
 
@@ -143,14 +142,6 @@
 
 func (t *GoHtmlTemplate) generateTemplateNameFrom(base, path string) string {
 	return filepath.ToSlash(path[len(base)+1:])
-}
-
-func (t *GoHtmlTemplate) primeTemplates() {
-	alias := "<!DOCTYPE html>\n <html>\n <head>\n <link rel=\"canonical\" href=\"{{ .Permalink }}\"/>\n <meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\" />\n <meta http-equiv=\"refresh\" content=\"0;url={{ .Permalink }}\" />\n </head>\n </html>"
-	alias_xhtml := "<!DOCTYPE html>\n <html xmlns=\"http://www.w3.org/1999/xhtml\">\n <head>\n <link rel=\"canonical\" href=\"{{ .Permalink }}\"/>\n <meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\" />\n <meta http-equiv=\"refresh\" content=\"0;url={{ .Permalink }}\" />\n </head>\n </html>"
-
-	t.AddTemplate("alias", alias)
-	t.AddTemplate("alias-xhtml", alias_xhtml)
 }
 
 func ignoreDotFile(path string) bool {