2020-09-24 21:21:00 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2023-02-14 17:50:14 +00:00
|
|
|
"fmt"
|
2022-08-30 21:52:53 +00:00
|
|
|
"html/template"
|
2021-12-22 13:12:40 +00:00
|
|
|
"os"
|
2020-09-24 21:21:00 +00:00
|
|
|
|
2022-08-30 21:52:53 +00:00
|
|
|
"github.com/avelino/awesome-go/pkg/markdown"
|
2020-09-24 21:21:00 +00:00
|
|
|
)
|
|
|
|
|
2023-02-14 23:35:13 +00:00
|
|
|
// renderIndex generate site html (index.html) from markdown file
|
|
|
|
func renderIndex(srcFilename, outFilename string) error {
|
2023-02-14 17:30:51 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
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 21:59:11 +00:00
|
|
|
data := map[string]interface{}{
|
|
|
|
"Body": template.HTML(body),
|
|
|
|
}
|
|
|
|
if err := tplIndex.Execute(f, data); err != nil {
|
2023-02-04 01:39:51 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-02-14 23:36:18 +00:00
|
|
|
if err := f.Close(); err != nil {
|
|
|
|
return fmt.Errorf("unable to close index file: %w", err)
|
|
|
|
}
|
|
|
|
|
2023-02-04 01:39:51 +00:00
|
|
|
return nil
|
2021-12-22 13:12:40 +00:00
|
|
|
}
|