2016-03-21 23:28:42 +00:00
|
|
|
// Copyright 2016 The Hugo Authors. All rights reserved.
|
2014-10-17 00:20:09 +00:00
|
|
|
//
|
2015-11-24 03:16:36 +00:00
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
2014-10-17 00:20:09 +00:00
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
2015-11-24 03:16:36 +00:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2014-10-17 00:20:09 +00: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 05:28:59 +00:00
|
|
|
package source
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2015-06-03 11:37:57 +00:00
|
|
|
"regexp"
|
2016-08-08 18:25:00 +00:00
|
|
|
"runtime"
|
2013-11-28 14:27:09 +00:00
|
|
|
"strings"
|
2015-02-17 21:21:37 +00:00
|
|
|
|
2016-07-30 13:14:41 +00:00
|
|
|
"github.com/spf13/hugo/hugofs"
|
2016-08-08 18:25:00 +00:00
|
|
|
"golang.org/x/text/unicode/norm"
|
2016-07-30 13:14:41 +00:00
|
|
|
|
2016-01-08 02:48:13 +00:00
|
|
|
"github.com/spf13/viper"
|
|
|
|
|
2015-02-17 21:21:37 +00:00
|
|
|
"github.com/spf13/hugo/helpers"
|
|
|
|
jww "github.com/spf13/jwalterweatherman"
|
2013-09-05 05:28:59 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Input interface {
|
|
|
|
Files() []*File
|
|
|
|
}
|
|
|
|
|
|
|
|
type Filesystem struct {
|
|
|
|
files []*File
|
|
|
|
Base string
|
|
|
|
AvoidPaths []string
|
|
|
|
}
|
|
|
|
|
2014-10-17 00:20:09 +00: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 05:28:59 +00:00
|
|
|
func (f *Filesystem) Files() []*File {
|
2013-10-01 21:27:09 +00:00
|
|
|
if len(f.files) < 1 {
|
|
|
|
f.captureFiles()
|
|
|
|
}
|
2013-09-05 05:28:59 +00:00
|
|
|
return f.files
|
|
|
|
}
|
|
|
|
|
2016-01-08 02:48:13 +00:00
|
|
|
// add populates a file in the Filesystem.files
|
2013-09-21 00:03:43 +00:00
|
|
|
func (f *Filesystem) add(name string, reader io.Reader) (err error) {
|
2014-10-17 00:20:09 +00:00
|
|
|
var file *File
|
2013-09-21 00:03:43 +00:00
|
|
|
|
2016-08-08 18:25:00 +00:00
|
|
|
if runtime.GOOS == "darwin" {
|
|
|
|
// When a file system is HFS+, its filepath is in NFD form.
|
|
|
|
name = norm.NFC.String(name)
|
|
|
|
}
|
|
|
|
|
2014-10-17 00:20:09 +00:00
|
|
|
file, err = NewFileFromAbs(f.Base, name, reader)
|
2013-11-28 14:27:09 +00:00
|
|
|
|
2014-10-17 00:20:09 +00:00
|
|
|
if err == nil {
|
|
|
|
f.files = append(f.files, file)
|
2013-09-21 00:03:43 +00:00
|
|
|
}
|
2014-10-17 00:20:09 +00:00
|
|
|
return err
|
2013-09-21 00:03:43 +00:00
|
|
|
}
|
|
|
|
|
2013-09-05 05:28:59 +00:00
|
|
|
func (f *Filesystem) captureFiles() {
|
2013-09-21 00:03:43 +00:00
|
|
|
walker := func(filePath string, fi os.FileInfo, err error) error {
|
2013-09-05 05:28:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-01-08 02:48:13 +00:00
|
|
|
b, err := f.shouldRead(filePath, fi)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if b {
|
2016-07-30 13:14:41 +00:00
|
|
|
rd, err := NewLazyFileReader(hugofs.Source(), filePath)
|
2015-02-17 21:21:37 +00:00
|
|
|
if err != nil {
|
2016-01-08 02:48:13 +00:00
|
|
|
return err
|
2015-02-17 21:21:37 +00:00
|
|
|
}
|
2016-01-08 02:48:13 +00:00
|
|
|
f.add(filePath, rd)
|
2014-12-10 15:48:51 +00:00
|
|
|
}
|
2016-01-08 02:48:13 +00:00
|
|
|
return err
|
|
|
|
}
|
2014-12-10 15:48:51 +00:00
|
|
|
|
2016-03-21 23:28:42 +00:00
|
|
|
err := helpers.SymbolicWalk(hugofs.Source(), f.Base, walker)
|
2016-02-14 11:16:03 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
jww.ERROR.Println(err)
|
2016-07-28 07:30:58 +00:00
|
|
|
if err == helpers.WalkRootTooShortError {
|
|
|
|
panic("The root path is too short. If this is a test, make sure to init the content paths.")
|
|
|
|
}
|
2016-02-14 11:16:03 +00:00
|
|
|
}
|
|
|
|
|
2016-01-08 02:48:13 +00:00
|
|
|
}
|
2015-03-07 11:58:39 +00:00
|
|
|
|
2016-01-08 02:48:13 +00: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 05:28:59 +00:00
|
|
|
}
|
2016-09-11 17:59:07 +00:00
|
|
|
linkfi, err := hugofs.Source().Stat(link)
|
2015-03-07 11:58:39 +00:00
|
|
|
if err != nil {
|
2016-01-08 02:48:13 +00:00
|
|
|
jww.ERROR.Printf("Cannot stat '%s', error was: %s", link, err)
|
|
|
|
return false, nil
|
2015-03-07 11:58:39 +00:00
|
|
|
}
|
2016-01-08 02:48:13 +00:00
|
|
|
if !linkfi.Mode().IsRegular() {
|
|
|
|
jww.ERROR.Printf("Symbolic links for directories not supported, skipping '%s'", filePath)
|
|
|
|
}
|
|
|
|
return false, nil
|
2013-09-05 05:28:59 +00:00
|
|
|
}
|
|
|
|
|
2016-01-08 02:48:13 +00: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 05:28:59 +00:00
|
|
|
}
|
|
|
|
|
2013-09-21 00:03:43 +00:00
|
|
|
func (f *Filesystem) avoid(filePath string) bool {
|
2013-09-05 05:28:59 +00:00
|
|
|
for _, avoid := range f.AvoidPaths {
|
2013-09-21 00:03:43 +00:00
|
|
|
if avoid == filePath {
|
2013-09-05 05:28:59 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2014-04-14 21:25:54 +00:00
|
|
|
func isNonProcessablePath(filePath string) bool {
|
2014-02-28 05:26:17 +00:00
|
|
|
base := filepath.Base(filePath)
|
2016-03-21 16:22:27 +00:00
|
|
|
if strings.HasPrefix(base, ".") ||
|
|
|
|
strings.HasPrefix(base, "#") ||
|
|
|
|
strings.HasSuffix(base, "~") {
|
2014-02-28 05:26:17 +00:00
|
|
|
return true
|
|
|
|
}
|
2016-10-24 18:56:00 +00:00
|
|
|
ignoreFiles := viper.GetStringSlice("ignoreFiles")
|
2015-06-03 11:37:57 +00:00
|
|
|
if len(ignoreFiles) > 0 {
|
|
|
|
for _, ignorePattern := range ignoreFiles {
|
2015-06-03 16:54:30 +00: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 11:37:57 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-02-28 05:26:17 +00:00
|
|
|
return false
|
2013-09-05 05:28:59 +00:00
|
|
|
}
|