awesome-go/scripts.go

63 lines
1.0 KiB
Go
Raw Normal View History

package main
import (
"bytes"
2023-02-14 17:50:14 +00:00
"fmt"
2022-08-30 21:52:53 +00:00
"html/template"
"os"
"github.com/PuerkitoBio/goquery"
2022-08-30 21:52:53 +00:00
"github.com/avelino/awesome-go/pkg/markdown"
)
func readme() []byte {
input, err := os.ReadFile(readmePath)
if err != nil {
panic(err)
}
2023-02-14 20:26:32 +00:00
html, err := markdown.ToHTML(input)
2022-08-30 21:52:53 +00:00
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 := os.ReadFile(srcFilename)
2023-02-04 01:39:51 +00:00
if err != nil {
return err
}
2023-02-14 20:26:32 +00:00
body, err := markdown.ToHTML(input)
2023-02-04 01:39:51 +00:00
if err != nil {
return err
}
2022-08-30 21:52:53 +00:00
c := &content{Body: template.HTML(body)}
2023-02-04 01:39:51 +00:00
f, err := os.Create(outFilename)
if err != nil {
return err
}
2023-02-14 17:50:14 +00:00
fmt.Printf("Write Index file: %s\n", outIndexFile)
2023-02-14 18:12:57 +00:00
if err := tplIndex.Execute(f, c); err != nil {
2023-02-04 01:39:51 +00:00
return err
}
return nil
}