2020-09-24 17:21:00 -04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2022-08-30 17:52:53 -04:00
|
|
|
"html/template"
|
2020-09-24 17:21:00 -04:00
|
|
|
"io/ioutil"
|
2021-12-22 08:12:40 -05:00
|
|
|
"os"
|
2020-09-24 17:21:00 -04:00
|
|
|
|
|
|
|
"github.com/PuerkitoBio/goquery"
|
2022-08-30 17:52:53 -04:00
|
|
|
"github.com/avelino/awesome-go/pkg/markdown"
|
2020-09-24 17:21:00 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func readme() []byte {
|
2022-08-30 17:52:53 -04:00
|
|
|
input, err := os.ReadFile("./README.md")
|
2020-09-24 17:21:00 -04:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2022-08-30 17:52:53 -04:00
|
|
|
html, err := markdown.ConvertMarkdownToHTML(input)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return html
|
2020-09-24 17:21:00 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func startQuery() *goquery.Document {
|
|
|
|
buf := bytes.NewBuffer(readme())
|
|
|
|
query, err := goquery.NewDocumentFromReader(buf)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return query
|
|
|
|
}
|
2021-12-22 08:12:40 -05:00
|
|
|
|
|
|
|
type content struct {
|
2022-08-30 17:52:53 -04:00
|
|
|
Body template.HTML
|
2021-12-22 08:12:40 -05:00
|
|
|
}
|
|
|
|
|
2021-12-27 04:55:03 -05:00
|
|
|
// GenerateHTML generate site html (index.html) from markdown file
|
2023-02-03 20:39:51 -05: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 17:52:53 -04:00
|
|
|
c := &content{Body: template.HTML(body)}
|
2021-12-22 08:12:40 -05:00
|
|
|
t := template.Must(template.ParseFiles(tplPath))
|
2023-02-03 20:39:51 -05:00
|
|
|
f, err := os.Create(outFilename)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := t.Execute(f, c); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2021-12-22 08:12:40 -05:00
|
|
|
}
|