shithub: hugo

Download patch

ref: becd4fe337822b450ac88005ccec8414de8351a0
parent: 4e9b04086afcb51103f339c4ad97136a7a10d6a6
author: Joel Scoble <[email protected]>
date: Tue Sep 9 12:57:14 EDT 2014

refactor handling of amber to AddTemplateFile as the TODO note stated. Used switch statement to make it easier to add other template support

--- a/hugolib/template.go
+++ b/hugolib/template.go
@@ -359,11 +359,30 @@
 }
 
 func (t *GoHtmlTemplate) AddTemplateFile(name, path string) error {
-	b, err := ioutil.ReadFile(path)
-	if err != nil {
-		return err
+	// get the suffix and switch on that
+	ext := filepath.Ext(path)
+	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 {
@@ -391,21 +410,8 @@
 				tplName = strings.Trim(prefix, "/") + "/" + tplName
 			}
 
-			// TODO move this into the AddTemplateFile function
-			if strings.HasSuffix(path, ".amber") {
-				compiler := amber.New()
-				// Parse the input file
-				if err := compiler.ParseFile(path); err != nil {
-					return nil
-				}
+			t.AddTemplateFile(tplName, path)
 
-				if _, err := compiler.CompileWithTemplate(t.New(tplName)); err != nil {
-					return err
-				}
-
-			} else {
-				t.AddTemplateFile(tplName, path)
-			}
 		}
 		return nil
 	}