use template.Must at program starts

This commit is contained in:
Kirill Zhuravlev 2023-02-14 19:12:57 +01:00 committed by Avelino
parent d9aabba637
commit 00bcb01584
No known key found for this signature in database
GPG Key ID: B345B4D52E98180A
3 changed files with 15 additions and 21 deletions

18
main.go
View File

@ -36,10 +36,11 @@ var staticFiles = []string{
"tmpl/robots.txt",
}
// Template files
const tplPath = "tmpl/tmpl.html"
const tmplCategory = "tmpl/cat-tmpl.html"
const tmplSitemap = "tmpl/sitemap-tmpl.xml"
// TODO: embed
// Templates
var tplIndex = template.Must(template.ParseFiles("tmpl/tmpl.html"))
var tplCategoryIndex = template.Must(template.ParseFiles("tmpl/cat-tmpl.html"))
var tplSitemap = template.Must(template.ParseFiles("tmpl/sitemap-tmpl.xml"))
// Output files
const outDir = "out/" // NOTE: trailing slash is required
@ -69,8 +70,7 @@ func main() {
panic(err)
}
buf := bytes.NewBuffer(input)
query, err := goquery.NewDocumentFromReader(buf)
query, err := goquery.NewDocumentFromReader(bytes.NewReader(input))
if err != nil {
panic(err)
}
@ -136,7 +136,6 @@ func makeSiteStruct(objs map[string]*Object) error {
// FIXME: embed templates
// FIXME: parse templates once at start
t := template.Must(template.ParseFiles(tmplCategory))
categoryIndexFilename := filepath.Join(categoryDir, "index.html")
f, err := os.Create(categoryIndexFilename)
if err != nil {
@ -145,7 +144,7 @@ func makeSiteStruct(objs map[string]*Object) error {
fmt.Printf("Write category Index file: %s\n", categoryIndexFilename)
if err := t.Execute(f, obj); err != nil {
if err := tplCategoryIndex.Execute(f, obj); err != nil {
return err
}
}
@ -154,12 +153,11 @@ func makeSiteStruct(objs map[string]*Object) error {
}
func makeSitemap(objs map[string]*Object) {
t := template.Must(template.ParseFiles(tmplSitemap))
// FIXME: handle error
f, _ := os.Create(outSitemapFile)
fmt.Printf("Render Sitemap to: %s\n", outSitemapFile)
_ = t.Execute(f, objs)
_ = tplSitemap.Execute(f, objs)
}
func makeObjByID(selector string, s *goquery.Selection) (obj *Object) {

View File

@ -48,14 +48,13 @@ func GenerateHTML(srcFilename, outFilename string) error {
}
c := &content{Body: template.HTML(body)}
t := template.Must(template.ParseFiles(tplPath))
f, err := os.Create(outFilename)
if err != nil {
return err
}
fmt.Printf("Write Index file: %s\n", outIndexFile)
if err := t.Execute(f, c); err != nil {
if err := tplIndex.Execute(f, c); err != nil {
return err
}

View File

@ -18,12 +18,14 @@ import (
"golang.org/x/oauth2"
)
const issueTemplate = `
const issueTemplateContent = `
{{range .}}
- [ ] {{.}}
{{end}}
`
var issueTemplate = template.Must(template.New("issue").Parse(issueTemplateContent))
var reGithubRepo = regexp.MustCompile("https://github.com/[a-zA-Z0-9-._]+/[a-zA-Z0-9-._]+$")
var githubGETREPO = "https://api.github.com/repos%s"
var githubGETCOMMITS = "https://api.github.com/repos%s/commits"
@ -38,7 +40,7 @@ const movedPermanently = " status code 301 received"
const status302 = " status code 302 received"
const archived = " repository has been archived"
//LIMIT specifies the max number of repositories that are added in a single run of the script
// LIMIT specifies the max number of repositories that are added in a single run of the script
var LIMIT = 10
var ctr = 0
@ -77,13 +79,8 @@ func getRepositoriesFromBody(body string) []string {
}
func generateIssueBody(repositories []string) (string, error) {
var writer bytes.Buffer
t := template.New("issue")
temp, err := t.Parse(issueTemplate)
if err != nil {
log.Print("Failed to generate template")
return "", err
}
err = temp.Execute(&writer, repositories)
err := issueTemplate.Execute(&writer, repositories)
if err != nil {
log.Print("Failed to generate template")
return "", err