-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrender_github_markdown.go
More file actions
166 lines (149 loc) · 3.28 KB
/
render_github_markdown.go
File metadata and controls
166 lines (149 loc) · 3.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package main
import (
"bytes"
"encoding/json"
"errors"
"flag"
"fmt"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"strings"
"text/template"
)
const (
required = "REQUIRED"
optional = "OPTIONAL"
web = "web"
pdf = "pdf"
)
func main() {
var inpath = flag.String("in", required, "path to an input markdown file")
var outpath = flag.String("out", optional, "path to an output html file")
var title = flag.String("title", optional, "title for the generated html doc")
var formatFor = flag.String("format-for", web, fmt.Sprintf("will the rendered markdown be used for %s or %s?", web, pdf))
flag.Parse()
if *inpath == "" || *inpath == required {
checkErr(errors.New("in is required"))
}
if *outpath == "" || *outpath == optional {
*outpath = fileName(*inpath) + ".html"
}
if *title == "" || *title == optional {
*title = fileName(*inpath)
}
if *formatFor != web && *formatFor != pdf {
checkErr(fmt.Errorf("type must be %s or %s", web, pdf))
}
markdown, err := ioutil.ReadFile(*inpath)
checkErr(err)
rendered, err := renderMarkdown(markdown)
checkErr(err)
var builder docBuilder
switch *formatFor {
case web:
builder = newWebDoc(*title, string(rendered))
case pdf:
builder = newPDFDoc(*title, string(rendered))
}
generated, err := builder.buildDoc()
checkErr(err)
if *outpath == optional {
fmt.Println(generated)
} else {
outfile, err := os.Create(*outpath)
checkErr(err)
_, err = outfile.WriteString(generated)
checkErr(err)
}
}
func renderMarkdown(markdown []byte) ([]byte, error) {
body := map[string]string{
"text": string(markdown),
}
bodyJSON, err := json.Marshal(body)
if err != nil {
fmt.Println(err)
}
resp, err := http.Post(
"https://api.github.com/markdown",
"application/json",
bytes.NewReader(bodyJSON),
)
if err != nil {
fmt.Println(err)
}
defer resp.Body.Close()
return ioutil.ReadAll(resp.Body)
}
func fileName(path string) string {
return strings.TrimSuffix(filepath.Base(path), filepath.Ext(path))
}
func checkErr(err error) {
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}
type docBuilder struct {
Title string
FontSize string
LineHeight string
Body string
}
func (builder docBuilder) buildDoc() (string, error) {
docTempl, err := template.New("doc").Parse(docTemplate)
if err != nil {
return "", err
}
var docBuf bytes.Buffer
err = docTempl.Execute(&docBuf, builder)
if err != nil {
return "", err
}
return docBuf.String(), nil
}
func newWebDoc(title string, body string) docBuilder {
return docBuilder{
title,
"1.1em",
"1.5em",
body,
}
}
func newPDFDoc(title string, body string) docBuilder {
return docBuilder{
title,
"0.9em",
"1.3em",
body,
}
}
var docTemplate = `
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="viewport" content="width=device-width">
<title>{{.Title}}</title>
<style type="text/css">
body {
font-size: {{.FontSize}};
line-height: {{.LineHeight}};
max-width: 45em;
margin: auto;
padding: 0 2%;
font-family: system-ui;
}
img {
max-width: 100%;
margin-left: 0.5em;
vertical-align: -0.3em;
}
</style>
</head>
<body>
{{.Body}}
</body>
`