-
Notifications
You must be signed in to change notification settings - Fork 1
/
scripts.go
55 lines (48 loc) · 1.2 KB
/
scripts.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
package main
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"text/template"
"github.com/PuerkitoBio/goquery"
"github.com/gomarkdown/markdown"
"github.com/gomarkdown/markdown/parser"
"github.com/russross/blackfriday"
)
func readme() []byte {
input, err := ioutil.ReadFile("./README.md")
if err != nil {
panic(err)
}
html := fmt.Sprintf("<body>%s</body>", blackfriday.MarkdownCommon(input))
htmlByteArray := []byte(html)
return htmlByteArray
}
func startQuery() *goquery.Document {
buf := bytes.NewBuffer(readme())
query, err := goquery.NewDocumentFromReader(buf)
if err != nil {
panic(err)
}
return query
}
type content struct {
Body string
}
// GenerateHTML generate site html (index.html) from markdown file
func GenerateHTML() (err error) {
// options
readmePath := "./README.md"
tplPath := "tmpl/tmpl.html"
idxPath := "tmpl/index.html"
input, _ := ioutil.ReadFile(readmePath)
extensions := parser.CommonExtensions | parser.AutoHeadingIDs | parser.LaxHTMLBlocks
parser := parser.NewWithExtensions(extensions)
body := string(markdown.ToHTML(input, parser, nil))
c := &content{Body: body}
t := template.Must(template.ParseFiles(tplPath))
f, err := os.Create(idxPath)
t.Execute(f, c)
return
}