trunkless/gutcontent.go
2024-01-31 21:05:10 -08:00

55 lines
924 B
Go

package main
import (
"bufio"
"fmt"
"os"
"strings"
)
/*
Given a project gutenberg plaintext book filename, open it and print just the content.
*/
func main() {
if len(os.Args) < 2 {
fmt.Fprintln(os.Stderr, "need a filename argument")
os.Exit(1)
}
filename := os.Args[1]
f, err := os.Open(filename)
if err != nil {
fmt.Fprintf(os.Stderr, "could not open '%s' for reading\n", filename)
os.Exit(2)
}
s := bufio.NewScanner(f)
inHeader := true
inFooter := false
skippedAll := true
for s.Scan() {
text := strings.TrimSpace(s.Text())
if inFooter {
break
}
if strings.HasPrefix(text, "*** START") {
inHeader = false
continue
}
if inHeader {
continue
}
if strings.HasPrefix(text, "*** END") {
inFooter = true
continue
}
fmt.Println(text)
if skippedAll {
skippedAll = false
}
}
if skippedAll {
fmt.Fprintln(os.Stderr, "warning: found no text to print")
}
}