-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
133 lines (109 loc) Β· 1.69 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
package main
import (
"flag"
"fmt"
"io"
"log"
"os"
)
const (
VERSION = "0.1.2"
USAGE = `NAME:
anki-md - markdown to anki flash cards converter
USAGE:
anki-md -i [input-file] -o [output-file]
EXAMPLES:
$ anki-md -i deck.md -o deck.csv
$ cat deck.md | anki-md -o deck.csv
$ anki-md -i deck.md > test.csv
VERSION:
%s
WEBSITE:
https://github.com/jpbruinsslot/anki-md
GLOBAL OPTIONS:
-i, -input [input-file] input file
-o, -output [output-file] output file
-html convert field content to html
-h, -help
`
)
var (
flgInput string
flgOutput string
flgHTML bool
)
func init() {
flag.StringVar(
&flgInput,
"i",
"",
"anki-md input file",
)
flag.StringVar(
&flgInput,
"input",
"",
"anki-md input file",
)
flag.StringVar(
&flgOutput,
"o",
"",
"output file",
)
flag.StringVar(
&flgOutput,
"output",
"",
"output file",
)
flag.BoolVar(
&flgHTML,
"html",
false,
"convert field content to html",
)
flag.Usage = func() {
fmt.Printf(USAGE, VERSION)
}
}
func main() {
flag.Parse()
var err error
// Input
var r io.Reader
if flgInput != "" {
r, err = os.Open(flgInput)
if err != nil {
log.Fatal(err)
}
} else {
r = os.Stdin
}
// Output
var fp *os.File
if flgOutput != "" {
fp, err = os.Create(flgOutput)
if err != nil {
log.Fatal(err)
}
defer fp.Close()
} else {
fp = os.Stdout
}
// Flag options
var options Option
if flgHTML {
options = options | HTML
}
// Parse input
d, err := NewParser(r).ParseDeck()
if err != nil {
log.Fatal(err)
}
// Write output
err = NewDeckWriter(fp, options).WriteDeck(d)
if err != nil {
log.Fatal(err)
}
}