mirror of
https://github.com/gohugoio/hugo.git
synced 2025-08-17 21:01:26 +02:00
Read/reread individual source content files
next is incremental conversion
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
@@ -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 {
|
||||
|
Reference in New Issue
Block a user