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).
This commit is contained in:
Noah Campbell 2013-09-12 21:18:13 -07:00
parent 2f10da1570
commit 2ebfb33fe0
7 changed files with 76 additions and 47 deletions

View file

@ -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)
}

View file

@ -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()

View file

@ -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},
}

View file

@ -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 != "" {

View file

@ -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)
}

View file

@ -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")
}
}

View file

@ -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] == '.'
}