mirror of
https://github.com/gohugoio/hugo.git
synced 2024-11-21 20:46:30 -05: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:
parent
2f10da1570
commit
2ebfb33fe0
7 changed files with 76 additions and 47 deletions
|
@ -76,7 +76,7 @@ type Site struct {
|
||||||
Shortcodes map[string]ShortcodeFunc
|
Shortcodes map[string]ShortcodeFunc
|
||||||
timer *nitro.B
|
timer *nitro.B
|
||||||
Target target.Output
|
Target target.Output
|
||||||
Alias target.Translator
|
Alias target.AliasPublisher
|
||||||
}
|
}
|
||||||
|
|
||||||
type SiteInfo struct {
|
type SiteInfo struct {
|
||||||
|
@ -405,15 +405,7 @@ func inStringArray(arr []string, el string) bool {
|
||||||
func (s *Site) RenderAliases() error {
|
func (s *Site) RenderAliases() error {
|
||||||
for _, p := range s.Pages {
|
for _, p := range s.Pages {
|
||||||
for _, a := range p.Aliases {
|
for _, a := range p.Aliases {
|
||||||
t := "alias"
|
if err := s.WriteAlias(a, p.Permalink()); err != nil {
|
||||||
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 {
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -657,18 +649,17 @@ func (s *Site) WritePublic(path string, content []byte) (err error) {
|
||||||
return s.Target.Publish(path, bytes.NewReader(content))
|
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 {
|
if s.Alias == nil {
|
||||||
s.initTarget()
|
s.initTarget()
|
||||||
s.Alias = new(target.HTMLRedirectAlias)
|
s.Alias = &target.HTMLRedirectAlias{
|
||||||
|
PublishDir: s.absPublishDir(),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if s.Config.Verbose {
|
if s.Config.Verbose {
|
||||||
fmt.Println(path)
|
fmt.Println(path)
|
||||||
}
|
}
|
||||||
|
|
||||||
if path, err = s.Alias.Translate(path); err != nil {
|
return s.Alias.Publish(path, permalink)
|
||||||
return err
|
|
||||||
}
|
|
||||||
return s.Target.Publish(path, bytes.NewReader(content))
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -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) {
|
func TestAddInvalidTemplate(t *testing.T) {
|
||||||
s := new(Site)
|
s := new(Site)
|
||||||
s.prepTemplates()
|
s.prepTemplates()
|
||||||
|
|
|
@ -4,6 +4,8 @@ import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"io"
|
"io"
|
||||||
"testing"
|
"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"
|
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
|
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{
|
var urlFakeSource = []byteSource{
|
||||||
{"content/blue/doc1.md", []byte(SLUG_DOC_1)},
|
{"content/blue/doc1.md", []byte(SLUG_DOC_1)},
|
||||||
{"content/blue/doc2.md", []byte(SLUG_DOC_2)},
|
{"content/blue/doc2.md", []byte(SLUG_DOC_2)},
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestPageCount(t *testing.T) {
|
func TestPageCount(t *testing.T) {
|
||||||
target := new(InMemoryTarget)
|
files := make(map[string][]byte)
|
||||||
|
target := &InMemoryTarget{files: files}
|
||||||
|
alias := &InMemoryAliasTarget{files: files}
|
||||||
s := &Site{
|
s := &Site{
|
||||||
Target: target,
|
Target: target,
|
||||||
|
Alias: alias,
|
||||||
Config: Config{UglyUrls: false},
|
Config: Config{UglyUrls: false},
|
||||||
Source: &inMemorySource{urlFakeSource},
|
Source: &inMemorySource{urlFakeSource},
|
||||||
}
|
}
|
||||||
|
|
|
@ -34,7 +34,11 @@ func (fs *Filesystem) Publish(path string, r io.Reader) (err error) {
|
||||||
return
|
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)
|
ospath := filepath.FromSlash(path)
|
||||||
|
|
||||||
if ospath != "" {
|
if ospath != "" {
|
||||||
|
|
|
@ -3,11 +3,30 @@ package target
|
||||||
import (
|
import (
|
||||||
helpers "github.com/spf13/hugo/template"
|
helpers "github.com/spf13/hugo/template"
|
||||||
"path"
|
"path"
|
||||||
|
"bytes"
|
||||||
"strings"
|
"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 {
|
type HTMLRedirectAlias struct {
|
||||||
PublishDir string
|
PublishDir string
|
||||||
|
Templates *template.Template
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *HTMLRedirectAlias) Translate(alias string) (aliasPath string, err error) {
|
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
|
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)
|
||||||
|
}
|
||||||
|
|
|
@ -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")
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -116,7 +116,6 @@ func NewTemplate() Template {
|
||||||
}
|
}
|
||||||
|
|
||||||
templates.Funcs(funcMap)
|
templates.Funcs(funcMap)
|
||||||
templates.primeTemplates()
|
|
||||||
return templates
|
return templates
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -145,14 +144,6 @@ func (t *GoHtmlTemplate) generateTemplateNameFrom(base, path string) string {
|
||||||
return filepath.ToSlash(path[len(base)+1:])
|
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 {
|
func ignoreDotFile(path string) bool {
|
||||||
return filepath.Base(path)[0] == '.'
|
return filepath.Base(path)[0] == '.'
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue