Commits

Josh Rickmar  committed 299c600

Buffer the output for (*Template).Execute() and io.Copy()

  • Participants
  • Parent commits 15ce0ab

Comments (0)

Files changed (1)

 package main
 
 import (
+	"bufio"
 	"bytes"
 	"flag"
 	"fmt"
 		panic(err)
 	}
 
-	// dest will be null if we're iterating over a directory
-	destPath, dest := getDest(srcPath)
-	if dest != nil {
+	var writer *bufio.Writer
+	var destPath string
+	if destPath, dest := getDest(srcPath); dest != nil {
+		writer = bufio.NewWriter(dest);
+
+		// writer flushes before dest closes
 		defer dest.Close()
-	} else {
+		defer func() {
+			if writer != nil && err == nil {
+				err = writer.Flush()
+			}
+		}()
+	} else {	// dest will be null if we're iterating over a directory
 		if err = os.Mkdir(destPath, 0755); err != nil {
 			panic(err)
 		}
 		buf := new(bytes.Buffer)
 		mdParser.Markdown(src, markdown.ToHTML(buf))
 		pageElems.Main = template.HTML(buf.String())
-		if err = tmplParser.Execute(dest, pageElems); err != nil {
+		if err = tmplParser.Execute(writer, pageElems); err != nil {
 			return fmt.Errorf("Could not parse template for file %s\n", destPath);
 		}
 	default:
-		io.Copy(dest, src)
+		io.Copy(writer, src)
 	}
 	return nil
 }