Read/reread individual source content files

next is incremental conversion
This commit is contained in:
Steve Francia
2016-01-07 21:48:13 -05:00
parent ca6ca4f4fc
commit 9f3796a31d
7 changed files with 253 additions and 81 deletions

View File

@@ -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,50 +77,59 @@ 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)
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 nil
}
if fi.IsDir() {
if f.avoid(filePath) || isNonProcessablePath(filePath) {
return filepath.SkipDir
}
return nil
}
if isNonProcessablePath(filePath) {
return nil
}
rd, err := NewLazyFileReader(filePath)
b, err := f.shouldRead(filePath, fi)
if err != nil {
return err
}
f.add(filePath, rd)
return nil
if b {
rd, err := NewLazyFileReader(filePath)
if err != nil {
return err
}
f.add(filePath, rd)
}
return err
}
filepath.Walk(f.Base, walker)
}
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
}
linkfi, err := os.Stat(link)
if err != nil {
jww.ERROR.Printf("Cannot stat '%s', error was: %s", link, err)
return false, nil
}
if !linkfi.Mode().IsRegular() {
jww.ERROR.Printf("Symbolic links for directories not supported, skipping '%s'", filePath)
}
return false, nil
}
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 {
for _, avoid := range f.AvoidPaths {
if avoid == filePath {