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