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