-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
54 lines (46 loc) · 995 Bytes
/
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
package main
import (
"bufio"
"fmt"
"os"
"strings"
)
func check(e error) {
if e != nil {
panic(e)
}
}
func main() {
fname := os.Args[1]
name := strings.Split(fname, ".")
if strings.Compare(strings.ToLower(name[len(name)-1]), "md") != 0 {
panic("input file must be a markdown file (.md)")
}
wfile, err := os.Create(name[0] + ".html")
check(err)
defer wfile.Close()
writer := bufio.NewWriter(wfile)
if len(os.Args) < 3 || os.Args[2] != "-nocss" {
fmt.Fprintln(writer, css())
}
rfile, err := os.Open(fname)
check(err)
defer rfile.Close()
reader := bufio.NewReader(rfile)
lines := make([]Line, 0)
for {
line, err := reader.ReadString('\n')
if err != nil { // io.EOF
break
}
// Remove newline at the end of line
if len(line) > 1 && line[len(line)-1] == '\n' {
line = line[:len(line)-1]
}
lines = append(lines, convert(line))
}
writer.WriteString("<body>")
writer.WriteString(generate(lines))
writer.WriteString("</body>")
writer.Flush()
}