summaryrefslogtreecommitdiffhomepage
path: root/source/filesystem.go
diff options
context:
space:
mode:
authorspf13 <[email protected]>2014-10-16 20:20:09 -0400
committerspf13 <[email protected]>2014-10-16 20:20:09 -0400
commit5dfc1dedb8ac53b7a2d3823d06808ae86f90b3d9 (patch)
treee90f9cfbcc920685dd9bceecd1925abec81ae0c0 /source/filesystem.go
parent24bbfe7d32fe151487ff87394433622e3f69eee4 (diff)
downloadhugo-5dfc1dedb8ac53b7a2d3823d06808ae86f90b3d9.tar.gz
hugo-5dfc1dedb8ac53b7a2d3823d06808ae86f90b3d9.zip
Big refactor of how source files are used. Also added default destination extension option.
Diffstat (limited to 'source/filesystem.go')
-rw-r--r--source/filesystem.go86
1 files changed, 42 insertions, 44 deletions
diff --git a/source/filesystem.go b/source/filesystem.go
index 3a176d7ba..d89149dc6 100644
--- a/source/filesystem.go
+++ b/source/filesystem.go
@@ -1,34 +1,56 @@
+// Copyright © 2014 Steve Francia <[email protected]>.
+//
+// Licensed under the Simple Public License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+// http://opensource.org/licenses/Simple-2.0
+//
+// 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.
+
package source
import (
"bytes"
- "errors"
"io"
"io/ioutil"
"os"
- "path"
"path/filepath"
"strings"
+
+ "github.com/spf13/hugo/helpers"
)
type Input interface {
Files() []*File
}
-type File struct {
- name string
- LogicalName string
- Contents io.Reader
- Section string
- Dir string
-}
-
type Filesystem struct {
files []*File
Base string
AvoidPaths []string
}
+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
+}
+
func (f *Filesystem) Files() []*File {
if len(f.files) < 1 {
f.captureFiles()
@@ -36,47 +58,23 @@ func (f *Filesystem) Files() []*File {
return f.files
}
-var errMissingBaseDir = errors.New("source: missing base directory")
-
func (f *Filesystem) add(name string, reader io.Reader) (err error) {
+ var file *File
- if name, err = f.getRelativePath(name); err != nil {
- return err
- }
-
- // section should be the first part of the path
- dir, logical := path.Split(name)
- parts := strings.Split(dir, "/")
- section := parts[0]
+ //if f.Base == "" {
+ //file = NewFileWithContents(name, reader)
+ //} else {
+ file, err = NewFileFromAbs(f.Base, name, reader)
+ //}
- if section == "." {
- section = ""
+ if err == nil {
+ f.files = append(f.files, file)
}
-
- f.files = append(f.files, &File{
- name: name,
- LogicalName: logical,
- Contents: reader,
- Section: section,
- Dir: dir,
- })
-
- return
+ return err
}
func (f *Filesystem) getRelativePath(name string) (final string, err error) {
- if filepath.IsAbs(name) && f.Base == "" {
- return "", errMissingBaseDir
- }
- name = filepath.Clean(name)
- base := filepath.Clean(f.Base)
-
- name, err = filepath.Rel(base, name)
- if err != nil {
- return "", err
- }
- name = filepath.ToSlash(name)
- return name, nil
+ return helpers.GetRelativePath(name, f.Base)
}
func (f *Filesystem) captureFiles() {