Mercurial > repos > public > wdown
annotate wdown.go @ 22:1581b64a72a1 default tip
Move todos to separate file
author | Jonatan Werpers <jonatan@werpers.com> |
---|---|
date | Wed, 01 Jul 2020 11:47:18 +0200 |
parents | adc92b0118f4 |
children |
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" |
10
1ffe6e4f933c
Add math support through MathJax
Jonatan Werpers <jonatan@werpers.com>
parents:
7
diff
changeset
|
12 mathjax "github.com/litao91/goldmark-mathjax" |
0 | 13 "github.com/yuin/goldmark" |
4
0b747e2d51c9
Add syntax highlighting support
Jonatan Werpers <jonatan@werpers.com>
parents:
1
diff
changeset
|
14 highlighting "github.com/yuin/goldmark-highlighting" |
7
a5aa39557726
Add parsing of YAML frontmatter
Jonatan Werpers <jonatan@werpers.com>
parents:
6
diff
changeset
|
15 meta "github.com/yuin/goldmark-meta" |
1
adf9c2b90279
Enable the gfm extension
Jonatan Werpers <jonatan@werpers.com>
parents:
0
diff
changeset
|
16 "github.com/yuin/goldmark/extension" |
adf9c2b90279
Enable the gfm extension
Jonatan Werpers <jonatan@werpers.com>
parents:
0
diff
changeset
|
17 "github.com/yuin/goldmark/parser" |
0 | 18 ) |
19 | |
20 func main() { | |
6
a5f397e25cb7
Add ability to render the markdown inside a template
Jonatan Werpers <jonatan@werpers.com>
parents:
5
diff
changeset
|
21 var templateFilename string |
a5f397e25cb7
Add ability to render the markdown inside a template
Jonatan Werpers <jonatan@werpers.com>
parents:
5
diff
changeset
|
22 |
a5f397e25cb7
Add ability to render the markdown inside a template
Jonatan Werpers <jonatan@werpers.com>
parents:
5
diff
changeset
|
23 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
|
24 |
5
534fa4c6668e
Add command line argument parsing
Jonatan Werpers <jonatan@werpers.com>
parents:
4
diff
changeset
|
25 flag.Parse() |
534fa4c6668e
Add command line argument parsing
Jonatan Werpers <jonatan@werpers.com>
parents:
4
diff
changeset
|
26 |
534fa4c6668e
Add command line argument parsing
Jonatan Werpers <jonatan@werpers.com>
parents:
4
diff
changeset
|
27 if flag.NArg() < 1 { |
534fa4c6668e
Add command line argument parsing
Jonatan Werpers <jonatan@werpers.com>
parents:
4
diff
changeset
|
28 fmt.Println("Please provide a markdown file for parsing.") |
534fa4c6668e
Add command line argument parsing
Jonatan Werpers <jonatan@werpers.com>
parents:
4
diff
changeset
|
29 os.Exit(1) |
534fa4c6668e
Add command line argument parsing
Jonatan Werpers <jonatan@werpers.com>
parents:
4
diff
changeset
|
30 } |
534fa4c6668e
Add command line argument parsing
Jonatan Werpers <jonatan@werpers.com>
parents:
4
diff
changeset
|
31 |
6
a5f397e25cb7
Add ability to render the markdown inside a template
Jonatan Werpers <jonatan@werpers.com>
parents:
5
diff
changeset
|
32 var useTemplate = false |
a5f397e25cb7
Add ability to render the markdown inside a template
Jonatan Werpers <jonatan@werpers.com>
parents:
5
diff
changeset
|
33 if templateFilename != "" { |
a5f397e25cb7
Add ability to render the markdown inside a template
Jonatan Werpers <jonatan@werpers.com>
parents:
5
diff
changeset
|
34 useTemplate = true |
a5f397e25cb7
Add ability to render the markdown inside a template
Jonatan Werpers <jonatan@werpers.com>
parents:
5
diff
changeset
|
35 } |
a5f397e25cb7
Add ability to render the markdown inside a template
Jonatan Werpers <jonatan@werpers.com>
parents:
5
diff
changeset
|
36 |
5
534fa4c6668e
Add command line argument parsing
Jonatan Werpers <jonatan@werpers.com>
parents:
4
diff
changeset
|
37 source, err := ioutil.ReadFile(flag.Arg(0)) |
0 | 38 if err != nil { |
39 log.Fatal(err) | |
40 } | |
41 | |
1
adf9c2b90279
Enable the gfm extension
Jonatan Werpers <jonatan@werpers.com>
parents:
0
diff
changeset
|
42 md := goldmark.New( |
4
0b747e2d51c9
Add syntax highlighting support
Jonatan Werpers <jonatan@werpers.com>
parents:
1
diff
changeset
|
43 goldmark.WithExtensions( |
0b747e2d51c9
Add syntax highlighting support
Jonatan Werpers <jonatan@werpers.com>
parents:
1
diff
changeset
|
44 extension.GFM, |
0b747e2d51c9
Add syntax highlighting support
Jonatan Werpers <jonatan@werpers.com>
parents:
1
diff
changeset
|
45 highlighting.Highlighting, |
7
a5aa39557726
Add parsing of YAML frontmatter
Jonatan Werpers <jonatan@werpers.com>
parents:
6
diff
changeset
|
46 meta.Meta, |
10
1ffe6e4f933c
Add math support through MathJax
Jonatan Werpers <jonatan@werpers.com>
parents:
7
diff
changeset
|
47 mathjax.MathJax, |
4
0b747e2d51c9
Add syntax highlighting support
Jonatan Werpers <jonatan@werpers.com>
parents:
1
diff
changeset
|
48 ), |
1
adf9c2b90279
Enable the gfm extension
Jonatan Werpers <jonatan@werpers.com>
parents:
0
diff
changeset
|
49 goldmark.WithParserOptions( |
adf9c2b90279
Enable the gfm extension
Jonatan Werpers <jonatan@werpers.com>
parents:
0
diff
changeset
|
50 parser.WithAutoHeadingID(), |
adf9c2b90279
Enable the gfm extension
Jonatan Werpers <jonatan@werpers.com>
parents:
0
diff
changeset
|
51 ), |
adf9c2b90279
Enable the gfm extension
Jonatan Werpers <jonatan@werpers.com>
parents:
0
diff
changeset
|
52 ) |
adf9c2b90279
Enable the gfm extension
Jonatan Werpers <jonatan@werpers.com>
parents:
0
diff
changeset
|
53 |
18
adc92b0118f4
Fix crash when not providing a template
Jonatan Werpers <jonatan@werpers.com>
parents:
15
diff
changeset
|
54 htmlContent := new(bytes.Buffer) |
7
a5aa39557726
Add parsing of YAML frontmatter
Jonatan Werpers <jonatan@werpers.com>
parents:
6
diff
changeset
|
55 parserContext := parser.NewContext() |
18
adc92b0118f4
Fix crash when not providing a template
Jonatan Werpers <jonatan@werpers.com>
parents:
15
diff
changeset
|
56 err = md.Convert(source, htmlContent, parser.WithContext(parserContext)) |
0 | 57 if err != nil { |
58 log.Fatal(err) | |
59 } | |
6
a5f397e25cb7
Add ability to render the markdown inside a template
Jonatan Werpers <jonatan@werpers.com>
parents:
5
diff
changeset
|
60 |
a5f397e25cb7
Add ability to render the markdown inside a template
Jonatan Werpers <jonatan@werpers.com>
parents:
5
diff
changeset
|
61 if useTemplate { |
18
adc92b0118f4
Fix crash when not providing a template
Jonatan Werpers <jonatan@werpers.com>
parents:
15
diff
changeset
|
62 tmpl, err := mustache.ParseFile(templateFilename) |
adc92b0118f4
Fix crash when not providing a template
Jonatan Werpers <jonatan@werpers.com>
parents:
15
diff
changeset
|
63 if err != nil { |
adc92b0118f4
Fix crash when not providing a template
Jonatan Werpers <jonatan@werpers.com>
parents:
15
diff
changeset
|
64 log.Fatal(err) |
adc92b0118f4
Fix crash when not providing a template
Jonatan Werpers <jonatan@werpers.com>
parents:
15
diff
changeset
|
65 } |
7
a5aa39557726
Add parsing of YAML frontmatter
Jonatan Werpers <jonatan@werpers.com>
parents:
6
diff
changeset
|
66 templateVars := meta.Get(parserContext) |
15
ebecd141e9d3
Fix crash when there is no YAML
Jonatan Werpers <jonatan@werpers.com>
parents:
10
diff
changeset
|
67 if templateVars == nil { |
ebecd141e9d3
Fix crash when there is no YAML
Jonatan Werpers <jonatan@werpers.com>
parents:
10
diff
changeset
|
68 templateVars = map[string]interface{}{} |
ebecd141e9d3
Fix crash when there is no YAML
Jonatan Werpers <jonatan@werpers.com>
parents:
10
diff
changeset
|
69 } |
7
a5aa39557726
Add parsing of YAML frontmatter
Jonatan Werpers <jonatan@werpers.com>
parents:
6
diff
changeset
|
70 templateVars["content"] = htmlContent.String() |
a5aa39557726
Add parsing of YAML frontmatter
Jonatan Werpers <jonatan@werpers.com>
parents:
6
diff
changeset
|
71 |
18
adc92b0118f4
Fix crash when not providing a template
Jonatan Werpers <jonatan@werpers.com>
parents:
15
diff
changeset
|
72 err = tmpl.FRender(os.Stdout, templateVars) |
6
a5f397e25cb7
Add ability to render the markdown inside a template
Jonatan Werpers <jonatan@werpers.com>
parents:
5
diff
changeset
|
73 if err != nil { |
a5f397e25cb7
Add ability to render the markdown inside a template
Jonatan Werpers <jonatan@werpers.com>
parents:
5
diff
changeset
|
74 log.Fatal(err) |
a5f397e25cb7
Add ability to render the markdown inside a template
Jonatan Werpers <jonatan@werpers.com>
parents:
5
diff
changeset
|
75 } |
a5f397e25cb7
Add ability to render the markdown inside a template
Jonatan Werpers <jonatan@werpers.com>
parents:
5
diff
changeset
|
76 } else { |
a5f397e25cb7
Add ability to render the markdown inside a template
Jonatan Werpers <jonatan@werpers.com>
parents:
5
diff
changeset
|
77 fmt.Print(htmlContent) |
a5f397e25cb7
Add ability to render the markdown inside a template
Jonatan Werpers <jonatan@werpers.com>
parents:
5
diff
changeset
|
78 } |
0 | 79 } |