Mercurial > repos > public > wdown
annotate wdown.go @ 12:bfffb69f55aa
Add forgotten abcjs file
author | Jonatan Werpers <jonatan@werpers.com> |
---|---|
date | Mon, 22 Jun 2020 07:50:43 +0200 |
parents | 1ffe6e4f933c |
children | ebecd141e9d3 |
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 |
a5f397e25cb7
Add ability to render the markdown inside a template
Jonatan Werpers <jonatan@werpers.com>
parents:
5
diff
changeset
|
37 tmpl, err := mustache.ParseFile(templateFilename) |
a5f397e25cb7
Add ability to render the markdown inside a template
Jonatan Werpers <jonatan@werpers.com>
parents:
5
diff
changeset
|
38 if err != nil { |
a5f397e25cb7
Add ability to render the markdown inside a template
Jonatan Werpers <jonatan@werpers.com>
parents:
5
diff
changeset
|
39 log.Fatal(err) |
a5f397e25cb7
Add ability to render the markdown inside a template
Jonatan Werpers <jonatan@werpers.com>
parents:
5
diff
changeset
|
40 } |
a5f397e25cb7
Add ability to render the markdown inside a template
Jonatan Werpers <jonatan@werpers.com>
parents:
5
diff
changeset
|
41 |
5
534fa4c6668e
Add command line argument parsing
Jonatan Werpers <jonatan@werpers.com>
parents:
4
diff
changeset
|
42 source, err := ioutil.ReadFile(flag.Arg(0)) |
0 | 43 |
44 if err != nil { | |
45 log.Fatal(err) | |
46 } | |
47 | |
1
adf9c2b90279
Enable the gfm extension
Jonatan Werpers <jonatan@werpers.com>
parents:
0
diff
changeset
|
48 md := goldmark.New( |
4
0b747e2d51c9
Add syntax highlighting support
Jonatan Werpers <jonatan@werpers.com>
parents:
1
diff
changeset
|
49 goldmark.WithExtensions( |
0b747e2d51c9
Add syntax highlighting support
Jonatan Werpers <jonatan@werpers.com>
parents:
1
diff
changeset
|
50 extension.GFM, |
0b747e2d51c9
Add syntax highlighting support
Jonatan Werpers <jonatan@werpers.com>
parents:
1
diff
changeset
|
51 highlighting.Highlighting, |
7
a5aa39557726
Add parsing of YAML frontmatter
Jonatan Werpers <jonatan@werpers.com>
parents:
6
diff
changeset
|
52 meta.Meta, |
10
1ffe6e4f933c
Add math support through MathJax
Jonatan Werpers <jonatan@werpers.com>
parents:
7
diff
changeset
|
53 mathjax.MathJax, |
4
0b747e2d51c9
Add syntax highlighting support
Jonatan Werpers <jonatan@werpers.com>
parents:
1
diff
changeset
|
54 ), |
1
adf9c2b90279
Enable the gfm extension
Jonatan Werpers <jonatan@werpers.com>
parents:
0
diff
changeset
|
55 goldmark.WithParserOptions( |
adf9c2b90279
Enable the gfm extension
Jonatan Werpers <jonatan@werpers.com>
parents:
0
diff
changeset
|
56 parser.WithAutoHeadingID(), |
adf9c2b90279
Enable the gfm extension
Jonatan Werpers <jonatan@werpers.com>
parents:
0
diff
changeset
|
57 ), |
adf9c2b90279
Enable the gfm extension
Jonatan Werpers <jonatan@werpers.com>
parents:
0
diff
changeset
|
58 ) |
adf9c2b90279
Enable the gfm extension
Jonatan Werpers <jonatan@werpers.com>
parents:
0
diff
changeset
|
59 |
6
a5f397e25cb7
Add ability to render the markdown inside a template
Jonatan Werpers <jonatan@werpers.com>
parents:
5
diff
changeset
|
60 var htmlContent bytes.Buffer |
7
a5aa39557726
Add parsing of YAML frontmatter
Jonatan Werpers <jonatan@werpers.com>
parents:
6
diff
changeset
|
61 parserContext := parser.NewContext() |
a5aa39557726
Add parsing of YAML frontmatter
Jonatan Werpers <jonatan@werpers.com>
parents:
6
diff
changeset
|
62 err = md.Convert(source, &htmlContent, parser.WithContext(parserContext)) |
0 | 63 if err != nil { |
64 log.Fatal(err) | |
65 } | |
6
a5f397e25cb7
Add ability to render the markdown inside a template
Jonatan Werpers <jonatan@werpers.com>
parents:
5
diff
changeset
|
66 |
a5f397e25cb7
Add ability to render the markdown inside a template
Jonatan Werpers <jonatan@werpers.com>
parents:
5
diff
changeset
|
67 if useTemplate { |
7
a5aa39557726
Add parsing of YAML frontmatter
Jonatan Werpers <jonatan@werpers.com>
parents:
6
diff
changeset
|
68 templateVars := meta.Get(parserContext) |
a5aa39557726
Add parsing of YAML frontmatter
Jonatan Werpers <jonatan@werpers.com>
parents:
6
diff
changeset
|
69 templateVars["content"] = htmlContent.String() |
a5aa39557726
Add parsing of YAML frontmatter
Jonatan Werpers <jonatan@werpers.com>
parents:
6
diff
changeset
|
70 |
a5aa39557726
Add parsing of YAML frontmatter
Jonatan Werpers <jonatan@werpers.com>
parents:
6
diff
changeset
|
71 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
|
72 if err != nil { |
a5f397e25cb7
Add ability to render the markdown inside a template
Jonatan Werpers <jonatan@werpers.com>
parents:
5
diff
changeset
|
73 log.Fatal(err) |
a5f397e25cb7
Add ability to render the markdown inside a template
Jonatan Werpers <jonatan@werpers.com>
parents:
5
diff
changeset
|
74 } |
a5f397e25cb7
Add ability to render the markdown inside a template
Jonatan Werpers <jonatan@werpers.com>
parents:
5
diff
changeset
|
75 } else { |
a5f397e25cb7
Add ability to render the markdown inside a template
Jonatan Werpers <jonatan@werpers.com>
parents:
5
diff
changeset
|
76 fmt.Print(htmlContent) |
a5f397e25cb7
Add ability to render the markdown inside a template
Jonatan Werpers <jonatan@werpers.com>
parents:
5
diff
changeset
|
77 } |
0 | 78 } |