diff options
-rw-r--r-- | commands/hugo.go | 4 | ||||
-rw-r--r-- | deps/deps.go | 6 | ||||
-rw-r--r-- | helpers/general.go | 10 | ||||
-rw-r--r-- | hugolib/embedded_templates_test.go | 54 | ||||
-rw-r--r-- | hugolib/hugo_sites.go | 8 | ||||
-rw-r--r-- | hugolib/page_output.go | 5 | ||||
-rw-r--r-- | hugolib/site.go | 12 | ||||
-rw-r--r-- | hugolib/testhelpers_test.go | 21 | ||||
-rw-r--r-- | tpl/tplimpl/embedded/templates.autogen.go | 6 | ||||
-rwxr-xr-x | tpl/tplimpl/embedded/templates/google_analytics.html | 4 | ||||
-rwxr-xr-x | tpl/tplimpl/embedded/templates/google_analytics_async.html | 2 |
11 files changed, 111 insertions, 21 deletions
diff --git a/commands/hugo.go b/commands/hugo.go index e2d447768..b898a9b7b 100644 --- a/commands/hugo.go +++ b/commands/hugo.go @@ -84,11 +84,11 @@ func Execute(args []string) Response { } if err == nil { - errCount := jww.LogCountForLevelsGreaterThanorEqualTo(jww.LevelError) + errCount := int(jww.LogCountForLevelsGreaterThanorEqualTo(jww.LevelError)) if errCount > 0 { err = fmt.Errorf("logged %d errors", errCount) } else if resp.Result != nil { - errCount = resp.Result.Log.LogCountForLevelsGreaterThanorEqualTo(jww.LevelError) + errCount = resp.Result.NumLogErrors() if errCount > 0 { err = fmt.Errorf("logged %d errors", errCount) } diff --git a/deps/deps.go b/deps/deps.go index 475d678a9..9a502b589 100644 --- a/deps/deps.go +++ b/deps/deps.go @@ -23,6 +23,9 @@ type Deps struct { // The logger to use. Log *jww.Notepad `json:"-"` + // Used to log errors that may repeat itself many times. + DistinctErrorLog *helpers.DistinctLogger + // The templates to use. This will usually implement the full tpl.TemplateHandler. Tmpl tpl.TemplateFinder `json:"-"` @@ -137,9 +140,12 @@ func New(cfg DepsCfg) (*Deps, error) { timeoutms = 3000 } + distinctErrorLogger := helpers.NewDistinctLogger(logger.ERROR) + d := &Deps{ Fs: fs, Log: logger, + DistinctErrorLog: distinctErrorLogger, templateProvider: cfg.TemplateProvider, translationProvider: cfg.TranslationProvider, WithTemplate: cfg.WithTemplate, diff --git a/helpers/general.go b/helpers/general.go index e6c0ec8e5..5b46520e5 100644 --- a/helpers/general.go +++ b/helpers/general.go @@ -260,7 +260,8 @@ func (p *PathSpec) ThemeSet() bool { return p.theme != "" } -type logPrinter interface { +// LogPrinter is the common interface of the JWWs loggers. +type LogPrinter interface { // Println is the only common method that works in all of JWWs loggers. Println(a ...interface{}) } @@ -268,7 +269,7 @@ type logPrinter interface { // DistinctLogger ignores duplicate log statements. type DistinctLogger struct { sync.RWMutex - logger logPrinter + logger LogPrinter m map[string]bool } @@ -309,6 +310,11 @@ func NewDistinctErrorLogger() *DistinctLogger { return &DistinctLogger{m: make(map[string]bool), logger: jww.ERROR} } +// NewDistinctLogger creates a new DistinctLogger that logs to the provided logger. +func NewDistinctLogger(logger LogPrinter) *DistinctLogger { + return &DistinctLogger{m: make(map[string]bool), logger: logger} +} + // NewDistinctWarnLogger creates a new DistinctLogger that logs WARNs func NewDistinctWarnLogger() *DistinctLogger { return &DistinctLogger{m: make(map[string]bool), logger: jww.WARN} diff --git a/hugolib/embedded_templates_test.go b/hugolib/embedded_templates_test.go new file mode 100644 index 000000000..fd620bd15 --- /dev/null +++ b/hugolib/embedded_templates_test.go @@ -0,0 +1,54 @@ +// Copyright 2018 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 hugolib + +import ( + "testing" + + "github.com/stretchr/testify/require" +) + +// Just some simple test of the embedded templates to avoid +// https://github.com/gohugoio/hugo/issues/4757 and similar. +func TestEmbeddedTemplates(t *testing.T) { + t.Parallel() + + assert := require.New(t) + assert.True(true) + + home := []string{"index.html", ` +GA: +{{ template "_internal/google_analytics.html" . }} + +GA async: + +{{ template "_internal/google_analytics_async.html" . }} + +Disqus: + +{{ template "_internal/disqus.html" . }} + +`} + + b := newTestSitesBuilder(t) + b.WithSimpleConfigFile().WithTemplatesAdded(home...) + + b.Build(BuildCfg{}) + + // Gheck GA regular and async + b.AssertFileContent("public/index.html", "'script','https://www.google-analytics.com/analytics.js','ga');\n\tga('create', 'ga_id', 'auto')", "<script async src='//www.google-analytics.com/analytics.js'>") + + // Disqus + b.AssertFileContent("public/index.html", "\"disqus_shortname\" + '.disqus.com/embed.js';") +} diff --git a/hugolib/hugo_sites.go b/hugolib/hugo_sites.go index e3defe974..c5c08b191 100644 --- a/hugolib/hugo_sites.go +++ b/hugolib/hugo_sites.go @@ -29,6 +29,7 @@ import ( "github.com/gohugoio/hugo/i18n" "github.com/gohugoio/hugo/tpl" "github.com/gohugoio/hugo/tpl/tplimpl" + jww "github.com/spf13/jwalterweatherman" ) // HugoSites represents the sites to build. Each site represents a language. @@ -56,6 +57,13 @@ func (h *HugoSites) IsMultihost() bool { return h != nil && h.multihost } +func (h *HugoSites) NumLogErrors() int { + if h == nil { + return 0 + } + return int(h.Log.LogCountForLevelsGreaterThanorEqualTo(jww.LevelError)) +} + func (h *HugoSites) PrintProcessingStats(w io.Writer) { stats := make([]*helpers.ProcessingStats, len(h.Sites)) for i := 0; i < len(h.Sites); i++ { diff --git a/hugolib/page_output.go b/hugolib/page_output.go index 5893bc172..132d755f2 100644 --- a/hugolib/page_output.go +++ b/hugolib/page_output.go @@ -24,7 +24,6 @@ import ( "github.com/gohugoio/hugo/media" - "github.com/gohugoio/hugo/helpers" "github.com/gohugoio/hugo/output" ) @@ -115,7 +114,7 @@ func (p *PageOutput) layouts(layouts ...string) ([]string, error) { func (p *PageOutput) Render(layout ...string) template.HTML { l, err := p.layouts(layout...) if err != nil { - helpers.DistinctErrorLog.Printf("in .Render: Failed to resolve layout %q for page %q", layout, p.pathOrTitle()) + p.s.DistinctErrorLog.Printf("in .Render: Failed to resolve layout %q for page %q", layout, p.pathOrTitle()) return "" } @@ -130,7 +129,7 @@ func (p *PageOutput) Render(layout ...string) template.HTML { if templ != nil { res, err := templ.ExecuteToString(p) if err != nil { - helpers.DistinctErrorLog.Printf("in .Render: Failed to execute template %q: %s", layout, err) + p.s.DistinctErrorLog.Printf("in .Render: Failed to execute template %q: %s", layout, err) return template.HTML("") } return template.HTML(res) diff --git a/hugolib/site.go b/hugolib/site.go index 378c005d6..2f47ac236 100644 --- a/hugolib/site.go +++ b/hugolib/site.go @@ -1746,7 +1746,7 @@ func (s *Site) renderAndWriteXML(statCounter *uint64, name string, dest string, } transformer := transform.NewChain(transform.AbsURLInXML) if err := transformer.Apply(outBuffer, renderBuffer, path); err != nil { - helpers.DistinctErrorLog.Println(err) + s.DistinctErrorLog.Println(err) return nil } @@ -1805,7 +1805,7 @@ func (s *Site) renderAndWritePage(statCounter *uint64, name string, dest string, transformer := transform.NewChain(transformLinks...) if err := transformer.Apply(outBuffer, renderBuffer, path); err != nil { - helpers.DistinctErrorLog.Println(err) + s.DistinctErrorLog.Println(err) return nil } @@ -1821,7 +1821,7 @@ func (s *Site) renderForLayouts(name string, d interface{}, w io.Writer, layouts if templ != nil { templName = templ.Name() } - helpers.DistinctErrorLog.Printf("Failed to render %q: %s", templName, r) + s.DistinctErrorLog.Printf("Failed to render %q: %s", templName, r) // TOD(bep) we really need to fix this. Also see below. if !s.running() && !testMode { os.Exit(-1) @@ -1838,12 +1838,12 @@ func (s *Site) renderForLayouts(name string, d interface{}, w io.Writer, layouts // Behavior here should be dependent on if running in server or watch mode. if p, ok := d.(*PageOutput); ok { if p.File != nil { - helpers.DistinctErrorLog.Printf("Error while rendering %q in %q: %s", name, p.File.Dir(), err) + s.DistinctErrorLog.Printf("Error while rendering %q in %q: %s", name, p.File.Dir(), err) } else { - helpers.DistinctErrorLog.Printf("Error while rendering %q: %s", name, err) + s.DistinctErrorLog.Printf("Error while rendering %q: %s", name, err) } } else { - helpers.DistinctErrorLog.Printf("Error while rendering %q: %s", name, err) + s.DistinctErrorLog.Printf("Error while rendering %q: %s", name, err) } if !s.running() && !testMode { // TODO(bep) check if this can be propagated diff --git a/hugolib/testhelpers_test.go b/hugolib/testhelpers_test.go index 62fe3860a..7a93fcd9a 100644 --- a/hugolib/testhelpers_test.go +++ b/hugolib/testhelpers_test.go @@ -135,10 +135,21 @@ func (s *sitesBuilder) WithThemeConfigFile(format, conf string) *sitesBuilder { return s } +const commonConfigSections = ` + +[services] +[services.disqus] +shortname = "disqus_shortname" +[services.googleAnalytics] +id = "ga_id" + +` + func (s *sitesBuilder) WithSimpleConfigFile() *sitesBuilder { var config = ` baseURL = "http://example.com/" -` + +` + commonConfigSections return s.WithConfigFile("toml", config) } @@ -198,7 +209,7 @@ languageName = "Bokmål" paginatePath = "side" [Languages.nb.Taxonomies] lag = "lag" -` +` + commonConfigSections return s.WithConfigFile("toml", defaultMultiSiteConfig) @@ -311,6 +322,12 @@ func (s *sitesBuilder) build(cfg BuildCfg, shouldFail bool) *sitesBuilder { s.CreateSites() } err := s.H.Build(cfg) + if err == nil { + logErrorCount := s.H.NumLogErrors() + if logErrorCount > 0 { + err = fmt.Errorf("logged %d errors", logErrorCount) + } + } if err != nil && !shouldFail { s.Fatalf("Build failed: %s", err) } else if err == nil && shouldFail { diff --git a/tpl/tplimpl/embedded/templates.autogen.go b/tpl/tplimpl/embedded/templates.autogen.go index bd33e77b5..3f55d2086 100644 --- a/tpl/tplimpl/embedded/templates.autogen.go +++ b/tpl/tplimpl/embedded/templates.autogen.go @@ -99,7 +99,7 @@ var EmbeddedTemplates = [][2]string{ <noscript>Please enable JavaScript to view the <a href="https://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript> <a href="https://disqus.com" class="dsq-brlink">comments powered by <span class="logo-disqus">Disqus</span></a>{{end}} {{- end -}}`}, - {`google_analytics.html`, `{{- $pc := .Site.PrivacyConfig.GoogleAnalytics -}} + {`google_analytics.html`, `{{- $pc := .Site.Config.Privacy.GoogleAnalytics -}} {{- if not $pc.Disable -}} {{ with .Site.GoogleAnalytics }} <script> @@ -116,7 +116,7 @@ if (!doNotTrack) { {{ end }} {{- end -}} {{- define "__ga_js_set_doNotTrack" -}}{{/* This is also used in the async version. */}} -{{- $pc := .Site.PrivacyConfig.GoogleAnalytics -}} +{{- $pc := .Site.Config.Privacy.GoogleAnalytics -}} {{- if not $pc.RespectDoNotTrack -}} var doNotTrack = false; {{- else -}} @@ -124,7 +124,7 @@ var dnt = (navigator.doNotTrack || window.doNotTrack || navigator.msDoNotTrack); var doNotTrack = (dnt == "1" || dnt == "yes"); {{- end -}} {{- end -}}`}, - {`google_analytics_async.html`, `{{- $pc := .Site.PrivacyConfig.GoogleAnalytics -}} + {`google_analytics_async.html`, `{{- $pc := .Site.Config.Privacy.GoogleAnalytics -}} {{- if not $pc.Disable -}} {{ with .Site.GoogleAnalytics }} <script> diff --git a/tpl/tplimpl/embedded/templates/google_analytics.html b/tpl/tplimpl/embedded/templates/google_analytics.html index 8c7951b58..ae4898c7a 100755 --- a/tpl/tplimpl/embedded/templates/google_analytics.html +++ b/tpl/tplimpl/embedded/templates/google_analytics.html @@ -1,4 +1,4 @@ -{{- $pc := .Site.PrivacyConfig.GoogleAnalytics -}} +{{- $pc := .Site.Config.Privacy.GoogleAnalytics -}} {{- if not $pc.Disable -}} {{ with .Site.GoogleAnalytics }} <script> @@ -15,7 +15,7 @@ if (!doNotTrack) { {{ end }} {{- end -}} {{- define "__ga_js_set_doNotTrack" -}}{{/* This is also used in the async version. */}} -{{- $pc := .Site.PrivacyConfig.GoogleAnalytics -}} +{{- $pc := .Site.Config.Privacy.GoogleAnalytics -}} {{- if not $pc.RespectDoNotTrack -}} var doNotTrack = false; {{- else -}} diff --git a/tpl/tplimpl/embedded/templates/google_analytics_async.html b/tpl/tplimpl/embedded/templates/google_analytics_async.html index 48a24c1c0..b81ade025 100755 --- a/tpl/tplimpl/embedded/templates/google_analytics_async.html +++ b/tpl/tplimpl/embedded/templates/google_analytics_async.html @@ -1,4 +1,4 @@ -{{- $pc := .Site.PrivacyConfig.GoogleAnalytics -}} +{{- $pc := .Site.Config.Privacy.GoogleAnalytics -}} {{- if not $pc.Disable -}} {{ with .Site.GoogleAnalytics }} <script> |