From fd02173b9a90689579fb31b904d38d29e7495823 Mon Sep 17 00:00:00 2001 From: Mark McGranaghan Date: Sun, 7 Oct 2012 08:37:04 -0700 Subject: [PATCH] refactor --- .../collection-functions.go | 6 ++-- tool/build-html.go | 33 ++++++++++--------- 2 files changed, 21 insertions(+), 18 deletions(-) diff --git a/src/collection-functions/collection-functions.go b/src/collection-functions/collection-functions.go index 1db115a..cf308c0 100644 --- a/src/collection-functions/collection-functions.go +++ b/src/collection-functions/collection-functions.go @@ -37,13 +37,13 @@ func All(elems []string, f func(string) bool) bool { } func Filter(vs []string, f func(string) bool) []string { - filtered := []string{} + vsf := make([]string, 0) for _, v := range elems { if f(v) { - filtered = append(filtered, v) + vsf = append(vsf, v) } } - return filtered + return vsf } func Map(strs []string, f func(string) string) []string { diff --git a/tool/build-html.go b/tool/build-html.go index 468f2a6..dadf5eb 100644 --- a/tool/build-html.go +++ b/tool/build-html.go @@ -19,6 +19,16 @@ func check(err error) { } } +func filterStrings(vs []string, f func(string) bool) []string { + vsf := make([]string, 0) + for _, v := range vs { + if f(v) { + vsf = append(vsf, v) + } + } + return vsf +} + func render(bin string, arg []string, src string) []byte { cmd := exec.Command(bin, arg...) in, _ := cmd.StdinPipe() @@ -65,9 +75,8 @@ func ensureCache() { } func readLines(path string) []string { - srcBytes, err := ioutil.ReadFile(path) - check(err) - return strings.Split(string(srcBytes), "\n") + src := mustReadFile(path) + return strings.Split(src, "\n") } func mustGlob(glob string) []string { @@ -187,23 +196,17 @@ func main() { `) - indexBytes, err := ioutil.ReadFile("src/index.txt") - check(err) - indexLines := strings.Split(string(indexBytes), "\n") - indexNames := make([]string, 0) - for _, indexLine := range indexLines { - if indexLine != "" { - indexNames = append(indexNames, indexLine) - } - } + indexLines := readLines("src/index.txt") + indexNames := filterStrings(indexLines, func(s string) bool { + return s != "" + }) for _, indexName := range indexNames { fmt.Fprintf(outF, `
`, indexName) if (indexName == "title") || (indexName == "contents") || (indexName == "introduction") { sourcePath := "src/" + indexName + "/" + indexName + ".html" - sourceBytes, err := ioutil.ReadFile(sourcePath) - check(err) - _, err = outF.Write(sourceBytes) + source := mustReadFile(sourcePath) + _, err = outF.WriteString(source) check(err) } else { chapterPath := "src/" + indexName