diff options
author | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2017-01-03 19:28:51 +0300 |
---|---|---|
committer | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2017-01-07 19:06:35 +0300 |
commit | 45e3ed517a17648d54e8ce33618a8f251cfec603 (patch) | |
tree | 160a1bbd03a107be08ae76018fb382268365e2a1 /tpl/template.go | |
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/template.go')
-rw-r--r-- | tpl/template.go | 45 |
1 files changed, 21 insertions, 24 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) } } |