diff --git a/hugolib/site.go b/hugolib/site.go index 3a34efc4d..2da0b77c1 100644 --- a/hugolib/site.go +++ b/hugolib/site.go @@ -76,7 +76,7 @@ type Site struct { Shortcodes map[string]ShortcodeFunc timer *nitro.B Target target.Output - Alias target.Translator + Alias target.AliasPublisher } type SiteInfo struct { @@ -405,15 +405,7 @@ func inStringArray(arr []string, el string) bool { 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 { - return err - } - if err = s.WriteAlias(a, content.Bytes()); err != nil { + if err := s.WriteAlias(a, p.Permalink()); err != nil { return err } } @@ -657,18 +649,17 @@ func (s *Site) WritePublic(path string, content []byte) (err error) { 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 { 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) } diff --git a/hugolib/site_test.go b/hugolib/site_test.go index 18b3afd7b..bbcf0f80a 100644 --- a/hugolib/site_test.go +++ b/hugolib/site_test.go @@ -52,14 +52,6 @@ func TestDegenerateRenderThingMissingTemplate(t *testing.T) { } } -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() diff --git a/hugolib/site_url_test.go b/hugolib/site_url_test.go index 5f741ae83..c5146b92d 100644 --- a/hugolib/site_url_test.go +++ b/hugolib/site_url_test.go @@ -4,6 +4,8 @@ import ( "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,15 +46,29 @@ func (t *InMemoryTarget) Translate(label string) (dest string, err error) { 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)}, } 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}, } diff --git a/target/file.go b/target/file.go index f5ae62db3..844d446c5 100644 --- a/target/file.go +++ b/target/file.go @@ -34,7 +34,11 @@ func (fs *Filesystem) Publish(path string, r io.Reader) (err error) { 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 != "" { diff --git a/target/htmlredirect.go b/target/htmlredirect.go index 3305e2921..398d3fefa 100644 --- a/target/htmlredirect.go +++ b/target/htmlredirect.go @@ -3,11 +3,30 @@ package target import ( helpers "github.com/spf13/hugo/template" "path" + "bytes" "strings" + "html/template" ) +const ALIAS = "" +const ALIAS_XHTML = "" + +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) { @@ -16,3 +35,31 @@ func (h *HTMLRedirectAlias) Translate(alias string) (aliasPath string, err error } 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) +} diff --git a/template/bundle/bundle_test.go b/template/bundle/bundle_test.go deleted file mode 100644 index 1951a71c5..000000000 --- a/template/bundle/bundle_test.go +++ /dev/null @@ -1,12 +0,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") - } -} diff --git a/template/bundle/template.go b/template/bundle/template.go index 7a194a71d..6d1653da8 100644 --- a/template/bundle/template.go +++ b/template/bundle/template.go @@ -116,7 +116,6 @@ func NewTemplate() Template { } templates.Funcs(funcMap) - templates.primeTemplates() return templates } @@ -145,14 +144,6 @@ func (t *GoHtmlTemplate) generateTemplateNameFrom(base, path string) string { return filepath.ToSlash(path[len(base)+1:]) } -func (t *GoHtmlTemplate) primeTemplates() { - alias := "\n \n \n \n \n \n \n " - alias_xhtml := "\n \n \n \n \n \n \n " - - t.AddTemplate("alias", alias) - t.AddTemplate("alias-xhtml", alias_xhtml) -} - func ignoreDotFile(path string) bool { return filepath.Base(path)[0] == '.' }