Section is determined by the source, not the url
This change allows for top level html content to exists.
This commit is contained in:
parent
784077da4d
commit
52e8c7a0ac
12 changed files with 265 additions and 142 deletions
|
@ -35,5 +35,4 @@ type UrlPath struct {
|
||||||
Permalink template.HTML
|
Permalink template.HTML
|
||||||
Slug string
|
Slug string
|
||||||
Section string
|
Section string
|
||||||
Path string
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -52,7 +52,7 @@ type Page struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
type File struct {
|
type File struct {
|
||||||
FileName, OutFile, Extension string
|
FileName, OutFile, Extension, Dir string
|
||||||
}
|
}
|
||||||
|
|
||||||
type PageMeta struct {
|
type PageMeta struct {
|
||||||
|
|
|
@ -37,10 +37,6 @@ func MakePermalink(domain string, path string) string {
|
||||||
return strings.TrimRight(domain, "/") + "/" + strings.TrimLeft(path, "/")
|
return strings.TrimRight(domain, "/") + "/" + strings.TrimLeft(path, "/")
|
||||||
}
|
}
|
||||||
|
|
||||||
func mkdirIf(path string) error {
|
|
||||||
return os.MkdirAll(path, 0777)
|
|
||||||
}
|
|
||||||
|
|
||||||
func FatalErr(str string) {
|
func FatalErr(str string) {
|
||||||
fmt.Println(str)
|
fmt.Println(str)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
|
@ -150,6 +146,18 @@ func (s *Site) Process() (err error) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *Site) setupPrevNext() {
|
||||||
|
for i, page := range s.Pages {
|
||||||
|
if i < len(s.Pages)-1 {
|
||||||
|
page.Next = s.Pages[i+1]
|
||||||
|
}
|
||||||
|
|
||||||
|
if i > 0 {
|
||||||
|
page.Prev = s.Pages[i-1]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (s *Site) Render() (err error) {
|
func (s *Site) Render() (err error) {
|
||||||
if err = s.RenderAliases(); err != nil {
|
if err = s.RenderAliases(); err != nil {
|
||||||
return
|
return
|
||||||
|
@ -239,7 +247,6 @@ func (s *Site) checkDirectories() {
|
||||||
if b, _ := dirExists(s.absContentDir()); !b {
|
if b, _ := dirExists(s.absContentDir()); !b {
|
||||||
FatalErr("No source directory found, expecting to find it at " + s.absContentDir())
|
FatalErr("No source directory found, expecting to find it at " + s.absContentDir())
|
||||||
}
|
}
|
||||||
mkdirIf(s.absPublishDir())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Site) ProcessShortcodes() {
|
func (s *Site) ProcessShortcodes() {
|
||||||
|
@ -250,16 +257,17 @@ func (s *Site) ProcessShortcodes() {
|
||||||
|
|
||||||
func (s *Site) CreatePages() (err error) {
|
func (s *Site) CreatePages() (err error) {
|
||||||
for _, file := range s.Source.Files() {
|
for _, file := range s.Source.Files() {
|
||||||
page, err := ReadFrom(file.Contents, file.Name)
|
page, err := ReadFrom(file.Contents, file.LogicalName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
page.Site = s.Info
|
page.Site = s.Info
|
||||||
page.Tmpl = s.Tmpl
|
page.Tmpl = s.Tmpl
|
||||||
|
page.Section = file.Section
|
||||||
|
page.Dir = file.Dir
|
||||||
if err = s.setUrlPath(page); err != nil {
|
if err = s.setUrlPath(page); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
s.setOutFile(page)
|
|
||||||
if s.Config.BuildDrafts || !page.Draft {
|
if s.Config.BuildDrafts || !page.Draft {
|
||||||
s.Pages = append(s.Pages, page)
|
s.Pages = append(s.Pages, page)
|
||||||
}
|
}
|
||||||
|
@ -269,36 +277,11 @@ func (s *Site) CreatePages() (err error) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Site) setupPrevNext() {
|
// Set p.Section and p.OutFile relying on p.FileName as the source.
|
||||||
for i, page := range s.Pages {
|
// Filename is broken apart for a "Section" which basically equates to
|
||||||
if i < len(s.Pages)-1 {
|
// the folder the file exists in.
|
||||||
page.Next = s.Pages[i+1]
|
func (s *Site) setUrlPath(p *Page) (err error) {
|
||||||
}
|
|
||||||
|
|
||||||
if i > 0 {
|
|
||||||
page.Prev = s.Pages[i-1]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *Site) setUrlPath(p *Page) error {
|
|
||||||
y := strings.TrimPrefix(p.FileName, s.absContentDir())
|
|
||||||
x := strings.Split(y, "/")
|
|
||||||
|
|
||||||
if len(x) <= 1 {
|
|
||||||
return fmt.Errorf("Zero length page name. filename: %s", y)
|
|
||||||
}
|
|
||||||
|
|
||||||
p.Section = strings.Trim(x[1], "/")
|
|
||||||
p.Path = path.Join(x[:len(x)-1]...)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// If Url is provided it is assumed to be the complete relative path
|
|
||||||
// and will override everything
|
|
||||||
// Otherwise path + slug is used if provided
|
|
||||||
// Lastly path + filename is used if provided
|
|
||||||
func (s *Site) setOutFile(p *Page) {
|
|
||||||
// Always use Url if it's specified
|
// Always use Url if it's specified
|
||||||
if len(strings.TrimSpace(p.Url)) > 2 {
|
if len(strings.TrimSpace(p.Url)) > 2 {
|
||||||
p.OutFile = strings.TrimSpace(p.Url)
|
p.OutFile = strings.TrimSpace(p.Url)
|
||||||
|
@ -318,7 +301,8 @@ func (s *Site) setOutFile(p *Page) {
|
||||||
outfile = replaceExtension(strings.TrimSpace(t), p.Extension)
|
outfile = replaceExtension(strings.TrimSpace(t), p.Extension)
|
||||||
}
|
}
|
||||||
|
|
||||||
p.OutFile = p.Path + "/" + strings.TrimSpace(outfile)
|
p.OutFile = p.Dir + "/" + strings.TrimSpace(outfile)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Site) BuildSiteMeta() (err error) {
|
func (s *Site) BuildSiteMeta() (err error) {
|
||||||
|
|
|
@ -9,30 +9,19 @@ import (
|
||||||
|
|
||||||
const ALIAS_DOC_1 = "---\ntitle: alias doc\naliases:\n - \"alias1/\"\n - \"alias-2/\"\n---\naliases\n"
|
const ALIAS_DOC_1 = "---\ntitle: alias doc\naliases:\n - \"alias1/\"\n - \"alias-2/\"\n---\naliases\n"
|
||||||
|
|
||||||
type byteSource struct {
|
var fakeSource = []source.ByteSource{
|
||||||
name string
|
{
|
||||||
content []byte
|
Name: "foo/bar/file.md",
|
||||||
}
|
Content: []byte(SIMPLE_PAGE),
|
||||||
|
},
|
||||||
var fakeSource = []byteSource{
|
{
|
||||||
{"foo/bar/file.md", []byte(SIMPLE_PAGE)},
|
Name: "alias/test/file1.md",
|
||||||
{"alias/test/file1.md", []byte(ALIAS_DOC_1)},
|
Content: []byte(ALIAS_DOC_1),
|
||||||
{"section/somecontent.html", []byte(RENDER_NO_FRONT_MATTER)},
|
},
|
||||||
}
|
{
|
||||||
|
Name: "section/somecontent.html",
|
||||||
type inMemorySource struct {
|
Content: []byte(RENDER_NO_FRONT_MATTER),
|
||||||
byteSource []byteSource
|
},
|
||||||
}
|
|
||||||
|
|
||||||
func (i *inMemorySource) Files() (files []*source.File) {
|
|
||||||
files = make([]*source.File, len(i.byteSource))
|
|
||||||
for i, fake := range i.byteSource {
|
|
||||||
files[i] = &source.File{
|
|
||||||
Name: fake.name,
|
|
||||||
Contents: bytes.NewReader(fake.content),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func checkShowPlanExpected(t *testing.T, s *Site, expected string) {
|
func checkShowPlanExpected(t *testing.T, s *Site, expected string) {
|
||||||
|
@ -52,7 +41,7 @@ func TestDegenerateNoFiles(t *testing.T) {
|
||||||
|
|
||||||
func TestDegenerateNoTarget(t *testing.T) {
|
func TestDegenerateNoTarget(t *testing.T) {
|
||||||
s := &Site{
|
s := &Site{
|
||||||
Source: &inMemorySource{fakeSource},
|
Source: &source.InMemorySource{fakeSource},
|
||||||
}
|
}
|
||||||
must(s.CreatePages())
|
must(s.CreatePages())
|
||||||
expected := "foo/bar/file.md (renderer: markdown)\n canonical => !no target specified!\n\n" +
|
expected := "foo/bar/file.md (renderer: markdown)\n canonical => !no target specified!\n\n" +
|
||||||
|
@ -63,7 +52,7 @@ func TestDegenerateNoTarget(t *testing.T) {
|
||||||
|
|
||||||
func TestFileTarget(t *testing.T) {
|
func TestFileTarget(t *testing.T) {
|
||||||
s := &Site{
|
s := &Site{
|
||||||
Source: &inMemorySource{fakeSource},
|
Source: &source.InMemorySource{fakeSource},
|
||||||
Target: new(target.Filesystem),
|
Target: new(target.Filesystem),
|
||||||
Alias: new(target.HTMLRedirectAlias),
|
Alias: new(target.HTMLRedirectAlias),
|
||||||
}
|
}
|
||||||
|
@ -81,7 +70,7 @@ func TestFileTarget(t *testing.T) {
|
||||||
func TestFileTargetUgly(t *testing.T) {
|
func TestFileTargetUgly(t *testing.T) {
|
||||||
s := &Site{
|
s := &Site{
|
||||||
Target: &target.Filesystem{UglyUrls: true},
|
Target: &target.Filesystem{UglyUrls: true},
|
||||||
Source: &inMemorySource{fakeSource},
|
Source: &source.InMemorySource{fakeSource},
|
||||||
Alias: new(target.HTMLRedirectAlias),
|
Alias: new(target.HTMLRedirectAlias),
|
||||||
}
|
}
|
||||||
s.CreatePages()
|
s.CreatePages()
|
||||||
|
@ -97,7 +86,7 @@ func TestFileTargetUgly(t *testing.T) {
|
||||||
func TestFileTargetPublishDir(t *testing.T) {
|
func TestFileTargetPublishDir(t *testing.T) {
|
||||||
s := &Site{
|
s := &Site{
|
||||||
Target: &target.Filesystem{PublishDir: "../public"},
|
Target: &target.Filesystem{PublishDir: "../public"},
|
||||||
Source: &inMemorySource{fakeSource},
|
Source: &source.InMemorySource{fakeSource},
|
||||||
Alias: &target.HTMLRedirectAlias{PublishDir: "../public"},
|
Alias: &target.HTMLRedirectAlias{PublishDir: "../public"},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -3,6 +3,7 @@ package hugolib
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"github.com/spf13/hugo/source"
|
||||||
"html/template"
|
"html/template"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
@ -74,25 +75,6 @@ func matchRender(t *testing.T, s *Site, p *Page, tmplName string, expected strin
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func _TestAddSameTemplateTwice(t *testing.T) {
|
|
||||||
p := pageMust(ReadFrom(strings.NewReader(PAGE_SIMPLE_TITLE), "content/a/file.md"))
|
|
||||||
s := new(Site)
|
|
||||||
s.prepTemplates()
|
|
||||||
err := s.addTemplate("foo", TEMPLATE_TITLE)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("Unable to add template foo")
|
|
||||||
}
|
|
||||||
|
|
||||||
matchRender(t, s, p, "foo", "simple template")
|
|
||||||
|
|
||||||
err = s.addTemplate("foo", "NEW {{ .Title }}")
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("Unable to add template foo: %s", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
matchRender(t, s, p, "foo", "NEW simple template")
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestRenderThing(t *testing.T) {
|
func TestRenderThing(t *testing.T) {
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
content string
|
content string
|
||||||
|
@ -177,32 +159,59 @@ func TestRenderThingOrDefault(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSetOutFile(t *testing.T) {
|
func TestSetOutFile(t *testing.T) {
|
||||||
s := new(Site)
|
tests := []struct {
|
||||||
p := pageMust(ReadFrom(strings.NewReader(PAGE_URL_SPECIFIED), "content/a/file.md"))
|
doc string
|
||||||
s.setOutFile(p)
|
content string
|
||||||
|
expectedOutFile string
|
||||||
|
expectedSection string
|
||||||
|
}{
|
||||||
|
{"content/a/file.md", PAGE_URL_SPECIFIED, "mycategory/my-whatever-content/index.html", "a"},
|
||||||
|
{"content/b/file.md", SIMPLE_PAGE, "b/file.html", "b"},
|
||||||
|
{"a/file.md", SIMPLE_PAGE, "a/file.html", "a"},
|
||||||
|
{"file.md", SIMPLE_PAGE, "file.html", ""},
|
||||||
|
}
|
||||||
|
|
||||||
expected := "mycategory/my-whatever-content/index.html"
|
if true {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
for _, test := range tests {
|
||||||
|
var err error
|
||||||
|
s := &Site{
|
||||||
|
Config: Config{ContentDir: "content"},
|
||||||
|
}
|
||||||
|
p := pageMust(ReadFrom(strings.NewReader(test.content), s.Config.GetAbsPath(test.doc)))
|
||||||
|
if err = s.setUrlPath(p); err != nil {
|
||||||
|
t.Fatalf("Unable to set urlpath: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
if p.OutFile != "mycategory/my-whatever-content/index.html" {
|
expected := test.expectedOutFile
|
||||||
t.Errorf("Outfile does not match. Expected '%s', got '%s'", expected, p.OutFile)
|
|
||||||
|
if p.OutFile != expected {
|
||||||
|
t.Errorf("%s => p.OutFile expected: '%s', got: '%s'", test.doc, expected, p.OutFile)
|
||||||
|
}
|
||||||
|
|
||||||
|
if p.Section != test.expectedSection {
|
||||||
|
t.Errorf("%s => p.Section expected: %s, got: %s", test.doc, test.expectedSection, p.Section)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSkipRender(t *testing.T) {
|
func TestSkipRender(t *testing.T) {
|
||||||
files := make(map[string][]byte)
|
files := make(map[string][]byte)
|
||||||
target := &InMemoryTarget{files: files}
|
target := &InMemoryTarget{files: files}
|
||||||
sources := []byteSource{
|
sources := []source.ByteSource{
|
||||||
{"sect/doc1.html", []byte("---\nmarkup: markdown\n---\n# title\nsome *content*")},
|
{"sect/doc1.html", []byte("---\nmarkup: markdown\n---\n# title\nsome *content*"), "sect"},
|
||||||
{"sect/doc2.html", []byte("<!doctype html><html><body>more content</body></html>")},
|
{"sect/doc2.html", []byte("<!doctype html><html><body>more content</body></html>"), "sect"},
|
||||||
{"sect/doc3.md", []byte("# doc3\n*some* content")},
|
{"sect/doc3.md", []byte("# doc3\n*some* content"), "sect"},
|
||||||
{"sect/doc4.md", []byte("---\ntitle: doc4\n---\n# doc4\n*some content*")},
|
{"sect/doc4.md", []byte("---\ntitle: doc4\n---\n# doc4\n*some content*"), "sect"},
|
||||||
{"sect/doc5.html", []byte("<!doctype html><html>{{ template \"head\" }}<body>body5</body></html>")},
|
{"sect/doc5.html", []byte("<!doctype html><html>{{ template \"head\" }}<body>body5</body></html>"), "sect"},
|
||||||
|
{"doc7.html", []byte("<html><body>doc7 content</body></html>"), ""},
|
||||||
}
|
}
|
||||||
|
|
||||||
s := &Site{
|
s := &Site{
|
||||||
Target: target,
|
Target: target,
|
||||||
Config: Config{BaseUrl: "http://auth/bub/"},
|
Config: Config{BaseUrl: "http://auth/bub/"},
|
||||||
Source: &inMemorySource{sources},
|
Source: &source.InMemorySource{sources},
|
||||||
}
|
}
|
||||||
s.initializeSiteInfo()
|
s.initializeSiteInfo()
|
||||||
s.prepTemplates()
|
s.prepTemplates()
|
||||||
|
@ -231,6 +240,7 @@ func TestSkipRender(t *testing.T) {
|
||||||
{"sect/doc3.html", "<html><head></head><body><h1>doc3</h1>\n\n<p><em>some</em> content</p>\n</body></html>"},
|
{"sect/doc3.html", "<html><head></head><body><h1>doc3</h1>\n\n<p><em>some</em> content</p>\n</body></html>"},
|
||||||
{"sect/doc4.html", "<html><head></head><body><h1>doc4</h1>\n\n<p><em>some content</em></p>\n</body></html>"},
|
{"sect/doc4.html", "<html><head></head><body><h1>doc4</h1>\n\n<p><em>some content</em></p>\n</body></html>"},
|
||||||
{"sect/doc5.html", "<!DOCTYPE html><html><head><script src=\"http://auth/bub/script.js\"></script></head><body>body5</body></html>"},
|
{"sect/doc5.html", "<!DOCTYPE html><html><head><script src=\"http://auth/bub/script.js\"></script></head><body>body5</body></html>"},
|
||||||
|
{"./doc7.html", "<html><head></head><body>doc7 content</body></html>"},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, test := range tests {
|
for _, test := range tests {
|
||||||
|
@ -248,14 +258,14 @@ func TestSkipRender(t *testing.T) {
|
||||||
func TestAbsUrlify(t *testing.T) {
|
func TestAbsUrlify(t *testing.T) {
|
||||||
files := make(map[string][]byte)
|
files := make(map[string][]byte)
|
||||||
target := &InMemoryTarget{files: files}
|
target := &InMemoryTarget{files: files}
|
||||||
sources := []byteSource{
|
sources := []source.ByteSource{
|
||||||
{"sect/doc1.html", []byte("<!doctype html><html><head></head><body><a href=\"#frag1\">link</a></body></html>")},
|
{"sect/doc1.html", []byte("<!doctype html><html><head></head><body><a href=\"#frag1\">link</a></body></html>"), "sect"},
|
||||||
{"content/blue/doc2.html", []byte("---\nf: t\n---\n<!doctype html><html><body>more content</body></html>")},
|
{"content/blue/doc2.html", []byte("---\nf: t\n---\n<!doctype html><html><body>more content</body></html>"), "blue"},
|
||||||
}
|
}
|
||||||
s := &Site{
|
s := &Site{
|
||||||
Target: target,
|
Target: target,
|
||||||
Config: Config{BaseUrl: "http://auth/bub/"},
|
Config: Config{BaseUrl: "http://auth/bub/"},
|
||||||
Source: &inMemorySource{sources},
|
Source: &source.InMemorySource{sources},
|
||||||
}
|
}
|
||||||
s.initializeSiteInfo()
|
s.initializeSiteInfo()
|
||||||
s.prepTemplates()
|
s.prepTemplates()
|
||||||
|
@ -281,14 +291,14 @@ func TestAbsUrlify(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, test := range tests {
|
for _, test := range tests {
|
||||||
content, ok := target.files[test.file]
|
content, ok := target.files[test.file]
|
||||||
if !ok {
|
if !ok {
|
||||||
t.Fatalf("Unable to locate rendered content: %s", test.file)
|
t.Fatalf("Unable to locate rendered content: %s", test.file)
|
||||||
}
|
}
|
||||||
|
|
||||||
expected := test.expected
|
expected := test.expected
|
||||||
if string(content) != expected {
|
if string(content) != expected {
|
||||||
t.Errorf("AbsUrlify content expected:\n%q\ngot\n%q", expected, string(content))
|
t.Errorf("AbsUrlify content expected:\n%q\ngot\n%q", expected, string(content))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
|
@ -2,6 +2,7 @@ package hugolib
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"github.com/spf13/hugo/source"
|
||||||
"github.com/spf13/hugo/target"
|
"github.com/spf13/hugo/target"
|
||||||
"html/template"
|
"html/template"
|
||||||
"io"
|
"io"
|
||||||
|
@ -61,9 +62,9 @@ func (t *InMemoryAliasTarget) Publish(label string, permalink template.HTML) (er
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
var urlFakeSource = []byteSource{
|
var urlFakeSource = []source.ByteSource{
|
||||||
{"content/blue/doc1.md", []byte(SLUG_DOC_1)},
|
{"content/blue/doc1.md", []byte(SLUG_DOC_1), "blue"},
|
||||||
{"content/blue/doc2.md", []byte(SLUG_DOC_2)},
|
{"content/blue/doc2.md", []byte(SLUG_DOC_2), "blue"},
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestPageCount(t *testing.T) {
|
func TestPageCount(t *testing.T) {
|
||||||
|
@ -74,7 +75,7 @@ func TestPageCount(t *testing.T) {
|
||||||
Target: target,
|
Target: target,
|
||||||
Alias: alias,
|
Alias: alias,
|
||||||
Config: Config{UglyUrls: false},
|
Config: Config{UglyUrls: false},
|
||||||
Source: &inMemorySource{urlFakeSource},
|
Source: &source.InMemorySource{urlFakeSource},
|
||||||
}
|
}
|
||||||
s.initializeSiteInfo()
|
s.initializeSiteInfo()
|
||||||
s.prepTemplates()
|
s.prepTemplates()
|
||||||
|
|
|
@ -1,8 +1,10 @@
|
||||||
package source
|
package source
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
|
"path"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -11,8 +13,11 @@ type Input interface {
|
||||||
}
|
}
|
||||||
|
|
||||||
type File struct {
|
type File struct {
|
||||||
Name string
|
name string
|
||||||
Contents io.Reader
|
LogicalName string
|
||||||
|
Contents io.Reader
|
||||||
|
Section string
|
||||||
|
Dir string
|
||||||
}
|
}
|
||||||
|
|
||||||
type Filesystem struct {
|
type Filesystem struct {
|
||||||
|
@ -26,32 +31,66 @@ func (f *Filesystem) Files() []*File {
|
||||||
return f.files
|
return f.files
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *Filesystem) add(name string, reader io.Reader) {
|
var errMissingBaseDir = errors.New("source: missing base directory")
|
||||||
|
|
||||||
|
func (f *Filesystem) add(name string, reader io.Reader) (err error) {
|
||||||
|
|
||||||
|
if name, err = f.getRelativePath(name); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
dir, logical := path.Split(name)
|
||||||
|
_, section := path.Split(path.Dir(name))
|
||||||
|
if section == "." {
|
||||||
|
section = ""
|
||||||
|
}
|
||||||
|
|
||||||
|
f.files = append(f.files, &File{
|
||||||
|
name: name,
|
||||||
|
LogicalName: logical,
|
||||||
|
Contents: reader,
|
||||||
|
Section: section,
|
||||||
|
Dir: dir,
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *Filesystem) getRelativePath(name string) (final string, err error) {
|
||||||
|
if filepath.IsAbs(name) && f.Base == "" {
|
||||||
|
return "", errMissingBaseDir
|
||||||
|
}
|
||||||
|
name = filepath.Clean(name)
|
||||||
|
base := filepath.Clean(f.Base)
|
||||||
|
|
||||||
|
name, err = filepath.Rel(base, name)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
name = filepath.ToSlash(name)
|
name = filepath.ToSlash(name)
|
||||||
f.files = append(f.files, &File{Name: name, Contents: reader})
|
return name, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *Filesystem) captureFiles() {
|
func (f *Filesystem) captureFiles() {
|
||||||
|
|
||||||
walker := func(path string, fi os.FileInfo, err error) error {
|
walker := func(filePath string, fi os.FileInfo, err error) error {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if fi.IsDir() {
|
if fi.IsDir() {
|
||||||
if f.avoid(path) {
|
if f.avoid(filePath) {
|
||||||
return filepath.SkipDir
|
return filepath.SkipDir
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
} else {
|
} else {
|
||||||
if ignoreDotFile(path) {
|
if ignoreDotFile(filePath) {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
file, err := os.Open(path)
|
file, err := os.Open(filePath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
f.add(path, file)
|
f.add(filePath, file)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -59,15 +98,15 @@ func (f *Filesystem) captureFiles() {
|
||||||
filepath.Walk(f.Base, walker)
|
filepath.Walk(f.Base, walker)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *Filesystem) avoid(path string) bool {
|
func (f *Filesystem) avoid(filePath string) bool {
|
||||||
for _, avoid := range f.AvoidPaths {
|
for _, avoid := range f.AvoidPaths {
|
||||||
if avoid == path {
|
if avoid == filePath {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
func ignoreDotFile(path string) bool {
|
func ignoreDotFile(filePath string) bool {
|
||||||
return filepath.Base(path)[0] == '.'
|
return filepath.Base(filePath)[0] == '.'
|
||||||
}
|
}
|
||||||
|
|
13
source/filesystem_linux_test.go
Normal file
13
source/filesystem_linux_test.go
Normal file
|
@ -0,0 +1,13 @@
|
||||||
|
package source
|
||||||
|
|
||||||
|
//
|
||||||
|
// NOTE, any changes here need to be reflected in filesystem_windows_test.go
|
||||||
|
//
|
||||||
|
var platformBase = "foo/bar/boo/"
|
||||||
|
var platformPaths = []TestPath{
|
||||||
|
{"foobar", "foobar", "aaa", "", ""},
|
||||||
|
{"b/1file", "1file", "aaa", "b", "b/"},
|
||||||
|
{"c/d/2file", "2file", "aaa", "d", "c/d/"},
|
||||||
|
{"/e/f/3file", "3file", "aaa", "f", "e/f/"},
|
||||||
|
{"section\\foo.rss", "foo.rss", "aaa", "section", "section/"},
|
||||||
|
}
|
|
@ -2,6 +2,8 @@ package source
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"path"
|
||||||
|
"path/filepath"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -12,21 +14,58 @@ func TestEmptySourceFilesystem(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type TestPath struct {
|
||||||
|
filename string
|
||||||
|
logical string
|
||||||
|
content string
|
||||||
|
section string
|
||||||
|
dir string
|
||||||
|
}
|
||||||
|
|
||||||
func TestAddFile(t *testing.T) {
|
func TestAddFile(t *testing.T) {
|
||||||
src := new(Filesystem)
|
tests := platformPaths
|
||||||
src.add("foobar", bytes.NewReader([]byte("aaa")))
|
for _, test := range tests {
|
||||||
if len(src.Files()) != 1 {
|
base := platformBase
|
||||||
t.Errorf("Files() should return 1 file")
|
srcDefault := new(Filesystem)
|
||||||
}
|
srcWithBase := &Filesystem{
|
||||||
|
Base: base,
|
||||||
|
}
|
||||||
|
|
||||||
f := src.Files()[0]
|
for _, src := range []*Filesystem{srcDefault, srcWithBase} {
|
||||||
if f.Name != "foobar" {
|
p := test.filename
|
||||||
t.Errorf("File name should be 'foobar', got: %s", f.Name)
|
if !filepath.IsAbs(test.filename) {
|
||||||
}
|
p = path.Join(src.Base, test.filename)
|
||||||
|
}
|
||||||
|
|
||||||
b := new(bytes.Buffer)
|
if err := src.add(p, bytes.NewReader([]byte(test.content))); err != nil {
|
||||||
b.ReadFrom(f.Contents)
|
if err == errMissingBaseDir {
|
||||||
if b.String() != "aaa" {
|
continue
|
||||||
t.Errorf("File contents should be 'aaa', got: %s", b.String())
|
}
|
||||||
|
t.Fatalf("%s add returned and error: %s", p, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(src.Files()) != 1 {
|
||||||
|
t.Fatalf("%s Files() should return 1 file", p)
|
||||||
|
}
|
||||||
|
|
||||||
|
f := src.Files()[0]
|
||||||
|
if f.LogicalName != test.logical {
|
||||||
|
t.Errorf("Filename (Base: %q) expected: %q, got: %q", src.Base, test.logical, f.LogicalName)
|
||||||
|
}
|
||||||
|
|
||||||
|
b := new(bytes.Buffer)
|
||||||
|
b.ReadFrom(f.Contents)
|
||||||
|
if b.String() != test.content {
|
||||||
|
t.Errorf("File (Base: %q) contents should be %q, got: %q", src.Base, test.content, b.String())
|
||||||
|
}
|
||||||
|
|
||||||
|
if f.Section != test.section {
|
||||||
|
t.Errorf("File section (Base: %q) expected: %q, got: %q", src.Base, test.section, f.Section)
|
||||||
|
}
|
||||||
|
|
||||||
|
if f.Dir != test.dir {
|
||||||
|
t.Errorf("Dir path (Base: %q) expected: %q, got: %q", src.Base, test.dir, f.Dir)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
15
source/filesystem_windows_test.go
Normal file
15
source/filesystem_windows_test.go
Normal file
|
@ -0,0 +1,15 @@
|
||||||
|
package source
|
||||||
|
|
||||||
|
//
|
||||||
|
// NOTE, any changes here need to be reflected in filesystem_linux_test.go
|
||||||
|
//
|
||||||
|
|
||||||
|
// Note the case of the volume drive. It must be the same in all examples.
|
||||||
|
var platformBase = "C:\\foo\\"
|
||||||
|
var platformPaths = []TestPath{
|
||||||
|
{"foobar", "foobar", "aaa", "", ""},
|
||||||
|
{"b\\1file", "1file", "aaa", "b", "b/"},
|
||||||
|
{"c\\d\\2file", "2file", "aaa", "d", "c/d/"},
|
||||||
|
{"C:\\foo\\e\\f\\3file", "3file", "aaa", "f", "e/f/"}, // note volume case is equal to platformBase
|
||||||
|
{"section\\foo.rss", "foo.rss", "aaa", "section", "section/"},
|
||||||
|
}
|
34
source/inmemory.go
Normal file
34
source/inmemory.go
Normal file
|
@ -0,0 +1,34 @@
|
||||||
|
package source
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"fmt"
|
||||||
|
"path"
|
||||||
|
)
|
||||||
|
|
||||||
|
type ByteSource struct {
|
||||||
|
Name string
|
||||||
|
Content []byte
|
||||||
|
Section string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *ByteSource) String() string {
|
||||||
|
return fmt.Sprintf("%s %s %s", b.Name, b.Section, string(b.Content))
|
||||||
|
}
|
||||||
|
|
||||||
|
type InMemorySource struct {
|
||||||
|
ByteSource []ByteSource
|
||||||
|
}
|
||||||
|
|
||||||
|
func (i *InMemorySource) Files() (files []*File) {
|
||||||
|
files = make([]*File, len(i.ByteSource))
|
||||||
|
for i, fake := range i.ByteSource {
|
||||||
|
files[i] = &File{
|
||||||
|
LogicalName: fake.Name,
|
||||||
|
Contents: bytes.NewReader(fake.Content),
|
||||||
|
Section: fake.Section,
|
||||||
|
Dir: path.Dir(fake.Name),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
|
@ -44,7 +44,7 @@ func writeToDisk(translated string, r io.Reader) (err error) {
|
||||||
if ospath != "" {
|
if ospath != "" {
|
||||||
err = os.MkdirAll(ospath, 0764) // rwx, rw, r
|
err = os.MkdirAll(ospath, 0764) // rwx, rw, r
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
panic(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue