2015-12-07 13:57:01 -05:00
|
|
|
// Copyright 2015 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 (
|
2015-06-03 07:37:57 -04:00
|
|
|
"github.com/spf13/viper"
|
2013-09-05 01:28:59 -04:00
|
|
|
"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
|
|
|
|
|
|
|
"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
|
|
|
|
}
|
|
|
|
|
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
|
|
|
//if f.Base == "" {
|
|
|
|
//file = NewFileWithContents(name, reader)
|
|
|
|
//} else {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
func (f *Filesystem) getRelativePath(name string) (final string, err error) {
|
2014-10-16 20:20:09 -04:00
|
|
|
return helpers.GetRelativePath(name, f.Base)
|
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
|
|
|
|
}
|
|
|
|
|
2014-12-10 10:48:51 -05:00
|
|
|
if fi.Mode()&os.ModeSymlink == os.ModeSymlink {
|
2015-02-17 16:21:37 -05:00
|
|
|
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)
|
|
|
|
}
|
2014-12-10 10:48:51 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-09-05 01:28:59 -04:00
|
|
|
if fi.IsDir() {
|
2014-04-14 17:25:54 -04:00
|
|
|
if f.avoid(filePath) || isNonProcessablePath(filePath) {
|
2013-09-05 01:28:59 -04:00
|
|
|
return filepath.SkipDir
|
|
|
|
}
|
|
|
|
return nil
|
2015-03-07 06:58:39 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if isNonProcessablePath(filePath) {
|
2013-09-05 01:28:59 -04:00
|
|
|
return nil
|
|
|
|
}
|
2015-08-15 23:30:22 -04:00
|
|
|
rd, err := NewLazyFileReader(filePath)
|
2015-03-07 06:58:39 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-08-15 23:30:22 -04:00
|
|
|
f.add(filePath, rd)
|
2015-03-07 06:58:39 -05:00
|
|
|
return nil
|
2013-09-05 01:28:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
filepath.Walk(f.Base, walker)
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
if base[0] == '.' {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2014-03-03 16:55:28 -05:00
|
|
|
if base[0] == '#' {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2014-02-28 00:26:17 -05:00
|
|
|
if base[len(base)-1] == '~' {
|
|
|
|
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
|
|
|
}
|