Commits

Josh Rickmar committed ec140e2

Clean up scoping and prevent unintentional shadow variables

Comments (0)

Files changed (1)

 	dirFlag      = flag.String(`in`, ``, `markdown file tree`)
 	ignoreRegExp = flag.String(`ignore`, ``, `ignore regexp`)
 	tmplFlag     = flag.String(`tmpl`, ``, `template file`)
+	mdParser     = markdown.NewParser(&markdown.Extensions{Smart: true})
 )
 
 type Navigation struct {
 	abs, _ := filepath.Abs(*dirFlag)
 	dir, inDir := filepath.Split(abs)
 	outDir := filepath.Join(dir, inDir+`.static`)
-	err = os.Chdir(dir)
-	if err != nil {
+	if err = os.Chdir(dir); err != nil {
 		panic(err)
 	}
 
-	fi, err = os.Stat(outDir)
-	if err == nil {
-		err = os.RemoveAll(outDir)
-		if err != nil {
+	if fi, err = os.Stat(outDir); err == nil {
+		if err = os.RemoveAll(outDir); err != nil {
 			panic(err)
 		}
 	}
 }
 
 func mkPage(srcPath string, info os.FileInfo, err error) error {
-	skip, err := regexp.MatchString(*ignoreRegExp, filepath.Base(srcPath))
-	if err != nil {
+	if skip, err := regexp.MatchString(*ignoreRegExp, filepath.Base(srcPath)); err != nil {
 		panic(err)
 	} else if skip {
 		return filepath.SkipDir
 	if err != nil {
 		panic(err)
 	}
+
+	// dest will be null if we're iterating over a directory
 	destPath, dest := getDest(srcPath)
 	if dest != nil {
 		defer dest.Close()
+	} else {
+		if err = os.Mkdir(destPath, 0755); err != nil {
+			panic(err)
+		}
+		return nil
 	}
 
 	pageElems := PageItems{
 		panic(err)
 	}
 
-	if info.IsDir() {
-		err := os.Mkdir(destPath, 0755)
-		if err != nil {
-			panic(err)
-		}
-		return nil
-	}
-
 	/* XXX: index and navitems
 	dir, err := os.Open(filepath.Dir(srcPath))
-	defer dir.Close()
 	if err != nil {
 		return err
+	} else {
+		defer dir.Close()
 	}
 	names, err := dir.Readdirnames(0)
 	if err != nil {
 	}
 	*/
 
-	mdParser := markdown.NewParser(&markdown.Extensions{Smart: true})
 	switch filepath.Ext(srcPath) {
 	case `.md`:
 		buf := new(bytes.Buffer)
 	destPath = strings.Join(parts, string(filepath.Separator))
 	fi, err := os.Stat(path)
 	if err == nil && !fi.IsDir() {
-		dest, err = os.Create(destPath)
-		if err != nil {
+		if dest, err = os.Create(destPath); err != nil {
 			panic(err)
 		}
 	}