-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
152 lines (130 loc) · 3.8 KB
/
main.go
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
package main
import (
"flag"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"os"
"path"
"github.com/golang-commonmark/markdown"
)
var cssUrlList ArrayFlags
var cssFileList ArrayFlags
func main() {
binPath := flag.String("bin", "/usr/local/bin/wkhtmltoimage", "wkhtmltoimage bin path")
markdownPath := flag.String("m", "", "markdown file path")
outputPath := flag.String("o", "", "output file path (default same as markdown file name)")
htmlFile := flag.String("html", "", "file path of HTML content. CSS relative options will ignore if include-css flag is not true")
width := flag.Int("w", 960, "output image width")
quality := flag.Int("q", 80, "output image quality, maxium is 100")
flag.Var(&cssUrlList, "cssurl", "CSS URLs [repeatable, optional]")
flag.Var(&cssFileList, "cssfile", "CSS file path, support any style you like❤️ , include fonts! [repeatable, optional]")
cssName := flag.String("cssname", "", "use builtin CSS from github.com/mixu/markdown-styles:"+cssListHelpText)
//staticPath := flag.String("static", ".", "static files path")
print := flag.Bool("print", false, "print generated html")
includeCSS := flag.Bool("include-css", false, "include css file as html header when read content from *.html file")
url := flag.String("url", "", "capture screenshot of the url")
flag.Parse()
html := ""
if *url != "" {
html = GetHTMLFromURL(*url)
} else {
if *cssName != "" {
cssUrlList = append(cssUrlList, getCssUrl(*cssName))
}
if *outputPath == "" {
*outputPath = ReplaceExt(path.Base(*markdownPath), "png")
}
//prepare static files
//go staticServer(*staticPath)
header := ""
if *htmlFile == "" || *includeCSS {
header = `<meta http-equiv="content-Type" content="text/html; charset=UTF-8" />`
for _, f := range cssFileList {
header += renderCssPath(f)
}
for _, u := range cssUrlList {
header += renderCssUrl(u)
}
}
if *htmlFile == "" {
md := ReadFile(*markdownPath)
html = fmt.Sprintf("%v\n\n<div class='main container content article'>\n%v\n</div>", header, markdown2html(md))
} else {
if *includeCSS {
html = header + ReadFile(*htmlFile)
} else {
html = ReadFile(*htmlFile)
}
}
}
// renderHTML
if *print {
fmt.Println(html)
}
imgRender := ImageRender{BinaryPath: binPath}
imgRender.generateImage(html, "png", *outputPath, *width, *quality)
}
type ImageRender struct {
BinaryPath *string
}
func markdown2html(text string) string {
// return string(github_flavored_markdown.Markdown([]byte(markdown)))
md := markdown.New(markdown.XHTMLOutput(true), markdown.Nofollow(true))
return md.RenderToString([]byte(text))
}
func GetHTMLFromURL(URL string) string {
u, err := url.Parse(URL)
if err != nil {
panic(err)
}
baseUrl := path.Dir(u.Path)
if baseUrl != "." && baseUrl != "" {
u.Path = baseUrl
}
base := u.String()
rv := ""
response, err := http.Get(URL)
if err != nil {
fmt.Printf("%s", err)
os.Exit(1)
} else {
defer response.Body.Close()
contents, err := ioutil.ReadAll(response.Body)
if err != nil {
fmt.Printf("%s", err)
os.Exit(1)
}
rv += fmt.Sprintf("<head><base href=\"%v\"></head>", base)
rv += string(contents)
}
return rv
}
func (r *ImageRender) generateImage(html, format, output string, width, quality int) []byte {
c := ImageOptions{
BinaryPath: *r.BinaryPath,
Input: "-",
Html: html,
Format: format,
Width: width,
Quality: quality,
Output: output,
}
out, err := GenerateImage(&c)
fatalErr(err)
return out
}
// builtin css
var cssListHelpText = `
jasonm23-dark
jasonm23-foghorn
jasonm23-markdown
jasonm23-swiss
markedapp-byword
thomasf-solarizedcssdark
thomasf-solarizedcsslight
`
func getCssUrl(name string) string {
return fmt.Sprintf("https://raw.githubusercontent.com/mixu/markdown-styles/master/output/%v/assets/style.css", name)
}