mirror of
https://github.com/gohugoio/hugo.git
synced 2024-11-21 20:46:30 -05:00
Move to target.Filesystem
Moving the ugly urls logic to the target. There is still UglyUrl logic in page for the permlink but this is dealing with the generate of urls.
This commit is contained in:
parent
7919603fb5
commit
4004687fb2
3 changed files with 65 additions and 57 deletions
|
@ -329,21 +329,11 @@ func (s *Site) setOutFile(p *Page) {
|
|||
|
||||
var outfile string
|
||||
if len(strings.TrimSpace(p.Slug)) > 0 {
|
||||
// Use Slug if provided
|
||||
if s.Config.UglyUrls {
|
||||
outfile = strings.TrimSpace(p.Slug) + "." + p.Extension
|
||||
} else {
|
||||
outfile = filepath.Join(strings.TrimSpace(p.Slug), "index."+p.Extension)
|
||||
}
|
||||
outfile = strings.TrimSpace(p.Slug) + "." + p.Extension
|
||||
} else {
|
||||
// Fall back to filename
|
||||
_, t := filepath.Split(p.FileName)
|
||||
if s.Config.UglyUrls {
|
||||
outfile = replaceExtension(strings.TrimSpace(t), p.Extension)
|
||||
} else {
|
||||
file, _ := fileExt(strings.TrimSpace(t))
|
||||
outfile = filepath.Join(file, "index."+p.Extension)
|
||||
}
|
||||
outfile = replaceExtension(strings.TrimSpace(t), p.Extension)
|
||||
}
|
||||
|
||||
p.OutFile = p.Path + string(os.PathSeparator) + strings.TrimSpace(outfile)
|
||||
|
@ -467,13 +457,8 @@ func (s *Site) RenderIndexes() error {
|
|||
n := s.NewNode()
|
||||
n.Title = strings.Title(k)
|
||||
url := helpers.Urlize(plural + "/" + k)
|
||||
plink := url
|
||||
if s.Config.UglyUrls {
|
||||
n.Url = url + ".html"
|
||||
plink = n.Url
|
||||
} else {
|
||||
n.Url = url + "/index.html"
|
||||
}
|
||||
n.Url = url + ".html"
|
||||
plink := n.Url
|
||||
n.Permalink = permalink(s, plink)
|
||||
n.RSSlink = permalink(s, url+".xml")
|
||||
n.Date = o[0].Date
|
||||
|
@ -486,12 +471,7 @@ func (s *Site) RenderIndexes() error {
|
|||
}
|
||||
|
||||
var base string
|
||||
if s.Config.UglyUrls {
|
||||
base = plural + "/" + k
|
||||
} else {
|
||||
base = plural + "/" + k + "/" + "index"
|
||||
}
|
||||
|
||||
base = plural + "/" + k
|
||||
err = s.WritePublic(base+".html", x.Bytes())
|
||||
if err != nil {
|
||||
return err
|
||||
|
@ -500,11 +480,7 @@ func (s *Site) RenderIndexes() error {
|
|||
if a := s.Tmpl.Lookup("rss.xml"); a != nil {
|
||||
// XML Feed
|
||||
y := s.NewXMLBuffer()
|
||||
if s.Config.UglyUrls {
|
||||
n.Url = helpers.Urlize(plural + "/" + k + ".xml")
|
||||
} else {
|
||||
n.Url = helpers.Urlize(plural + "/" + k + "/" + "index.xml")
|
||||
}
|
||||
n.Url = helpers.Urlize(plural + "/" + k + ".xml")
|
||||
n.Permalink = permalink(s, n.Url)
|
||||
s.Tmpl.ExecuteTemplate(y, "rss.xml", n)
|
||||
err = s.WritePublic(base+".xml", y.Bytes())
|
||||
|
@ -567,11 +543,7 @@ func (s *Site) RenderLists() error {
|
|||
|
||||
if a := s.Tmpl.Lookup("rss.xml"); a != nil {
|
||||
// XML Feed
|
||||
if s.Config.UglyUrls {
|
||||
n.Url = helpers.Urlize(section + ".xml")
|
||||
} else {
|
||||
n.Url = helpers.Urlize(section + "/" + "index.xml")
|
||||
}
|
||||
n.Url = Urlize(section + ".xml")
|
||||
n.Permalink = template.HTML(string(n.Site.BaseUrl) + n.Url)
|
||||
y := s.NewXMLBuffer()
|
||||
s.Tmpl.ExecuteTemplate(y, "rss.xml", n)
|
||||
|
@ -600,7 +572,7 @@ func (s *Site) RenderHomePage() error {
|
|||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = s.WritePublic("index.html", x.Bytes())
|
||||
err = s.WritePublic("/", x.Bytes())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -677,25 +649,16 @@ func (s *Site) NewXMLBuffer() *bytes.Buffer {
|
|||
|
||||
func (s *Site) WritePublic(path string, content []byte) (err error) {
|
||||
|
||||
if s.Target != nil {
|
||||
return s.Target.Publish(path, bytes.NewReader(content))
|
||||
if s.Target == nil {
|
||||
s.Target = &target.Filesystem{
|
||||
PublishDir: s.absPublishDir(),
|
||||
UglyUrls: s.Config.UglyUrls,
|
||||
}
|
||||
}
|
||||
|
||||
if s.Config.Verbose {
|
||||
fmt.Println(path)
|
||||
}
|
||||
|
||||
path, filename := filepath.Split(path)
|
||||
|
||||
path = filepath.FromSlash(s.Config.GetAbsPath(filepath.Join(s.Config.PublishDir, path)))
|
||||
err = mkdirIf(path)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
file, _ := os.Create(filepath.Join(path, filename))
|
||||
defer file.Close()
|
||||
|
||||
_, err = file.Write(content)
|
||||
return
|
||||
return s.Target.Publish(path, bytes.NewReader(content))
|
||||
}
|
||||
|
|
|
@ -3,7 +3,9 @@ package target
|
|||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
type Publisher interface {
|
||||
|
@ -17,9 +19,39 @@ type Translator interface {
|
|||
type Filesystem struct {
|
||||
UglyUrls bool
|
||||
DefaultExtension string
|
||||
PublishDir string
|
||||
}
|
||||
|
||||
func (fs *Filesystem) Publish(path string, r io.Reader) (err error) {
|
||||
|
||||
translated, err := fs.Translate(path)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
path, _ = filepath.Split(translated)
|
||||
dest := filepath.Join(fs.PublishDir, path)
|
||||
ospath := filepath.FromSlash(dest)
|
||||
|
||||
err = os.MkdirAll(ospath, 0764) // rwx, rw, r
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
file, err := os.Create(filepath.Join(fs.PublishDir, translated))
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
_, err = io.Copy(file, r)
|
||||
return
|
||||
}
|
||||
|
||||
func (fs *Filesystem) Translate(src string) (dest string, err error) {
|
||||
if src == "/" {
|
||||
return "index.html", nil
|
||||
}
|
||||
if fs.UglyUrls {
|
||||
return src, nil
|
||||
}
|
||||
|
|
|
@ -9,6 +9,8 @@ func TestFileTranslator(t *testing.T) {
|
|||
content string
|
||||
expected string
|
||||
}{
|
||||
{"/", "index.html"},
|
||||
{"index.html", "index/index.html"},
|
||||
{"foo", "foo/index.html"},
|
||||
{"foo.html", "foo/index.html"},
|
||||
{"foo.xhtml", "foo/index.xhtml"},
|
||||
|
@ -31,14 +33,25 @@ func TestFileTranslator(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestTranslateUglyUrls(t *testing.T) {
|
||||
f := &Filesystem{UglyUrls: true}
|
||||
dest, err := f.Translate("foo.html")
|
||||
if err != nil {
|
||||
t.Fatalf("Translate returned an unexpected err: %s", err)
|
||||
tests := []struct {
|
||||
content string
|
||||
expected string
|
||||
}{
|
||||
{"foo.html", "foo.html"},
|
||||
{"/", "index.html"},
|
||||
{"index.html", "index.html"},
|
||||
}
|
||||
|
||||
if dest != "foo.html" {
|
||||
t.Errorf("Translate expected return: %s, got: %s", "foo.html", dest)
|
||||
for _, test := range tests {
|
||||
f := &Filesystem{UglyUrls: true}
|
||||
dest, err := f.Translate(test.content)
|
||||
if err != nil {
|
||||
t.Fatalf("Translate returned an unexpected err: %s", err)
|
||||
}
|
||||
|
||||
if dest != test.expected {
|
||||
t.Errorf("Translate expected return: %s, got: %s", test.expected, dest)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue