-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
57 lines (53 loc) · 1.42 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
package main
import (
"flag"
"io"
"log"
"os"
"github.com/sivukhin/godjot/djot_parser"
"github.com/sivukhin/godjot/html_writer"
)
func main() {
from := flag.String("from", "", "path to the input djot file (empty or '-' for stdin)")
to := flag.String("to", "", "path to the output html file (empty or '-' for stdout)")
overwrite := flag.Bool("overwrite", false, "overwrite output html file")
flag.Parse()
var inReader io.Reader
var outWriter io.Writer
if *from == "" || *from == "-" {
inReader = os.Stdin
} else {
f, err := os.Open(*from)
if err != nil {
log.Fatalf("failed to open input file %v: %v", *from, err)
}
inReader = f
}
if *to == "" || *to == "-" {
outWriter = os.Stdout
} else {
flags := os.O_CREATE | os.O_WRONLY | os.O_TRUNC
if !*overwrite {
flags |= os.O_EXCL
}
f, err := os.OpenFile(*to, flags, 0660)
if err != nil {
log.Fatalf("failed to open output file %v: %v", *to, err)
}
outWriter = f
}
input, err := io.ReadAll(inReader)
if err != nil {
log.Fatalf("failed to read input file %v: %v", *from, err)
}
ast := djot_parser.BuildDjotAst(input)
context := djot_parser.NewConversionContext("html", djot_parser.DefaultConversionRegistry)
html := []byte(context.ConvertDjotToHtml(&html_writer.HtmlWriter{}, ast...))
for len(html) > 0 {
n, err := outWriter.Write(html)
if err != nil {
log.Fatalf("failed to write output file %v: %v", *to, err)
}
html = html[n:]
}
}