-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
189 lines (147 loc) · 3.21 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
package main
import (
"bytes"
"flag"
"fmt"
"log"
"os"
"os/exec"
"runtime"
"time"
"github.com/SebastiaanKlippert/go-wkhtmltopdf"
"github.com/microcosm-cc/bluemonday"
"github.com/russross/blackfriday/v2"
)
const (
header = `<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<title>Markdown Preview Tool</title>
</head>
<body>
`
footer = `
</body>
</html>
`
)
func main() {
filename := flag.String("file", "", "markdown file")
preview := flag.Bool("preview", false, "directly preview in browser")
delete := flag.Bool("delete", false, "delete the converted html file")
pdf := flag.Bool("pdf", false, "export to pdf file")
flag.Parse()
if *filename == "" {
flag.Usage()
os.Exit(1)
}
err := run(*filename, *preview, *delete, *pdf)
if err != nil {
log.Fatal(err)
os.Exit(1)
}
}
func run(file string, previewFile bool, deleteFile bool, exportPDF bool) error {
input, err := os.ReadFile(file)
if err != nil {
log.Fatal(err)
os.Exit(1)
}
htmlData := parseContent(input)
if _, err := os.Stat("output_html"); os.IsNotExist(err) {
err = os.Mkdir("output_html", os.ModePerm)
if err != nil {
log.Fatal(err)
}
}
temp, err := os.CreateTemp("./output_html", "markdown*.html")
if err != nil {
log.Fatal(err)
}
temp.Close()
outName := temp.Name()
fmt.Println(outName)
if err := saveHTML(outName, htmlData); err != nil {
return err
}
if exportPDF {
exportPdf(outName)
}
if previewFile {
preview(outName)
}
if deleteFile {
defer os.Remove(outName)
}
return nil
}
func parseContent(input []byte) []byte {
// use blackfridy to convert to html
output := blackfriday.Run(input)
// santize the output
body := bluemonday.UGCPolicy().SanitizeBytes(output)
var buffer bytes.Buffer
buffer.WriteString(header)
buffer.Write(body)
buffer.WriteString(footer)
return buffer.Bytes()
}
func saveHTML(outName string, data []byte) error {
return os.WriteFile(outName, data, 0644)
}
func preview(file string) error {
cmd := ""
args := []string{}
switch runtime.GOOS {
case "linux":
cmd = "xdg-open"
case "windows":
cmd = "cmd.exe"
args = []string{"/c", "start"}
case "darwin":
cmd = "open"
default:
return fmt.Errorf("OS not supported")
}
// so the string would be "/c start file"
args = append(args, file)
err := exec.Command(cmd, args...).Run()
time.Sleep(2 * time.Second)
return err
}
// https://github.com/SebastiaanKlippert/go-wkhtmltopdf#usage
func exportPdf(file string) {
pdfg, err := wkhtmltopdf.NewPDFGenerator()
if err != nil {
log.Fatal(err)
}
if _, err := os.Stat("output_pdf"); os.IsNotExist(err) {
err = os.Mkdir("output_pdf", os.ModePerm)
if err != nil {
log.Fatal(err)
}
}
pdfg.Dpi.Set(300)
pdfg.Grayscale.Set(true)
html_file := wkhtmltopdf.NewPage(file)
html_file.FooterRight.Set("[page]")
html_file.FooterFontSize.Set(10)
html_file.Zoom.Set(0.95)
pdfg.AddPage(html_file)
// Create PDF document in internal buffer
err = pdfg.Create()
if err != nil {
log.Fatal(err)
}
temp, err := os.CreateTemp("./output_pdf", "pdf*.pdf")
if err != nil {
log.Fatal(err)
}
temp.Close()
outName := temp.Name()
err = pdfg.WriteFile(outName)
if err != nil {
log.Fatal(err)
}
}