-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rfc2csv.go
253 lines (216 loc) · 4.57 KB
/
rfc2csv.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
package main
import (
"encoding/csv"
"fmt"
gohtml "html"
"io"
"log"
"net/http"
"os"
"path"
"regexp"
"strings"
"sync"
"golang.org/x/net/html"
"golang.org/x/net/html/atom"
)
// Service implements the RFC to CSV conversion
type Service struct {
doc *html.Tokenizer
save chan Row
writer *csv.Writer
wg sync.WaitGroup
}
// Row as written to the CSV
type Row struct {
Category string
Name string
Title string
Description string
Notes string
}
var skipSections = []string{"Introduction", "Definitions",
"IANA Considerations", "Acknowledgements", "References"}
func main() {
if len(os.Args) < 2 {
log.Println("usage: " + os.Args[0] + " 5280 ...")
os.Exit(1)
}
for _, rfc := range os.Args[1:] {
doRFC(rfc)
}
}
func doRFC(rfc string) {
url := "https://tools.ietf.org/html/rfc" + rfc
filename := path.Base(url)
s := &Service{}
go s.newWriter(filename)
err := s.parse(url)
if err != nil {
log.Fatal(err)
}
s.wg.Wait()
}
func (s *Service) newWriter(filename string) {
s.wg.Add(1)
defer s.wg.Done()
file, err := os.Create(filename + ".csv")
if err != nil {
log.Println(err)
return
}
defer file.Close()
s.writer = csv.NewWriter(file)
defer s.writer.Flush()
err = s.writer.Write([]string{
"Category", "Name", "Title", "Description", "Notes"})
if err != nil {
log.Println(err)
return
}
s.save = make(chan Row)
for {
row, more := <-s.save
if !more {
// channel closed, stop writer
return
}
err = s.writer.Write([]string{
row.Category,
row.Name,
row.Title,
row.Description,
row.Notes,
})
if err != nil {
log.Fatal(err)
return
}
}
}
func (s *Service) parse(url string) error {
resp, err := http.Get(url)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return fmt.Errorf(resp.Status)
}
if !strings.HasPrefix(resp.Header.Get("Content-Type"), "text/html") {
return fmt.Errorf("content type must be html")
}
var category string
s.doc = html.NewTokenizer(resp.Body)
for tokenType := s.doc.Next(); tokenType != html.ErrorToken; {
token := s.doc.Token()
if tokenType == html.StartTagToken {
if isSection(&token) {
var row Row
row.Name = s.getNextText()
row.Title = s.getNextText()
row.Title = strings.TrimPrefix(row.Title, ".")
row.Title = strings.TrimSpace(row.Title)
if !strings.Contains(row.Name, ".") {
category = row.Title
}
row.Category = category
row.Description = s.getSection()
if row.Description != "" && !inSlice(category, skipSections) {
s.save <- row
}
}
}
tokenType = s.doc.Next()
}
close(s.save)
err = s.doc.Err()
if err != io.EOF {
return err
}
return nil
}
func (s *Service) skipToEndToken(t *html.Token) {
level := 1
for i := 0; i < 100000; i++ {
tokenType := s.doc.Next()
token := s.doc.Token()
if tokenType == html.StartTagToken && token.DataAtom == t.DataAtom {
level++
} else if tokenType == html.EndTagToken && token.DataAtom == t.DataAtom {
level--
}
if level <= 0 {
break
}
}
}
func (s *Service) getNextText() string {
for i := 0; i < 100000; i++ {
tokenType := s.doc.Next()
if tokenType == html.TextToken {
token := s.doc.Token()
return gohtml.UnescapeString(token.String())
}
}
return ""
}
func (s *Service) getSection() string {
var content string
for i := 0; i < 10000; i++ {
tokenType := s.doc.Next()
token := s.doc.Token()
if isBlocked(&token) {
if tokenType != html.SelfClosingTagToken {
s.skipToEndToken(&token)
}
continue
}
if isSection(&token) {
break
}
if tokenType == html.TextToken {
content = content + gohtml.UnescapeString(token.String())
}
}
// remove duplicate newlines
regex, err := regexp.Compile("(?m)^\n\n*")
if err != nil {
return content
}
content = regex.ReplaceAllString(content, "\n")
// remove section indentation
regex, err = regexp.Compile("(?m)^ ")
if err != nil {
return content
}
content = regex.ReplaceAllString(content, "")
return strings.TrimSpace(content)
}
func isSection(t *html.Token) bool {
if t.DataAtom == atom.A {
for _, a := range t.Attr {
if a.Key == "name" && strings.HasPrefix(a.Val, "section") {
return true
}
}
}
return false
}
func isBlocked(t *html.Token) bool {
for _, a := range t.Attr {
if a.Key == "class" && (a.Val == "invisible" || a.Val == "grey" ||
strings.Contains(a.Val, "noprint")) {
return true
}
}
return false
}
func inSlice(key string, slice []string) bool {
for _, item := range slice {
if strings.ToLower(key) == strings.ToLower(item) {
return true
}
}
return false
}