mirror of
https://github.com/gohugoio/hugo.git
synced 2025-08-28 22:19:59 +02:00
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:
@@ -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)
|
||||
}
|
||||
|
@@ -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()
|
||||
|
@@ -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},
|
||||
}
|
||||
|
Reference in New Issue
Block a user