diff options
author | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2017-01-03 17:28:51 +0100 |
---|---|---|
committer | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2017-01-07 17:06:35 +0100 |
commit | 45e3ed517a17648d54e8ce33618a8f251cfec603 (patch) | |
tree | 160a1bbd03a107be08ae76018fb382268365e2a1 /tpl | |
parent | 24a286791f37cbf6638b37f29386949045d0bba2 (diff) |
all: Refactor to non-global logger
Note that this looks like overkill for just the logger, and that is correct,
but this will make sense once we start with the template handling etc.
Updates #2701
Diffstat (limited to 'tpl')
-rw-r--r-- | tpl/template.go | 45 | ||||
-rw-r--r-- | tpl/template_funcs_test.go | 27 | ||||
-rw-r--r-- | tpl/template_test.go | 6 |
3 files changed, 41 insertions, 37 deletions
diff --git a/tpl/template.go b/tpl/template.go index 0872372b5..db6a912ff 100644 --- a/tpl/template.go +++ b/tpl/template.go @@ -31,7 +31,9 @@ import ( ) var localTemplates *template.Template -var tmpl Template + +// TODO(bep) globals get rid of the reset of the jww.ERR etc. +var tmpl *GoHTMLTemplate // TODO(bep) an interface with hundreds of methods ... remove it. // And unexport most of these methods. @@ -66,30 +68,25 @@ type GoHTMLTemplate struct { overlays map[string]*template.Template errors []*templateErr -} -// T is the "global" template system -func T() Template { - if tmpl == nil { - tmpl = New() - } - - return tmpl + // TODO(bep) globals template + log *jww.Notepad } // InitializeT resets the internal template state to its initial state -func InitializeT() Template { - tmpl = New() +func InitializeT(logger *jww.Notepad) *GoHTMLTemplate { + tmpl = New(logger) return tmpl } // New returns a new Hugo Template System // with all the additional features, templates & functions -func New() Template { +func New(logger *jww.Notepad) *GoHTMLTemplate { var templates = &GoHTMLTemplate{ Template: *template.New(""), overlays: make(map[string]*template.Template), errors: make([]*templateErr, 0), + log: logger, } localTemplates = &templates.Template @@ -139,8 +136,8 @@ func executeTemplate(context interface{}, w io.Writer, layouts ...string) { } } if !worked { - jww.ERROR.Println("Unable to render", layouts) - jww.ERROR.Println("Expecting to find a template in either the theme/layouts or /layouts in one of the following relative locations", layouts) + tmpl.log.ERROR.Println("Unable to render", layouts) + tmpl.log.ERROR.Println("Expecting to find a template in either the theme/layouts or /layouts in one of the following relative locations", layouts) } } @@ -152,7 +149,7 @@ func ExecuteTemplateToHTML(context interface{}, layouts ...string) template.HTML } func Lookup(name string) *template.Template { - return (tmpl.(*GoHTMLTemplate)).Lookup(name) + return tmpl.Lookup(name) } func (t *GoHTMLTemplate) Lookup(name string) *template.Template { @@ -361,7 +358,7 @@ func (t *GoHTMLTemplate) AddTemplateFile(name, baseTemplatePath, path string) er return err } - jww.DEBUG.Printf("Add template file from path %s", path) + t.log.DEBUG.Printf("Add template file from path %s", path) return t.AddTemplate(name, string(b)) } @@ -391,25 +388,25 @@ func isBaseTemplate(path string) bool { } func (t *GoHTMLTemplate) loadTemplates(absPath string, prefix string) { - jww.DEBUG.Printf("Load templates from path %q prefix %q", absPath, prefix) + t.log.DEBUG.Printf("Load templates from path %q prefix %q", absPath, prefix) walker := func(path string, fi os.FileInfo, err error) error { if err != nil { return nil } - jww.DEBUG.Println("Template path", path) + t.log.DEBUG.Println("Template path", path) if fi.Mode()&os.ModeSymlink == os.ModeSymlink { link, err := filepath.EvalSymlinks(absPath) if err != nil { - jww.ERROR.Printf("Cannot read symbolic link '%s', error was: %s", absPath, err) + t.log.ERROR.Printf("Cannot read symbolic link '%s', error was: %s", absPath, err) return nil } linkfi, err := hugofs.Source().Stat(link) if err != nil { - jww.ERROR.Printf("Cannot stat '%s', error was: %s", link, err) + t.log.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'", absPath) + t.log.ERROR.Printf("Symbolic links for directories not supported, skipping '%s'", absPath) } return nil } @@ -492,14 +489,14 @@ func (t *GoHTMLTemplate) loadTemplates(absPath string, prefix string) { } if err := t.AddTemplateFile(tplName, baseTemplatePath, path); err != nil { - jww.ERROR.Printf("Failed to add template %s in path %s: %s", tplName, path, err) + t.log.ERROR.Printf("Failed to add template %s in path %s: %s", tplName, path, err) } } return nil } if err := helpers.SymbolicWalk(hugofs.Source(), absPath, walker); err != nil { - jww.ERROR.Printf("Failed to load templates: %s", err) + t.log.ERROR.Printf("Failed to load templates: %s", err) } } @@ -526,6 +523,6 @@ func (t *GoHTMLTemplate) LoadTemplates(absPath string) { func (t *GoHTMLTemplate) PrintErrors() { for _, e := range t.errors { - jww.ERROR.Println(e.err) + t.log.ERROR.Println(e.err) } } diff --git a/tpl/template_funcs_test.go b/tpl/template_funcs_test.go index fd51e3a1a..c3fbb6ad8 100644 --- a/tpl/template_funcs_test.go +++ b/tpl/template_funcs_test.go @@ -33,13 +33,20 @@ import ( "github.com/spf13/hugo/helpers" + "io/ioutil" + "log" + "os" + "github.com/spf13/afero" "github.com/spf13/cast" "github.com/spf13/hugo/hugofs" + jww "github.com/spf13/jwalterweatherman" "github.com/spf13/viper" "github.com/stretchr/testify/assert" ) +var logger = jww.NewNotepad(jww.LevelFatal, jww.LevelFatal, os.Stdout, ioutil.Discard, "", log.Ldate|log.Ltime) + type tstNoStringer struct { } @@ -237,7 +244,7 @@ urlize: bat-man ` var b bytes.Buffer - templ, err := New().New("test").Parse(in) + templ, err := New(logger).New("test").Parse(in) var data struct { Title string Section string @@ -2371,7 +2378,7 @@ func TestDefault(t *testing.T) { {map[string]string{"foo": "dog"}, `{{ default "nope" .foo "extra" }}`, ``, false}, {map[string]interface{}{"images": []string{}}, `{{ default "default.jpg" (index .images 0) }}`, `default.jpg`, true}, } { - tmpl, err := New().New("test").Parse(this.tpl) + tmpl, err := New(logger).New("test").Parse(this.tpl) if err != nil { t.Errorf("[%d] unable to create new html template %q: %s", i, this.tpl, err) continue @@ -2773,7 +2780,7 @@ func TestPartialCached(t *testing.T) { data.Params = map[string]interface{}{"langCode": "en"} tstInitTemplates() - InitializeT() + InitializeT(logger) for i, tc := range testCases { var tmp string if tc.variant != "" { @@ -2782,7 +2789,7 @@ func TestPartialCached(t *testing.T) { tmp = tc.tmpl } - tmpl, err := New().New("testroot").Parse(tmp) + tmpl, err := New(logger).New("testroot").Parse(tmp) if err != nil { t.Fatalf("[%d] unable to create new html template: %s", i, err) } @@ -2824,8 +2831,8 @@ func TestPartialCached(t *testing.T) { } func BenchmarkPartial(b *testing.B) { - InitializeT() - tmpl, err := New().New("testroot").Parse(`{{ partial "bench1" . }}`) + InitializeT(logger) + tmpl, err := New(logger).New("testroot").Parse(`{{ partial "bench1" . }}`) if err != nil { b.Fatalf("unable to create new html template: %s", err) } @@ -2844,8 +2851,8 @@ func BenchmarkPartial(b *testing.B) { } func BenchmarkPartialCached(b *testing.B) { - InitializeT() - tmpl, err := New().New("testroot").Parse(`{{ partialCached "bench1" . }}`) + InitializeT(logger) + tmpl, err := New(logger).New("testroot").Parse(`{{ partialCached "bench1" . }}`) if err != nil { b.Fatalf("unable to create new html template: %s", err) } @@ -2864,8 +2871,8 @@ func BenchmarkPartialCached(b *testing.B) { } func BenchmarkPartialCachedVariants(b *testing.B) { - InitializeT() - tmpl, err := New().New("testroot").Parse(`{{ partialCached "bench1" . "header" }}`) + InitializeT(logger) + tmpl, err := New(logger).New("testroot").Parse(`{{ partialCached "bench1" . "header" }}`) if err != nil { b.Fatalf("unable to create new html template: %s", err) } diff --git a/tpl/template_test.go b/tpl/template_test.go index c96b8c87c..2f4946598 100644 --- a/tpl/template_test.go +++ b/tpl/template_test.go @@ -55,7 +55,7 @@ html lang=en for _, root := range []string{"", os.TempDir()} { - templ := New() + templ := New(logger) basePath := this.basePath innerPath := this.innerPath @@ -124,7 +124,7 @@ func TestAddTemplateFileWithMaster(t *testing.T) { } { hugofs.InitMemFs() - templ := New() + templ := New(logger) overlayTplName := "ot" masterTplName := "mt" finalTplName := "tp" @@ -245,7 +245,7 @@ func TestTplGoFuzzReports(t *testing.T) { // Issue #1095 {"{{apply .C \"urlize\" " + "\".\"}}", 2}} { - templ := New() + templ := New(logger) d := &Data{ A: 42, |