-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
169 lines (137 loc) · 3.54 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
package main
import (
"bytes"
"flag"
"fmt"
"html/template"
"io"
"os"
"os/exec"
"path/filepath"
"runtime"
"time"
"github.com/microcosm-cc/bluemonday"
"github.com/russross/blackfriday/v2"
)
const (
defaultTemplate = `<!DOCTYPE html><html><head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<title>{{ .Title}}</title></head><body>
{{ .Body}}<</body>/html>`
)
type content struct {
Title string
Body template.HTML
}
func main() {
// Parse flags
filename := flag.String("file", "", "Markdown file to preview")
skipPreview := flag.Bool("s", false, "Skip auto-preview")
tFname := flag.String("t", "", "Alternate template name")
showName := flag.Bool("name", false, "Show file name")
flag.Parse()
// If user did not provide input file, show usage
if *filename == "" {
flag.Usage()
os.Exit(1)
}
if err := run(*filename, *tFname, os.Stdout, *skipPreview, *showName); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
func run(filename string, tFname string, out io.Writer, skipPreview bool, showName bool) error {
// Read all the data from the input file and check for errors
input, err := os.ReadFile(filename)
if err != nil {
return err
}
htmlData, err := parseContent(input, tFname)
if err != nil {
return err
}
fileName := filepath.Base(filename)
// Create temporary file and check for errors
temp, err := os.CreateTemp("", "mdp*.html")
if err != nil {
return err
}
if err := temp.Close(); err != nil {
return err
}
outName := temp.Name()
fmt.Fprintln(out, outName)
htmlDataWithFileName := fmt.Sprintf("%s\nFile Name: %s\n", htmlData, fileName)
if err := saveHTML(outName, []byte(htmlDataWithFileName)); err != nil {
return err
}
if showName {
fmt.Printf("File Name: %s\n", fileName)
}
if skipPreview {
return nil
}
defer os.Remove(outName)
return preview(outName)
}
func parseContent(input []byte, tFname string) ([]byte, error) {
// Prase the markdown file through blackfriday and bluemonday
// to generate a valid and safe HTML
output := blackfriday.Run(input)
body := bluemonday.UGCPolicy().SanitizeBytes(output)
// Parse the contents of the defaultTemplate const into a new Template
t, err := template.New("mdp").Parse(defaultTemplate)
if err != nil {
return nil, err
}
// If user provided alternate template file, replace template
if tFname != "" {
t, err = template.ParseFiles(tFname)
if err != nil {
return nil, err
}
}
// Instantitate the content type, adding the title and body
c := content{
Title: "Markdown Preview Tool",
Body: template.HTML(body),
}
// Create a buffer of bytes to write to file
var buffer bytes.Buffer
// Excute the template with the content type
if err := t.Execute(&buffer, c); err != nil {
return nil, err
}
return buffer.Bytes(), nil
}
func saveHTML(outFname string, data []byte) error {
return os.WriteFile(outFname, data, 0o644)
}
func preview(fname string) error {
cName := ""
cParams := []string{}
// Define executable based on OS
switch runtime.GOOS {
case "linux":
cName = "xdg-open"
case "windows":
cName = "cmd.exe"
cParams = []string{"/C", "start"}
case "darwin":
cName = "open"
default:
return fmt.Errorf("OS not supported")
}
// Append filename to parameters slice
cParams = append(cParams, fname)
// Locate executable in PATH
cPath, err := exec.LookPath(cName)
if err != nil {
return err
}
// Open the file using default program
err = exec.Command(cPath, cParams...).Run()
// Give the browser some time to open the file before deleting it
time.Sleep(2 * time.Second)
return err
}