mirror of
https://github.com/gohugoio/hugo.git
synced 2025-08-18 21:11:19 +02:00
Handle views in combo with Ace base templates
As views looks like a regular template, but doesn't need a base template, we have to look inside it. Altough really not needed by this commit, reading the full file content into memory just to do a substring search is a waste. So this commit implements a `ReaderContains` func that in most cases should be much faster than doing an `ioutil.ReadAll` and `bytes.Contains`: ``` benchmark old ns/op new ns/op delta BenchmarkReaderContains 78452 20260 -74.18% benchmark old allocs new allocs delta BenchmarkReaderContains 46 20 -56.52% benchmark old bytes new bytes delta BenchmarkReaderContains 46496 1258 -97.29% ``` Fixes #999
This commit is contained in:
@@ -16,16 +16,15 @@ package helpers
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/spf13/afero"
|
||||
jww "github.com/spf13/jwalterweatherman"
|
||||
"github.com/spf13/viper"
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"strings"
|
||||
"unicode"
|
||||
|
||||
"github.com/spf13/afero"
|
||||
jww "github.com/spf13/jwalterweatherman"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
// FilepathPathBridge is a bridge for common functionality in filepath vs path
|
||||
@@ -153,6 +152,17 @@ func IsEmpty(path string, fs afero.Fs) (bool, error) {
|
||||
return fi.Size() == 0, nil
|
||||
}
|
||||
|
||||
// Check if a file contains a specified string.
|
||||
func FileContains(filename string, subslice []byte, fs afero.Fs) (bool, error) {
|
||||
f, err := os.Open(filename)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
return ReaderContains(f, subslice), nil
|
||||
}
|
||||
|
||||
// Check if a file or directory exists.
|
||||
func Exists(path string, fs afero.Fs) (bool, error) {
|
||||
_, err := fs.Stat(path)
|
||||
|
Reference in New Issue
Block a user