/
main.go
222 lines (181 loc) · 3.93 KB
/
main.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
package main
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"text/scanner"
"github.com/kardianos/osext"
)
type Fact struct {
Front string
Back string
Tags []string
}
var (
debugEnabled bool
)
func init() {
if os.Getenv("FACTS_DEBUG") == "true" {
debugEnabled = true
}
}
// Just makes output a little more succint for debugging lines so we can show
// more information.
func boolToString(b bool) string {
if b {
return "t"
} else {
return "f"
}
}
func dataFiles() ([]string, error) {
filename, err := osext.Executable()
if err != nil {
return nil, err
}
path, err := filepath.Abs(filename + "/../categories/*.md")
if err != nil {
return nil, err
}
return filepath.Glob(path)
}
func parseMarkdown(str string) (string, []*Fact, error) {
var s scanner.Scanner
var tok rune
// result fields
var facts []*Fact
var header string
// current parsing state
var buf bytes.Buffer
var fact *Fact
inHeader := false
inNote := false
indentCount := 0
whitespaceCount := 0
debug := func(format string, a ...interface{}) {
if debugEnabled {
info := "(indent=%v header=%v note=%v)"
a = append(a, indentCount, boolToString(inHeader), boolToString(inNote))
fmt.Printf("scanner: "+format+"\t\t"+info+"\n", a...)
}
}
debugPlain := func(format string, a ...interface{}) {
if debugEnabled {
fmt.Printf("scanner: "+format+"\n", a...)
}
}
s.Init(strings.NewReader(str))
// set mode to 0 to avoid any attempt at string reading, which may cause
// problems when we run into apostrophes
s.Mode = scanner.ScanFloats | scanner.ScanIdents | scanner.ScanInts
s.Whitespace = 1 << '\r'
for tok != scanner.EOF {
tok = s.Scan()
switch s.TokenText() {
case " ":
debug("[SPACE]")
if inHeader || inNote {
buf.WriteString(s.TokenText())
} else {
whitespaceCount += 1
if whitespaceCount == 4 {
indentCount += 1
whitespaceCount = 0
}
}
// reset everything and handle a completed string
case "\n":
debug("\\n")
// string is likely left with leading and possibly trailing
// whitespace
trimmed := strings.TrimSpace(buf.String())
if inNote && trimmed != "" {
if indentCount == 0 {
debugPlain("FACT (front): %v\n", trimmed)
fact = &Fact{Front: trimmed}
facts = append(facts, fact)
} else {
if fact != nil {
debugPlain("FACT (back): %v\n", trimmed)
fact.Back = trimmed
}
}
}
if inHeader {
debugPlain("HEADER: %v\n", trimmed)
header = trimmed
}
buf.Reset()
inHeader = false
inNote = false
indentCount = 0
whitespaceCount = 0
case "#":
debug("#")
if !inHeader && !inNote {
inHeader = true
} else {
buf.WriteString(s.TokenText())
}
case "*":
debug("*")
if !inHeader && !inNote {
inNote = true
} else {
buf.WriteString(s.TokenText())
}
default:
debug("%s", s.TokenText())
if inHeader || inNote {
buf.WriteString(s.TokenText())
}
// any non-whitespace character resets whitespace count to zero
whitespaceCount = 0
}
}
return header, facts, nil
}
func main() {
files, err := dataFiles()
if err != nil {
panic(err)
}
var facts []*Fact
for _, file := range files {
data, err := ioutil.ReadFile(file)
if err != nil {
panic(err)
}
header, fileFacts, err := parseMarkdown(string(data))
if err != nil {
panic(err)
}
// add header as a tag to all facts
if header != "" {
for _, fact := range fileFacts {
fact.Tags = []string{header}
}
}
facts = append(facts, fileFacts...)
}
var buf bytes.Buffer
for _, fact := range facts {
if debugEnabled {
fmt.Printf("fact: %v\n", *fact)
}
buf.WriteString(fact.Front)
buf.WriteString("\t")
buf.WriteString(fact.Back)
buf.WriteString("\t")
buf.WriteString(strings.Join(fact.Tags, ","))
buf.WriteString("\n")
}
fmt.Printf("writing: facts.tsv\n")
err = ioutil.WriteFile("./facts.tsv", buf.Bytes(), 0644)
if err != nil {
panic(err)
}
}