This commit is contained in:
Mark McGranaghan 2012-10-07 08:37:04 -07:00
parent b16f814be0
commit fd02173b9a
2 changed files with 21 additions and 18 deletions

View File

@ -37,13 +37,13 @@ func All(elems []string, f func(string) bool) bool {
} }
func Filter(vs []string, f func(string) bool) []string { func Filter(vs []string, f func(string) bool) []string {
filtered := []string{} vsf := make([]string, 0)
for _, v := range elems { for _, v := range elems {
if f(v) { if f(v) {
filtered = append(filtered, v) vsf = append(vsf, v)
} }
} }
return filtered return vsf
} }
func Map(strs []string, f func(string) string) []string { func Map(strs []string, f func(string) string) []string {

View File

@ -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 { func render(bin string, arg []string, src string) []byte {
cmd := exec.Command(bin, arg...) cmd := exec.Command(bin, arg...)
in, _ := cmd.StdinPipe() in, _ := cmd.StdinPipe()
@ -65,9 +75,8 @@ func ensureCache() {
} }
func readLines(path string) []string { func readLines(path string) []string {
srcBytes, err := ioutil.ReadFile(path) src := mustReadFile(path)
check(err) return strings.Split(src, "\n")
return strings.Split(string(srcBytes), "\n")
} }
func mustGlob(glob string) []string { func mustGlob(glob string) []string {
@ -187,23 +196,17 @@ func main() {
</head> </head>
<body>`) <body>`)
indexBytes, err := ioutil.ReadFile("src/index.txt") indexLines := readLines("src/index.txt")
check(err) indexNames := filterStrings(indexLines, func(s string) bool {
indexLines := strings.Split(string(indexBytes), "\n") return s != ""
indexNames := make([]string, 0) })
for _, indexLine := range indexLines {
if indexLine != "" {
indexNames = append(indexNames, indexLine)
}
}
for _, indexName := range indexNames { for _, indexName := range indexNames {
fmt.Fprintf(outF, `<div id="%s">`, indexName) fmt.Fprintf(outF, `<div id="%s">`, indexName)
if (indexName == "title") || (indexName == "contents") || (indexName == "introduction") { if (indexName == "title") || (indexName == "contents") || (indexName == "introduction") {
sourcePath := "src/" + indexName + "/" + indexName + ".html" sourcePath := "src/" + indexName + "/" + indexName + ".html"
sourceBytes, err := ioutil.ReadFile(sourcePath) source := mustReadFile(sourcePath)
check(err) _, err = outF.WriteString(source)
_, err = outF.Write(sourceBytes)
check(err) check(err)
} else { } else {
chapterPath := "src/" + indexName chapterPath := "src/" + indexName