1//go:build errata
2
3package main
4
5// Convert eid html file, e.g. https://www.rfc-editor.org/errata/eid3192 to text with leading blank line for references.
6// See Makefile, run with "go run errata.go < eid.html >eid.txt"
7// I could not find a source for the text version of errata.
8
9import (
10 "bufio"
11 "fmt"
12 "log"
13 "os"
14
15 "golang.org/x/net/html"
16)
17
18func xcheckf(err error, format string, args ...any) {
19 if err != nil {
20 log.Fatalf("%s: %s", fmt.Sprintf(format, args...), err)
21 }
22}
23
24func main() {
25 log.SetFlags(0)
26 doc, err := html.Parse(os.Stdin)
27 xcheckf(err, "parsing html")
28 out := bufio.NewWriter(os.Stdout)
29 _, err = out.WriteString("\n") // First line for references.
30 xcheckf(err, "write")
31
32 // We will visit the html nodes. We skip <form>'s. We turn on text
33 // output when we encounter an h4, and we stop again when we see a div
34 // or form. This works at the moment, but may break in the future.
35 output := false
36 var walk func(*html.Node)
37 walk = func(n *html.Node) {
38 if n.Type == html.ElementNode {
39 if n.Data == "form" {
40 return
41 }
42 if !output && n.Data == "h4" {
43 output = true
44 } else if output && (n.Data == "div" || n.Data == "form") {
45 output = false
46 }
47 }
48 if output && n.Type == html.TextNode {
49 _, err := out.WriteString(n.Data)
50 xcheckf(err, "write")
51 }
52 for c := n.FirstChild; c != nil; c = c.NextSibling {
53 walk(c)
54 }
55 }
56 walk(doc)
57 err = out.Flush()
58 xcheckf(err, "flush")
59}
60