changeset 18:adc92b0118f4

Fix crash when not providing a template
author Jonatan Werpers <jonatan@werpers.com>
date Thu, 25 Jun 2020 21:57:11 +0200
parents c515a9fa8bd5
children fbdb89dc8e8c
files test.md wdown.go
diffstat 2 files changed, 8 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/test.md	Thu Jun 25 21:18:30 2020 +0200
+++ b/test.md	Thu Jun 25 21:57:11 2020 +0200
@@ -54,7 +54,7 @@
 ## TODO
 Some things need doing
  - [x] Fix bug where there's a nil error when there is no yaml in .md file
- - [ ] Fix bug where wdown doesn't work without a template
+ - [x] Fix bug where wdown doesn't work without a template
  - [x] Build the simplest possible markdown commandline utility
  - [x] Add command-line argument parsing
  - [x] Allow template file as an input
--- a/wdown.go	Thu Jun 25 21:18:30 2020 +0200
+++ b/wdown.go	Thu Jun 25 21:57:11 2020 +0200
@@ -34,13 +34,7 @@
 		useTemplate = true
 	}
 
-	tmpl, err := mustache.ParseFile(templateFilename)
-	if err != nil {
-		log.Fatal(err)
-	}
-
 	source, err := ioutil.ReadFile(flag.Arg(0))
-
 	if err != nil {
 		log.Fatal(err)
 	}
@@ -57,21 +51,25 @@
 		),
 	)
 
-	var htmlContent bytes.Buffer
+	htmlContent := new(bytes.Buffer)
 	parserContext := parser.NewContext()
-	err = md.Convert(source, &htmlContent, parser.WithContext(parserContext))
+	err = md.Convert(source, htmlContent, parser.WithContext(parserContext))
 	if err != nil {
 		log.Fatal(err)
 	}
 
 	if useTemplate {
+		tmpl, err := mustache.ParseFile(templateFilename)
+		if err != nil {
+			log.Fatal(err)
+		}
 		templateVars := meta.Get(parserContext)
 		if templateVars == nil {
 			templateVars = map[string]interface{}{}
 		}
 		templateVars["content"] = htmlContent.String()
 
-		err := tmpl.FRender(os.Stdout, templateVars)
+		err = tmpl.FRender(os.Stdout, templateVars)
 		if err != nil {
 			log.Fatal(err)
 		}