diff options
71 files changed, 4758 insertions, 2430 deletions
diff --git a/commands/convert.go b/commands/convert.go index b9129e594..fe64405e9 100644 --- a/commands/convert.go +++ b/commands/convert.go @@ -16,10 +16,11 @@ package commands import ( "bytes" "fmt" - "io" "strings" "time" + "github.com/gohugoio/hugo/parser/pageparser" + "github.com/gohugoio/hugo/resources/page" "github.com/gohugoio/hugo/hugofs" @@ -28,7 +29,6 @@ import ( "github.com/gohugoio/hugo/parser" "github.com/gohugoio/hugo/parser/metadecoders" - "github.com/gohugoio/hugo/parser/pageparser" "github.com/pkg/errors" @@ -157,7 +157,7 @@ func (cc *convertCmd) convertAndSavePage(p page.Page, site *hugolib.Site, target return nil } - pf, err := parseContentFile(file) + pf, err := pageparser.ParseFrontMatterAndContent(file) if err != nil { site.Log.ERROR.Println(errMsg) file.Close() @@ -167,23 +167,23 @@ func (cc *convertCmd) convertAndSavePage(p page.Page, site *hugolib.Site, target file.Close() // better handling of dates in formats that don't have support for them - if pf.frontMatterFormat == metadecoders.JSON || pf.frontMatterFormat == metadecoders.YAML || pf.frontMatterFormat == metadecoders.TOML { - for k, v := range pf.frontMatter { + if pf.FrontMatterFormat == metadecoders.JSON || pf.FrontMatterFormat == metadecoders.YAML || pf.FrontMatterFormat == metadecoders.TOML { + for k, v := range pf.FrontMatter { switch vv := v.(type) { case time.Time: - pf.frontMatter[k] = vv.Format(time.RFC3339) + pf.FrontMatter[k] = vv.Format(time.RFC3339) } } } var newContent bytes.Buffer - err = parser.InterfaceToFrontMatter(pf.frontMatter, targetFormat, &newContent) + err = parser.InterfaceToFrontMatter(pf.FrontMatter, targetFormat, &newContent) if err != nil { site.Log.ERROR.Println(errMsg) return err } - newContent.Write(pf.content) + newContent.Write(pf.Content) newFilename := p.File().Filename() @@ -210,39 +210,3 @@ type parsedFile struct { // Everything after Front Matter content []byte } - -func parseContentFile(r io.Reader) (parsedFile, error) { - var pf parsedFile - - psr, err := pageparser.Parse(r, pageparser.Config{}) - if err != nil { - return pf, err - } - - iter := psr.Iterator() - - walkFn := func(item pageparser.Item) bool { - if pf.frontMatterSource != nil { - // The rest is content. - pf.content = psr.Input()[item.Pos:] - // Done - return false - } else if item.IsFrontMatter() { - pf.frontMatterFormat = metadecoders.FormatFromFrontMatterType(item.Type) - pf.frontMatterSource = item.Val - } - return true - - } - - iter.PeekWalk(walkFn) - - metadata, err := metadecoders.Default.UnmarshalToMap(pf.frontMatterSource, pf.frontMatterFormat) - if err != nil { - return pf, err - } - pf.frontMatter = metadata - - return pf, nil - -} diff --git a/commands/import_jekyll.go b/commands/import_jekyll.go index d2e7b27da..2dd0fc051 100644 --- a/commands/import_jekyll.go +++ b/commands/import_jekyll.go @@ -26,6 +26,8 @@ import ( "time" "unicode" + "github.com/gohugoio/hugo/parser/pageparser" + "github.com/gohugoio/hugo/common/hugio" "github.com/gohugoio/hugo/parser/metadecoders" @@ -397,19 +399,19 @@ func convertJekyllPost(path, relPath, targetDir string, draft bool) error { return err } - pf, err := parseContentFile(bytes.NewReader(contentBytes)) + pf, err := pageparser.ParseFrontMatterAndContent(bytes.NewReader(contentBytes)) if err != nil { jww.ERROR.Println("Parse file error:", path) return err } - newmetadata, err := convertJekyllMetaData(pf.frontMatter, postName, postDate, draft) + newmetadata, err := convertJekyllMetaData(pf.FrontMatter, postName, postDate, draft) if err != nil { jww.ERROR.Println("Convert metadata error:", path) return err } - content, err := convertJekyllContent(newmetadata, string(pf.content)) + content, err := convertJekyllContent(newmetadata, string(pf.Content)) if err != nil { jww.ERROR.Println("Converting Jekyll error:", path) return err diff --git a/common/herrors/errors.go b/common/herrors/errors.go index 5fae6fcae..fded30b1a 100644 --- a/common/herrors/errors.go +++ b/common/herrors/errors.go @@ -57,6 +57,11 @@ func PrintStackTrace(w io.Writer) { fmt.Fprintf(w, "%s", buf) } +// ErrorSender is a, typically, non-blocking error handler. +type ErrorSender interface { + SendError(err error) +} + // Recover is a helper function that can be used to capture panics. // Put this at the top of a method/function that crashes in a template: // defer herrors.Recover() diff --git a/common/para/para_test.go b/common/para/para_test.go index bda7f5d27..9b268b0c0 100644 --- a/common/para/para_test.go +++ b/common/para/para_test.go @@ -16,6 +16,7 @@ package para import ( "context" "runtime" + "sort" "sync" "sync/atomic" diff --git a/common/types/convert.go b/common/types/convert.go new file mode 100644 index 000000000..b55330757 --- /dev/null +++ b/common/types/convert.go @@ -0,0 +1,28 @@ +// Copyright 2019 The Hugo Authors. All rights reserved. +// +// Licensed under the Apache 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://www.apache.org/licenses/LICENSE-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 types + +import "github.com/spf13/cast" + +// ToStringSlicePreserveString converts v to a string slice. +// If v is a string, it will be wrapped in a string slice. +func ToStringSlicePreserveString(v interface{}) []string { + if v == nil { + return nil + } + if sds, ok := v.(string); ok { + return []string{sds} + } + return cast.ToStringSlice(v) +} diff --git a/common/types/convert_test.go b/common/types/convert_test.go new file mode 100644 index 000000000..7f86f4c8a --- /dev/null +++ b/common/types/convert_test.go @@ -0,0 +1,29 @@ +// Copyright 2019 The Hugo Authors. All rights reserved. +// +// Licensed under the Apache 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://www.apache.org/licenses/LICENSE-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 types + +import ( + "testing" + + qt "github.com/frankban/quicktest" +) + +func TestToStringSlicePreserveString(t *testing.T) { + c := qt.New(t) + + c.Assert(ToStringSlicePreserveString("Hugo"), qt.DeepEquals, []string{"Hugo"}) + c.Assert(ToStringSlicePreserveString([]interface{}{"A", "B"}), qt.DeepEquals, []string{"A", "B"}) + c.Assert(ToStringSlicePreserveString(nil), qt.IsNil) + +} diff --git a/config/configProvider.go b/config/configProvider.go index 187fb7b10..928bf948a 100644 --- a/config/configProvider.go +++ b/config/configProvider.go @@ -14,7 +14,7 @@ package config import ( - "github.com/spf13/cast" + "github.com/gohugoio/hugo/common/types" ) // Provider provides the configuration settings for Hugo. @@ -35,14 +35,7 @@ type Provider interface { // we do not attempt to split it into fields. func GetStringSlicePreserveString(cfg Provider, key string) []string { sd := cfg.Get(key) - return toStringSlicePreserveString(sd) -} - -func toStringSlicePreserveString(v interface{}) []string { - if sds, ok := v.(string); ok { - return []string{sds} - } - return cast.ToStringSlice(v) + return types.ToStringSlicePreserveString(sd) } // SetBaseTestDefaults provides some common config defaults used in tests. diff --git a/create/content_template_handler.go b/create/content_template_handler.go index e4cddedf5..3a7007f1a 100644 --- a/create/content_template_handler.go +++ b/create/content_template_handler.go @@ -110,7 +110,7 @@ func executeArcheTypeAsTemplate(s *hugolib.Site, name, kind, targetPath, archety Date: time.Now().Format(time.RFC3339), Name: name, File: f, - Site: &s.Info, + Site: s.Info, } if archetypeFilename == "" { @@ -73,6 +73,7 @@ github.com/bep/gitmap v1.1.1 h1:Nf8ySnC3I7/xPjuWeCwzukUFv185iTUQ6nOvLy9gCJA= github.com/bep/gitmap v1.1.1/go.mod h1:g9VRETxFUXNWzMiuxOwcudo6DfZkW9jOsOW0Ft4kYaY= github.com/bep/golibsass v0.4.0 h1:B2jsNZuRgpsyzv0I5iubJYApDhib87RzjTcRhVOjg78= github.com/bep/golibsass v0.4.0/go.mod h1:DL87K8Un/+pWUS75ggYv41bliGiolxzDKWJAq3eJ1MA= +github.com/bep/golibsass v0.5.0 h1:b+Uxsk826Q35OmbenSmU65P+FJJQoVs2gI2mk1ba28s= github.com/bep/golibsass v0.5.0/go.mod h1:DL87K8Un/+pWUS75ggYv41bliGiolxzDKWJAq3eJ1MA= github.com/bep/tmc v0.5.1 h1:CsQnSC6MsomH64gw0cT5f+EwQDcvZz4AazKunFwTpuI= github.com/bep/tmc v0.5.1/go.mod h1:tGYHN8fS85aJPhDLgXETVKp+PR382OvFi2+q2GkGsq0= diff --git a/helpers/general.go b/helpers/general.go index aa1e00d3a..80e303087 100644 --- a/helpers/general.go +++ b/helpers/general.go @@ -437,36 +437,6 @@ func NormalizeHugoFlags(f *pflag.FlagSet, name string) pflag.NormalizedName { return pflag.NormalizedName(name) } -// DiffStringSlices returns the difference between two string slices. -// Useful in tests. -// See: -// http://stackoverflow.com/questions/19374219/how-to-find-the-difference-between-two-slices-of-strings-in-golang -func DiffStringSlices(slice1 []string, slice2 []string) []string { - diffStr := []string{} - m := map[string]int{} - - for _, s1Val := range slice1 { - m[s1Val] = 1 - } - for _, s2Val := range slice2 { - m[s2Val] = m[s2Val] + 1 - } - - for mKey, mVal := range m { - if mVal == 1 { - diffStr = append(diffStr, mKey) - } - } - - return diffStr -} - -// DiffStrings splits the strings into fields and runs it into DiffStringSlices. -// Useful for tests. -func DiffStrings(s1, s2 string) []string { - return DiffStringSlices(strings.Fields(s1), strings.Fields(s2)) -} - // PrintFs prints the given filesystem to the given writer starting from the given path. // This is useful for debugging. func PrintFs(fs afero.Fs, path string, w io.Writer) { diff --git a/helpers/path.go b/helpers/path.go index d97789e15..29e1e6071 100644 --- a/helpers/path.go +++ b/helpers/path.go @@ -18,6 +18,7 @@ import ( "fmt" "io" "os" + "path" "path/filepath" "regexp" "sort" @@ -243,13 +244,19 @@ func FileAndExtNoDelimiter(in string) (string, string) { return file, strings.TrimPrefix(ext, ".") } -// Filename takes a path, strips out the extension, +// Filename takes a file path, strips out the extension, // and returns the name of the file. func Filename(in string) (name string) { name, _ = fileAndExt(in, fpb) return } +// PathNoExt takes a path, strips out the extension, +// and returns the name of the file. +func PathNoExt(in string) string { + return strings.TrimSuffix(in, path.Ext(in)) +} + // FileAndExt returns the filename and any extension of a file path as // two separate strings. // diff --git a/htesting/hqt/checkers.go b/htesting/hqt/checkers.go index eb8b733ac..6fb65ee47 100644 --- a/htesting/hqt/checkers.go +++ b/htesting/hqt/checkers.go @@ -15,12 +15,24 @@ package hqt import ( "errors" + "fmt" "reflect" + "strings" qt "github.com/frankban/quicktest" + "github.com/gohugoio/hugo/htesting" |