-
Notifications
You must be signed in to change notification settings - Fork 0
/
md2html.go
140 lines (120 loc) · 2.77 KB
/
md2html.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
package md2html
// Copyright 2015 The Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// An example command-line tool that uses opennota/markdown to process markdown input.
import (
"fmt"
"html"
"io"
"io/ioutil"
"os"
"strings"
"github.com/morgulbrut/colorlog"
"github.com/morgulbrut/dotgo/converter/config"
"gitlab.com/golang-commonmark/markdown"
)
var title string
func readFromFile(fn string) ([]byte, error) {
f, err := os.Open(fn)
if err != nil {
return nil, err
}
defer f.Close()
return ioutil.ReadAll(f)
}
func extractText(tok markdown.Token) string {
switch tok := tok.(type) {
case *markdown.Text:
return tok.Content
case *markdown.Inline:
text := ""
for _, tok := range tok.Children {
text += extractText(tok)
}
return text
}
return ""
}
func writePreamble(w io.Writer) error {
var opening string
var ending string
opening = `<!DOCTYPE html>
<html>`
_, err := fmt.Fprintf(w, `%s
<head>
<meta charset="utf-8"%s>
<title>%s</title>
</head>
<body>
`, opening, ending, html.EscapeString(title))
return err
}
func writePostamble(w io.Writer) error {
_, err := fmt.Fprint(w, `</body>
</html>
`)
return err
}
func Html(cf config.File) {
colorlog.Info("[Md2html]: converting")
var rendererOutput string
data, err := readFromFile(cf.Infile)
if err != nil {
colorlog.Fatal("[Md2html] readFromFile(): %s", err.Error())
os.Exit(1)
}
md := markdown.New(
markdown.HTML(true),
markdown.Tables(true),
markdown.Linkify(true),
markdown.Typographer(true),
markdown.XHTMLOutput(false),
)
tokens := md.Parse(data)
if len(tokens) > 0 {
if heading, ok := tokens[0].(*markdown.HeadingOpen); ok {
for i := 1; i < len(tokens); i++ {
if tok, ok := tokens[i].(*markdown.HeadingClose); ok && tok.Lvl == heading.Lvl {
break
}
title += extractText(tokens[i])
}
title = strings.TrimSpace(title)
}
}
rendererOutput = md.RenderTokensToString(tokens)
var outfile string
if cf.Type == "md2pdf" && cf.Converter == "wkhtmltopdf" {
outfile = "temp.html"
} else {
outfile = cf.Outfile
}
f, err := os.OpenFile(outfile, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0600)
if err != nil {
colorlog.Fatal("[Md2html] OpenFile(): %s", err.Error())
os.Exit(1)
}
defer func() {
err := f.Close()
if err != nil {
colorlog.Fatal("[Md2html] Close(): %s", err.Error())
os.Exit(1)
}
}()
err = writePreamble(f)
if err != nil {
colorlog.Fatal("[Md2html] writePreamble(): %s", err.Error())
os.Exit(1)
}
_, err = f.WriteString(rendererOutput)
if err != nil {
colorlog.Fatal("[Md2html] WriteString(): %s", err.Error())
os.Exit(1)
}
err = writePostamble(f)
if err != nil {
colorlog.Fatal("[Md2html] writePostamble(): %s", err.Error())
os.Exit(1)
}
}