2016-03-21 19:28:42 -04:00
|
|
|
// Copyright 2016 The Hugo Authors. All rights reserved.
|
2014-10-16 20:20:09 -04:00
|
|
|
//
|
2015-11-23 22:16:36 -05:00
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
2014-10-16 20:20:09 -04:00
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
2015-11-23 22:16:36 -05:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2014-10-16 20:20:09 -04:00
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2013-09-05 01:28:59 -04:00
|
|
|
package source
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2015-06-03 07:37:57 -04:00
|
|
|
"regexp"
|
2013-11-28 09:27:09 -05:00
|
|
|
"strings"
|
2015-02-17 16:21:37 -05:00
|
|
|
|
2016-07-30 09:14:41 -04:00
|
|
|
"github.com/spf13/hugo/hugofs"
|
|
|
|
|
2016-01-07 21:48:13 -05:00
|
|
|
"github.com/spf13/viper"
|
|
|
|
|
2015-02-17 16:21:37 -05:00
|
|
|
"github.com/spf13/hugo/helpers"
|
|
|
|
jww "github.com/spf13/jwalterweatherman"
|
2013-09-05 01:28:59 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
type Input interface {
|
|
|
|
Files() []*File
|
|
|
|
}
|
|
|
|
|
|
|
|
type Filesystem struct {
|
|
|
|
files []*File
|
|
|
|
Base string
|
|
|
|
AvoidPaths []string
|
|
|
|
}
|
|
|
|
|
2014-10-16 20:20:09 -04:00
|
|
|
func (f *Filesystem) FilesByExts(exts ...string) []*File {
|
|
|
|
var newFiles []*File
|
|
|
|
|
|
|
|
if len(exts) == 0 {
|
|
|
|
return f.Files()
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, x := range f.Files() {
|
|
|
|
for _, e := range exts {
|
|
|
|
if x.Ext() == strings.TrimPrefix(e, ".") {
|
|
|
|
newFiles = append(newFiles, x)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return newFiles
|
|
|
|
}
|
|
|
|
|
2013-09-05 01:28:59 -04:00
|
|
|
func (f *Filesystem) Files() []*File {
|
2013-10-01 17:27:09 -04:00
|
|
|
if len(f.files) < 1 {
|
|
|
|
f.captureFiles()
|
|
|
|
}
|
2013-09-05 01:28:59 -04:00
|
|
|
return f.files
|
|
|
|
}
|
|
|
|
|
2016-01-07 21:48:13 -05:00
|
|
|
// add populates a file in the Filesystem.files
|
2013-09-20 20:03:43 -04:00
|
|
|
func (f *Filesystem) add(name string, reader io.Reader) (err error) {
|
2014-10-16 20:20:09 -04:00
|
|
|
var file *File
|
2013-09-20 20:03:43 -04:00
|
|
|
|
2014-10-16 20:20:09 -04:00
|
|
|
file, err = NewFileFromAbs(f.Base, name, reader)
|
2013-11-28 09:27:09 -05:00
|
|
|
|
2014-10-16 20:20:09 -04:00
|
|
|
if err == nil {
|
|
|
|
f.files = append(f.files, file)
|
2013-09-20 20:03:43 -04:00
|
|
|
}
|
2014-10-16 20:20:09 -04:00
|
|
|
return err
|
2013-09-20 20:03:43 -04:00
|
|
|
}
|
|
|
|
|
2013-09-05 01:28:59 -04:00
|
|
|
func (f *Filesystem) captureFiles() {
|
2013-09-20 20:03:43 -04:00
|
|
|
walker := func(filePath string, fi os.FileInfo, err error) error {
|
2013-09-05 01:28:59 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-01-07 21:48:13 -05:00
|
|
|
b, err := f.shouldRead(filePath, fi)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if b {
|
2016-07-30 09:14:41 -04:00
|
|
|
rd, err := NewLazyFileReader(hugofs.Source(), filePath)
|
2015-02-17 16:21:37 -05:00
|
|
|
if err != nil {
|
2016-01-07 21:48:13 -05:00
|
|
|
return err
|
2015-02-17 16:21:37 -05:00
|
|
|
}
|
2016-01-07 21:48:13 -05:00
|
|
|
f.add(filePath, rd)
|
2014-12-10 10:48:51 -05:00
|
|
|
}
|
2016-01-07 21:48:13 -05:00
|
|
|
return err
|
|
|
|
}
|
2014-12-10 10:48:51 -05:00
|
|
|
|
2016-03-21 19:28:42 -04:00
|
|
|
err := helpers.SymbolicWalk(hugofs.Source(), f.Base, walker)
|
2016-02-14 06:16:03 -05:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
jww.ERROR.Println(err)
|
|
|
|
}
|
|
|
|
|
2016-01-07 21:48:13 -05:00
|
|
|
}
|
2015-03-07 06:58:39 -05:00
|
|
|
|
2016-01-07 21:48:13 -05:00
|
|
|
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
|
2013-09-05 01:28:59 -04:00
|
|
|
}
|
2016-01-07 21:48:13 -05:00
|
|
|
linkfi, err := os.Stat(link)
|
2015-03-07 06:58:39 -05:00
|
|
|
if err != nil {
|
2016-01-07 21:48:13 -05:00
|
|
|
jww.ERROR.Printf("Cannot stat '%s', error was: %s", link, err)
|
|
|
|
return false, nil
|
2015-03-07 06:58:39 -05:00
|
|
|
}
|
2016-01-07 21:48:13 -05:00
|
|
|
if !linkfi.Mode().IsRegular() {
|
|
|
|
jww.ERROR.Printf("Symbolic links for directories not supported, skipping '%s'", filePath)
|
|
|
|
}
|
|
|
|
return false, nil
|
2013-09-05 01:28:59 -04:00
|
|
|
}
|
|
|
|
|
2016-01-07 21:48:13 -05:00
|
|
|
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
|
2013-09-05 01:28:59 -04:00
|
|
|
}
|
|
|
|
|
2013-09-20 20:03:43 -04:00
|
|
|
func (f *Filesystem) avoid(filePath string) bool {
|
2013-09-05 01:28:59 -04:00
|
|
|
for _, avoid := range f.AvoidPaths {
|
2013-09-20 20:03:43 -04:00
|
|
|
if avoid == filePath {
|
2013-09-05 01:28:59 -04:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2014-04-14 17:25:54 -04:00
|
|
|
func isNonProcessablePath(filePath string) bool {
|
2014-02-28 00:26:17 -05:00
|
|
|
base := filepath.Base(filePath)
|
2016-03-21 12:22:27 -04:00
|
|
|
if strings.HasPrefix(base, ".") ||
|
|
|
|
strings.HasPrefix(base, "#") ||
|
|
|
|
strings.HasSuffix(base, "~") {
|
2014-02-28 00:26:17 -05:00
|
|
|
return true
|
|
|
|
}
|
2015-06-03 07:45:52 -04:00
|
|
|
ignoreFiles := viper.GetStringSlice("IgnoreFiles")
|
2015-06-03 07:37:57 -04:00
|
|
|
if len(ignoreFiles) > 0 {
|
|
|
|
for _, ignorePattern := range ignoreFiles {
|
2015-06-03 12:54:30 -04:00
|
|
|
match, err := regexp.MatchString(ignorePattern, filePath)
|
|
|
|
if err != nil {
|
|
|
|
helpers.DistinctErrorLog.Printf("Invalid regexp '%s' in ignoreFiles: %s", ignorePattern, err)
|
|
|
|
return false
|
|
|
|
} else if match {
|
2015-06-03 07:37:57 -04:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-02-28 00:26:17 -05:00
|
|
|
return false
|
2013-09-05 01:28:59 -04:00
|
|
|
}
|