awesome-go/scripts.go

63 lines
1.1 KiB
Go
Raw Normal View History

package main
import (
"bytes"
2022-08-30 21:52:53 +00:00
"html/template"
"io/ioutil"
"os"
"github.com/PuerkitoBio/goquery"
2022-08-30 21:52:53 +00:00
"github.com/avelino/awesome-go/pkg/markdown"
)
func readme() []byte {
2022-08-30 21:52:53 +00:00
input, err := os.ReadFile("./README.md")
if err != nil {
panic(err)
}
2022-08-30 21:52:53 +00:00
html, err := markdown.ConvertMarkdownToHTML(input)
if err != nil {
panic(err)
}
return html
}
func startQuery() *goquery.Document {
buf := bytes.NewBuffer(readme())
query, err := goquery.NewDocumentFromReader(buf)
if err != nil {
panic(err)
}
return query
}
type content struct {
2022-08-30 21:52:53 +00:00
Body template.HTML
}
// GenerateHTML generate site html (index.html) from markdown file
2023-02-04 01:39:51 +00:00
func GenerateHTML(srcFilename, outFilename string) error {
input, err := ioutil.ReadFile(srcFilename)
if err != nil {
return err
}
body, err := markdown.ConvertMarkdownToHTML(input)
if err != nil {
return err
}
2022-08-30 21:52:53 +00:00
c := &content{Body: template.HTML(body)}
t := template.Must(template.ParseFiles(tplPath))
2023-02-04 01:39:51 +00:00
f, err := os.Create(outFilename)
if err != nil {
return err
}
if err := t.Execute(f, c); err != nil {
return err
}
return nil
}