-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
190 lines (163 loc) · 4.59 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
190
package main
import (
"bytes"
"flag"
"fmt"
"github.com/microcosm-cc/bluemonday"
"github.com/russross/blackfriday/v2"
"html/template"
"io"
"io/ioutil"
"os"
"os/exec"
"runtime"
"time"
)
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>
`
)
// content type represents the HTML content tto add into the template
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")
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); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
//run coordinate the execution of the program
//takes the filename of the MD to preview,
//it reads the content of input markdown file into a slice of bytes
//by using the FileRead()
//then pass content to ParseContent() func
//which is responsible for converting MD to HTML
//and returns a potential error
func run(filename string, tFname string, out io.Writer, skipPreview bool) error {
// Read all the data from the input file and check for errors
mdContent, err := ioutil.ReadFile(filename)
if err != nil {
return err
}
htmlData, err := parseContent(mdContent, tFname)
if err != nil {
return err
}
//preview the md content through a temporary file
//use TempFile func that generate a random file name
//store the file in a system temp directory
//instead of creating file locally
tf, err := ioutil.TempFile("", "mdp*.html")
if err != nil {
return err
}
if err := tf.Close(); err != nil {
return err
}
outName := tf.Name()
//print the outName to the out
fmt.Fprintln(out, outName)
if err := saveHTML(outName, htmlData); err != nil {
return err
}
if skipPreview {
return nil
}
defer os.Remove(outName)
return preview(outName)
}
//parseContent parse Markdown content into HTML
//receives a slice of bytes representing the content of the MD file
//returns another slice of bytes with the converted content as HTML
func parseContent(mdContent []byte, tFname string) ([]byte, error) {
// Parse the markdown file through blackfriday and bluemonday
// to generate a valid and safe HTML
body := blackfriday.Run(mdContent)
sanitizedBody := bluemonday.UGCPolicy().SanitizeBytes(body)
// 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
}
}
// Instantiate the content type, adding predefined title and body
c := content{
Title: "Markdown Preview Tool",
Body: template.HTML(sanitizedBody),
}
// Create a buffer of bytes to store the template execution's result
var buffer bytes.Buffer
// Execute the template with the content type,
//inject c into the template and write the results to the buffer
if err := t.Execute(&buffer, c); err != nil {
return nil, err
}
return buffer.Bytes(), nil
}
//saveHTML wrapper of WriteFile to save the content to a html extension
//it receives the entire html Content to be saved, and html file name
//specified by the parameter savedTo
//with file permission of creating a file if it doesn't exist
//that's both readable and writable by the owner's only, readable by everyone
//returns potential error
func saveHTML(savedTo string, data []byte) error {
// Write the bytes to the file
return ioutil.WriteFile(savedTo, data, 0644)
}
//preview takes the temp file name as input
//and return error in case it can't open the file
func preview(filename string) error {
cName := ""
var 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 parameter list
cParams = append(cParams, filename)
//locate executable in $PATH and executes it
cPath, err := exec.LookPath(cName)
if err != nil {
return err
}
//open the file using default program
err = exec.Command(cPath, cParams...).Run()
//add delay for preview to be displayed before cleaning up
time.Sleep(2 * time.Second)
return err
}