mirror of
https://github.com/gohugoio/hugo.git
synced 2025-03-19 10:31:20 +00:00
refactor handling of amber to AddTemplateFile as the TODO note stated. Used switch statement to make it easier to add other template support
This commit is contained in:
parent
4e9b04086a
commit
becd4fe337
1 changed files with 24 additions and 18 deletions
|
@ -359,11 +359,30 @@ func (t *GoHtmlTemplate) AddTemplate(name, tpl string) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *GoHtmlTemplate) AddTemplateFile(name, path string) error {
|
func (t *GoHtmlTemplate) AddTemplateFile(name, path string) error {
|
||||||
b, err := ioutil.ReadFile(path)
|
// get the suffix and switch on that
|
||||||
if err != nil {
|
ext := filepath.Ext(path)
|
||||||
return err
|
switch ext {
|
||||||
|
case ".amber":
|
||||||
|
compiler := amber.New()
|
||||||
|
// Parse the input file
|
||||||
|
if err := compiler.ParseFile(path); err != nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, err := compiler.CompileWithTemplate(t.New(name)); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
b, err := ioutil.ReadFile(path)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return t.AddTemplate(name, string(b))
|
||||||
}
|
}
|
||||||
return t.AddTemplate(name, string(b))
|
|
||||||
|
return nil
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *GoHtmlTemplate) generateTemplateNameFrom(base, path string) string {
|
func (t *GoHtmlTemplate) generateTemplateNameFrom(base, path string) string {
|
||||||
|
@ -391,21 +410,8 @@ func (t *GoHtmlTemplate) loadTemplates(absPath string, prefix string) {
|
||||||
tplName = strings.Trim(prefix, "/") + "/" + tplName
|
tplName = strings.Trim(prefix, "/") + "/" + tplName
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO move this into the AddTemplateFile function
|
t.AddTemplateFile(tplName, path)
|
||||||
if strings.HasSuffix(path, ".amber") {
|
|
||||||
compiler := amber.New()
|
|
||||||
// Parse the input file
|
|
||||||
if err := compiler.ParseFile(path); err != nil {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
if _, err := compiler.CompileWithTemplate(t.New(tplName)); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
} else {
|
|
||||||
t.AddTemplateFile(tplName, path)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue