mirror of
https://github.com/avelino/awesome-go.git
synced 2024-11-07 16:33:40 +00:00
use template.Must at program starts
This commit is contained in:
parent
d9aabba637
commit
00bcb01584
18
main.go
18
main.go
@ -36,10 +36,11 @@ var staticFiles = []string{
|
|||||||
"tmpl/robots.txt",
|
"tmpl/robots.txt",
|
||||||
}
|
}
|
||||||
|
|
||||||
// Template files
|
// TODO: embed
|
||||||
const tplPath = "tmpl/tmpl.html"
|
// Templates
|
||||||
const tmplCategory = "tmpl/cat-tmpl.html"
|
var tplIndex = template.Must(template.ParseFiles("tmpl/tmpl.html"))
|
||||||
const tmplSitemap = "tmpl/sitemap-tmpl.xml"
|
var tplCategoryIndex = template.Must(template.ParseFiles("tmpl/cat-tmpl.html"))
|
||||||
|
var tplSitemap = template.Must(template.ParseFiles("tmpl/sitemap-tmpl.xml"))
|
||||||
|
|
||||||
// Output files
|
// Output files
|
||||||
const outDir = "out/" // NOTE: trailing slash is required
|
const outDir = "out/" // NOTE: trailing slash is required
|
||||||
@ -69,8 +70,7 @@ func main() {
|
|||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
buf := bytes.NewBuffer(input)
|
query, err := goquery.NewDocumentFromReader(bytes.NewReader(input))
|
||||||
query, err := goquery.NewDocumentFromReader(buf)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
@ -136,7 +136,6 @@ func makeSiteStruct(objs map[string]*Object) error {
|
|||||||
|
|
||||||
// FIXME: embed templates
|
// FIXME: embed templates
|
||||||
// FIXME: parse templates once at start
|
// FIXME: parse templates once at start
|
||||||
t := template.Must(template.ParseFiles(tmplCategory))
|
|
||||||
categoryIndexFilename := filepath.Join(categoryDir, "index.html")
|
categoryIndexFilename := filepath.Join(categoryDir, "index.html")
|
||||||
f, err := os.Create(categoryIndexFilename)
|
f, err := os.Create(categoryIndexFilename)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -145,7 +144,7 @@ func makeSiteStruct(objs map[string]*Object) error {
|
|||||||
|
|
||||||
fmt.Printf("Write category Index file: %s\n", categoryIndexFilename)
|
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
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -154,12 +153,11 @@ func makeSiteStruct(objs map[string]*Object) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func makeSitemap(objs map[string]*Object) {
|
func makeSitemap(objs map[string]*Object) {
|
||||||
t := template.Must(template.ParseFiles(tmplSitemap))
|
|
||||||
// FIXME: handle error
|
// FIXME: handle error
|
||||||
f, _ := os.Create(outSitemapFile)
|
f, _ := os.Create(outSitemapFile)
|
||||||
fmt.Printf("Render Sitemap to: %s\n", 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) {
|
func makeObjByID(selector string, s *goquery.Selection) (obj *Object) {
|
||||||
|
@ -48,14 +48,13 @@ func GenerateHTML(srcFilename, outFilename string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
c := &content{Body: template.HTML(body)}
|
c := &content{Body: template.HTML(body)}
|
||||||
t := template.Must(template.ParseFiles(tplPath))
|
|
||||||
f, err := os.Create(outFilename)
|
f, err := os.Create(outFilename)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Printf("Write Index file: %s\n", outIndexFile)
|
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
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -18,12 +18,14 @@ import (
|
|||||||
"golang.org/x/oauth2"
|
"golang.org/x/oauth2"
|
||||||
)
|
)
|
||||||
|
|
||||||
const issueTemplate = `
|
const issueTemplateContent = `
|
||||||
{{range .}}
|
{{range .}}
|
||||||
- [ ] {{.}}
|
- [ ] {{.}}
|
||||||
{{end}}
|
{{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 reGithubRepo = regexp.MustCompile("https://github.com/[a-zA-Z0-9-._]+/[a-zA-Z0-9-._]+$")
|
||||||
var githubGETREPO = "https://api.github.com/repos%s"
|
var githubGETREPO = "https://api.github.com/repos%s"
|
||||||
var githubGETCOMMITS = "https://api.github.com/repos%s/commits"
|
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 status302 = " status code 302 received"
|
||||||
const archived = " repository has been archived"
|
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 LIMIT = 10
|
||||||
var ctr = 0
|
var ctr = 0
|
||||||
|
|
||||||
@ -77,13 +79,8 @@ func getRepositoriesFromBody(body string) []string {
|
|||||||
}
|
}
|
||||||
func generateIssueBody(repositories []string) (string, error) {
|
func generateIssueBody(repositories []string) (string, error) {
|
||||||
var writer bytes.Buffer
|
var writer bytes.Buffer
|
||||||
t := template.New("issue")
|
|
||||||
temp, err := t.Parse(issueTemplate)
|
err := issueTemplate.Execute(&writer, repositories)
|
||||||
if err != nil {
|
|
||||||
log.Print("Failed to generate template")
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
err = temp.Execute(&writer, repositories)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Print("Failed to generate template")
|
log.Print("Failed to generate template")
|
||||||
return "", err
|
return "", err
|
||||||
|
Loading…
Reference in New Issue
Block a user