From: Noah Campbell <noahcampbell@gmail.com> Date: Fri, 13 Sep 2013 04:18:13 +0000 (-0700) Subject: Move alias logic to target module X-Git-Tag: v0.9~105 X-Git-Url: http://git.maquefel.me/?a=commitdiff_plain;h=2ebfb33fe01d38a1341d6fe3c922dad5e93fd769;p=brevno-suite%2Fhugo 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). --- diff --git a/hugolib/site.go b/hugolib/site.go index 3a34efc4..2da0b77c 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 18b3afd7..bbcf0f80 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 5f741ae8..c5146b92 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 f5ae62db..844d446c 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 3305e292..398d3fef 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 = "<!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) { @@ -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 1951a71c..00000000 --- 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 7a194a71..6d1653da 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 := "<!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 { return filepath.Base(path)[0] == '.' }