-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmd2csv.go
More file actions
236 lines (198 loc) · 4.86 KB
/
md2csv.go
File metadata and controls
236 lines (198 loc) · 4.86 KB
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
package main
import (
"bufio"
"os"
"regexp"
"path"
"sync"
"fmt"
"io"
"log"
"io/ioutil"
"encoding/csv"
"net/http"
"strings"
"github.com/gomarkdown/markdown/parser"
"github.com/gomarkdown/markdown/ast"
)
// Service implements the MarkDown (MD) to CSV conversion
type Service struct {
doc ast.Node
wg sync.WaitGroup
writer *csv.Writer
save chan Row
}
var sectionFormat = regexp.MustCompile(`^([0-9]+\.?)+$`)
var sectionSeperator = regexp.MustCompile(`\s`)
var doubleNewlines = regexp.MustCompile(`(?m)^[\s\n$]+`)
var skipSections = []string{"Introduction", "Scope", "Definitions", "Acronyms",
"Revisions", "PUBLICATION AND REPOSITORY RESPONSIBILITIES",
"Acknowledgements", "References"}
func main() {
if len(os.Args) < 2 {
log.Println("usage: " + os.Args[0] + " https://example.com/document.md ...")
os.Exit(1)
}
for _, arg := range os.Args[1:] {
fmt.Println("Processing:", arg)
if strings.HasPrefix(arg, "http") {
err := fromURL(arg)
if err != nil {
log.Fatal(err)
}
} else {
file, err := os.Open(arg)
if err != nil {
log.Fatal(err)
}
parse(arg, bufio.NewReader(file))
}
}
}
func fromURL(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)
}
// text/markdown - text/x-markdown - text/plain
if !strings.HasPrefix(resp.Header.Get("Content-Type"), "text/") {
return fmt.Errorf("content type must be text/")
}
return parse(path.Base(url), resp.Body)
}
func parse(name string, data io.Reader) error {
s := &Service{}
go s.newWriter(name)
markdown, err := ioutil.ReadAll(data)
if err != nil {
log.Fatal(err)
}
p := parser.New()
s.doc = p.Parse(markdown)
fmt.Println("Walking", name)
ast.Walk(s.doc, &NodeVisitor{s: s})
fmt.Println("Done walking", name)
close(s.save)
fmt.Println("Almost done with", name)
s.wg.Wait()
fmt.Println("Done with", name)
return nil
}
// NodeVisitor implements the ast.NodeVisitor interface
type NodeVisitor struct {
Row
index sync.Map
s *Service
}
// Visit defines what to do for a specific node type
func (n *NodeVisitor) Visit(node ast.Node, entering bool) ast.WalkStatus {
fmt.Print(".")
//fmt.Printf("---------------------------- %T, %t\n", node, entering)
switch node := node.(type) {
case *ast.Text:
if n.Category != "" {
n.Description = n.Description + string(node.Literal)
}
case *ast.Softbreak:
if n.Category != "" {
n.Description = n.Description + "\n"
}
case *ast.Hardbreak:
if n.Category != "" {
n.Description = n.Description + "\n\n"
}
case *ast.Emph:
case *ast.Strong:
case *ast.Del:
case *ast.BlockQuote:
case *ast.Aside:
case *ast.Link:
case *ast.CrossReference:
case *ast.Citation:
case *ast.Image:
case *ast.Code:
case *ast.CodeBlock:
case *ast.Caption:
case *ast.CaptionFigure:
case *ast.Document:
// save remaining data when leaving document
if !entering {
n.save()
fmt.Println()
}
case *ast.Paragraph:
if n.Category != "" {
n.Description = n.Description + "\n"
}
case *ast.HTMLSpan:
case *ast.HTMLBlock:
case *ast.Heading:
if entering {
// Check if heading has a text child
var title []string
titleNode := ast.GetFirstChild(node)
if txt, ok := titleNode.(*ast.Text); ok {
title = sectionSeperator.Split(string(txt.Literal), 2)
} else {
return ast.GoToNext
}
if len(title) != 2 {
return ast.GoToNext
}
// first part of title is expected to be a pragraph/section number
title[0] = strings.TrimSpace(title[0])
if !sectionFormat.MatchString(title[0]) {
return ast.GoToNext
}
n.save()
n.Name = strings.Trim(title[0], ".") // trimmed above for regex
n.Title = strings.Trim(strings.TrimSpace(title[1]), ".")
n.Description = ""
if strings.HasPrefix(n.Category, n.Name) {
n.Category = n.Title
}
n.index.Store(n.Name, n.Title)
if strings.Count(n.Name, ".") == 0 {
n.Category = n.Title
} else if cat, ok := n.index.Load(n.Name[:strings.LastIndex(n.Name, ".")]); ok {
n.Category = cat.(string)
}
return ast.SkipChildren
}
case *ast.HorizontalRule:
case *ast.List:
case *ast.ListItem:
if entering {
n.Description = n.Description + "\t"
}
case *ast.Table:
case *ast.TableCell:
case *ast.TableHeader:
case *ast.TableBody:
case *ast.TableRow:
case *ast.TableFooter:
case *ast.Math:
case *ast.MathBlock:
case *ast.DocumentMatter:
case *ast.Callout:
case *ast.Index:
case *ast.Subscript:
case *ast.Superscript:
case *ast.Footnotes:
default:
panic(fmt.Sprintf("Unknown node %T", node))
}
return ast.GoToNext
}
func (n *NodeVisitor) save() {
if n.Description != "" && !inSlice(n.Category, skipSections) &&
!inSlice(n.Title, skipSections) {
n.Description = strings.TrimSpace(n.Description)
n.Description = doubleNewlines.ReplaceAllString(n.Description, "\n")
n.s.save <- n.Row
}
}