2020-09-24 21:21:00 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2022-08-30 21:52:53 +00:00
|
|
|
"html/template"
|
2020-09-24 21:21:00 +00:00
|
|
|
"io/ioutil"
|
2021-12-22 13:12:40 +00:00
|
|
|
"os"
|
2020-09-24 21:21:00 +00:00
|
|
|
|
|
|
|
"github.com/PuerkitoBio/goquery"
|
2022-08-30 21:52:53 +00:00
|
|
|
"github.com/avelino/awesome-go/pkg/markdown"
|
2020-09-24 21:21:00 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func readme() []byte {
|
2022-08-30 21:52:53 +00:00
|
|
|
input, err := os.ReadFile("./README.md")
|
2020-09-24 21:21:00 +00:00
|
|
|
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
|
2020-09-24 21:21:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func startQuery() *goquery.Document {
|
|
|
|
buf := bytes.NewBuffer(readme())
|
|
|
|
query, err := goquery.NewDocumentFromReader(buf)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return query
|
|
|
|
}
|
2021-12-22 13:12:40 +00:00
|
|
|
|
|
|
|
type content struct {
|
2022-08-30 21:52:53 +00:00
|
|
|
Body template.HTML
|
2021-12-22 13:12:40 +00:00
|
|
|
}
|
|
|
|
|
2021-12-27 09:55:03 +00:00
|
|
|
// 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)}
|
2021-12-22 13:12:40 +00:00
|
|
|
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
|
2021-12-22 13:12:40 +00:00
|
|
|
}
|