-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
76 lines (61 loc) · 1.81 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
package main
import (
"bytes"
"flag"
"html/template"
"log/slog"
"os"
"github.com/yuin/goldmark"
)
type PageData struct {
Content template.HTML
}
func main() {
handler := slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{})
logger := slog.New(handler)
markdownDocument := flag.String("markdown", "post.md", "the input markdown post file")
templateFile := flag.String("template", "template.html", "the template file for the markdown document")
outputFile := flag.String("output", "output.html", "the output html file")
flag.Parse()
if markdownDocument == nil || *markdownDocument == "" {
logger.Error("the markdown document flag must be present")
return
}
if templateFile == nil || *templateFile == "" {
logger.Error("the template file must be present")
return
}
if outputFile == nil || *outputFile == "" {
logger.Error("the output file must be present")
return
}
markdownContent, err := os.ReadFile(*markdownDocument)
if err != nil {
logger.Error("there was an error parsing the markdown content", "err", err)
return
}
var buf bytes.Buffer
if err := goldmark.Convert(markdownContent, &buf); err != nil {
logger.Error("error converting markdown into html", "err", err)
return
}
tmpl, err := template.ParseFiles(*templateFile)
if err != nil {
logger.Error("error parsing template file", "err", err)
return
}
output, err := os.Create(*outputFile)
if err != nil {
logger.Error("error creating output file", "err", err)
return
}
defer output.Close()
data := PageData{
Content: template.HTML(buf.String()),
}
if err := tmpl.Execute(output, data); err != nil {
logger.Error("error rendering template with markdown", "err", err)
return
}
logger.Info("the template was successfully rendered", "input", *markdownDocument, "template", *templateFile, "output", *outputFile)
}