diff options
author | Steve Francia <steve.francia@gmail.com> | 2016-01-07 21:48:13 -0500 |
---|---|---|
committer | Steve Francia <steve.francia@gmail.com> | 2016-01-26 14:26:23 -0500 |
commit | 9f3796a31dc217b2b8094f948266b23ac3808aa6 (patch) | |
tree | e31b0061e7ed687276c1811e211a5892c288bbb4 /source | |
parent | ca6ca4f4fc29906e491b5ac6b63fb65125c9c9e4 (diff) |
Read/reread individual source content files
next is incremental conversion
Diffstat (limited to 'source')
-rw-r--r-- | source/file.go | 12 | ||||
-rw-r--r-- | source/filesystem.go | 71 |
2 files changed, 48 insertions, 35 deletions
diff --git a/source/file.go b/source/file.go index a132cefde..efe604912 100644 --- a/source/file.go +++ b/source/file.go @@ -14,14 +14,16 @@ package source import ( - "github.com/spf13/hugo/helpers" "io" "path/filepath" "strings" + + "github.com/spf13/hugo/helpers" ) +// All paths are relative from the source directory base type File struct { - relpath string // Original Full Path eg. /Users/Home/Hugo/foo.txt + relpath string // Original Full Path eg. content/foo.txt logicalName string // foo.txt Contents io.Reader section string // The first directory @@ -30,6 +32,7 @@ type File struct { uniqueID string // MD5 of the filename } +// UniqueID: MD5 of the filename func (f *File) UniqueID() string { return f.uniqueID } @@ -42,15 +45,17 @@ func (f *File) Bytes() []byte { return helpers.ReaderToBytes(f.Contents) } -// Filename without extension +// BaseFileName Filename without extension func (f *File) BaseFileName() string { return helpers.Filename(f.LogicalName()) } +// Section The first directory func (f *File) Section() string { return f.section } +// LogicalName The filename and extension of the file func (f *File) LogicalName() string { return f.logicalName } @@ -71,6 +76,7 @@ func (f *File) Ext() string { return f.Extension() } +// Path the relative path including file name and extension from the base of the source directory func (f *File) Path() string { return f.relpath } diff --git a/source/filesystem.go b/source/filesystem.go index 75cf09d56..09118c27a 100644 --- a/source/filesystem.go +++ b/source/filesystem.go @@ -14,13 +14,14 @@ package source import ( - "github.com/spf13/viper" "io" "os" "path/filepath" "regexp" "strings" + "github.com/spf13/viper" + "github.com/spf13/hugo/helpers" jww "github.com/spf13/jwalterweatherman" ) @@ -59,14 +60,11 @@ func (f *Filesystem) Files() []*File { return f.files } +// add populates a file in the Filesystem.files func (f *Filesystem) add(name string, reader io.Reader) (err error) { var file *File - //if f.Base == "" { - //file = NewFileWithContents(name, reader) - //} else { file, err = NewFileFromAbs(f.Base, name, reader) - //} if err == nil { f.files = append(f.files, file) @@ -79,48 +77,57 @@ func (f *Filesystem) getRelativePath(name string) (final string, err error) { } func (f *Filesystem) captureFiles() { - walker := func(filePath string, fi os.FileInfo, err error) error { if err != nil { return nil } - if fi.Mode()&os.ModeSymlink == os.ModeSymlink { - link, err := filepath.EvalSymlinks(filePath) - if err != nil { - jww.ERROR.Printf("Cannot read symbolic link '%s', error was: %s", filePath, err) - return nil - } - linkfi, err := os.Stat(link) + b, err := f.shouldRead(filePath, fi) + if err != nil { + return err + } + if b { + rd, err := NewLazyFileReader(filePath) if err != nil { - jww.ERROR.Printf("Cannot stat '%s', error was: %s", link, err) - return nil - } - if !linkfi.Mode().IsRegular() { - jww.ERROR.Printf("Symbolic links for directories not supported, skipping '%s'", filePath) + return err } - return nil + f.add(filePath, rd) } + return err + } - if fi.IsDir() { - if f.avoid(filePath) || isNonProcessablePath(filePath) { - return filepath.SkipDir - } - return nil - } + filepath.Walk(f.Base, walker) +} - if isNonProcessablePath(filePath) { - return nil +func (f *Filesystem) shouldRead(filePath string, fi os.FileInfo) (bool, error) { + if fi.Mode()&os.ModeSymlink == os.ModeSymlink { + link, err := filepath.EvalSymlinks(filePath) + if err != nil { + jww.ERROR.Printf("Cannot read symbolic link '%s', error was: %s", filePath, err) + return false, nil } - rd, err := NewLazyFileReader(filePath) + linkfi, err := os.Stat(link) if err != nil { - return err + jww.ERROR.Printf("Cannot stat '%s', error was: %s", link, err) + return false, nil } - f.add(filePath, rd) - return nil + if !linkfi.Mode().IsRegular() { + jww.ERROR.Printf("Symbolic links for directories not supported, skipping '%s'", filePath) + } + return false, nil } - filepath.Walk(f.Base, walker) + if fi.IsDir() { + if f.avoid(filePath) || isNonProcessablePath(filePath) { + return false, filepath.SkipDir + } + return false, nil + } + + if isNonProcessablePath(filePath) { + return false, nil + } + return true, nil } func (f *Filesystem) avoid(filePath string) bool { |