Mercurial > repos > public > wdown
comparison wdown.go @ 6:a5f397e25cb7
Add ability to render the markdown inside a template
author | Jonatan Werpers <jonatan@werpers.com> |
---|---|
date | Tue, 16 Jun 2020 22:10:31 +0200 |
parents | 534fa4c6668e |
children | a5aa39557726 |
comparison
equal
deleted
inserted
replaced
5:534fa4c6668e | 6:a5f397e25cb7 |
---|---|
1 package main | 1 package main |
2 | 2 |
3 import ( | 3 import ( |
4 "bytes" | |
4 "flag" | 5 "flag" |
5 "fmt" | 6 "fmt" |
6 "io/ioutil" | 7 "io/ioutil" |
7 "log" | 8 "log" |
8 "os" | 9 "os" |
9 | 10 |
11 "github.com/cbroglie/mustache" | |
10 "github.com/yuin/goldmark" | 12 "github.com/yuin/goldmark" |
11 highlighting "github.com/yuin/goldmark-highlighting" | 13 highlighting "github.com/yuin/goldmark-highlighting" |
12 "github.com/yuin/goldmark/extension" | 14 "github.com/yuin/goldmark/extension" |
13 "github.com/yuin/goldmark/parser" | 15 "github.com/yuin/goldmark/parser" |
14 ) | 16 ) |
15 | 17 |
16 func main() { | 18 func main() { |
19 var templateFilename string | |
20 | |
21 flag.StringVar(&templateFilename, "tmpl", "", "Template file to render the html inside. The content of the markdown file will replace the placeholder {{content}}.") | |
22 | |
17 flag.Parse() | 23 flag.Parse() |
18 | 24 |
19 if flag.NArg() < 1 { | 25 if flag.NArg() < 1 { |
20 fmt.Println("Please provide a markdown file for parsing.") | 26 fmt.Println("Please provide a markdown file for parsing.") |
21 os.Exit(1) | 27 os.Exit(1) |
28 } | |
29 | |
30 var useTemplate = false | |
31 if templateFilename != "" { | |
32 useTemplate = true | |
33 } | |
34 | |
35 tmpl, err := mustache.ParseFile(templateFilename) | |
36 if err != nil { | |
37 log.Fatal(err) | |
22 } | 38 } |
23 | 39 |
24 source, err := ioutil.ReadFile(flag.Arg(0)) | 40 source, err := ioutil.ReadFile(flag.Arg(0)) |
25 | 41 |
26 if err != nil { | 42 if err != nil { |
35 goldmark.WithParserOptions( | 51 goldmark.WithParserOptions( |
36 parser.WithAutoHeadingID(), | 52 parser.WithAutoHeadingID(), |
37 ), | 53 ), |
38 ) | 54 ) |
39 | 55 |
40 err = md.Convert(source, os.Stdout) | 56 var htmlContent bytes.Buffer |
41 | 57 err = md.Convert(source, &htmlContent) |
42 if err != nil { | 58 if err != nil { |
43 log.Fatal(err) | 59 log.Fatal(err) |
44 } | 60 } |
61 | |
62 if useTemplate { | |
63 context := map[string]string{ | |
64 "content": htmlContent.String(), | |
65 } | |
66 err := tmpl.FRender(os.Stdout, context) | |
67 if err != nil { | |
68 log.Fatal(err) | |
69 } | |
70 } else { | |
71 fmt.Print(htmlContent) | |
72 } | |
45 } | 73 } |