greatly simplity template code
This commit is contained in:
parent
41dca2aaf5
commit
906f398022
53
main.go
53
main.go
|
@ -26,6 +26,8 @@ var inputDir string = filepath.Join(root, "content")
|
|||
var tagFile string = filepath.Join(root, "tags.json")
|
||||
var feedFile string = filepath.Join(root, "feeds.json")
|
||||
|
||||
var templates *t.Template = t.Must(t.ParseGlob(templateDir + "/*"))
|
||||
|
||||
type filePathInfo struct {
|
||||
Path string
|
||||
New bool
|
||||
|
@ -165,7 +167,7 @@ func processPage(f filePathInfo) error {
|
|||
}
|
||||
}
|
||||
}
|
||||
// TODO maybe feeds for html?
|
||||
// TODO feeds for html
|
||||
ls, err := processLinkString(f.Path, GEMINI)
|
||||
if err != nil {
|
||||
return err
|
||||
|
@ -280,12 +282,15 @@ func walkInputDir(path string, info os.FileInfo, err error) error {
|
|||
}
|
||||
|
||||
// writeTagPage writes a tag page.
|
||||
func writeTagPage(tag string, pages *[]pageInfo, t *t.Template, basePath string, linkType int) error {
|
||||
func writeTagPage(tag string, pages *[]pageInfo, basePath string, linkType int) error {
|
||||
var outPath string
|
||||
var tmpl string
|
||||
if linkType == HTML {
|
||||
outPath = filepath.Join(basePath, tag+".html")
|
||||
tmpl = "tag.html"
|
||||
} else {
|
||||
outPath = filepath.Join(basePath, tag+".gmi")
|
||||
tmpl = "tag.gmi"
|
||||
}
|
||||
f, err := os.Create(filepath.Join(outPath))
|
||||
defer f.Close()
|
||||
|
@ -300,7 +305,7 @@ func writeTagPage(tag string, pages *[]pageInfo, t *t.Template, basePath string,
|
|||
return err
|
||||
}
|
||||
}
|
||||
err = t.Execute(f, templateTag{Name: tag, Pages: np})
|
||||
err = templates.ExecuteTemplate(f, tmpl, templateTag{Name: tag, Pages: np})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -342,22 +347,6 @@ func main() {
|
|||
}
|
||||
}
|
||||
feed.Updated = time.Now()
|
||||
htmlTagPageTemplateFile, err := ioutil.ReadFile(filepath.Join(templateDir, "tag.html"))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
htmlTagPageTemplate, err := t.New("htmlTagPage").Parse(string(htmlTagPageTemplateFile))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
geminiTagPageTemplateFile, err := ioutil.ReadFile(filepath.Join(templateDir, "tag.gmi"))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
geminiTagPageTemplate, err := t.New("geminiTagPage").Parse(string(geminiTagPageTemplateFile))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
err = filepath.Walk(inputDir, walkInputDir) // walks the tree, creates the files slice and updates tagData
|
||||
if err != nil {
|
||||
panic(err)
|
||||
|
@ -378,57 +367,41 @@ func main() {
|
|||
panic(err)
|
||||
}
|
||||
}
|
||||
// Write out index page
|
||||
templateFile, err := ioutil.ReadFile(filepath.Join(templateDir, "index.gmi"))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
indexTemplate, err := t.New("gemtextIndex").Parse(string(templateFile))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
f, err := os.Create(filepath.Join(geminiOutputDir, "index.gmi"))
|
||||
defer f.Close()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
err = indexTemplate.Execute(f, tagData)
|
||||
err = templates.ExecuteTemplate(f, "index.gmi", tagData)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
f.Sync()
|
||||
|
||||
// Write out HTML index page
|
||||
templateFile, err = ioutil.ReadFile(filepath.Join(templateDir, "index.html"))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
indexTemplate, err = t.New("htmlIndex").Parse(string(templateFile))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
hf, err := os.Create(filepath.Join(htmlOutputDir, "index.html"))
|
||||
defer hf.Close()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
err = indexTemplate.Execute(hf, tagData)
|
||||
err = templates.ExecuteTemplate(hf, "index.html", tagData)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
hf.Sync()
|
||||
// Write tag pages
|
||||
for tag, pages := range tagData {
|
||||
err = writeTagPage(tag, &pages, htmlTagPageTemplate, filepath.Join(htmlOutputDir, "/_tags/"), HTML)
|
||||
err = writeTagPage(tag, &pages, filepath.Join(htmlOutputDir, "/_tags/"), HTML)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
err = writeTagPage(tag, &pages, geminiTagPageTemplate, filepath.Join(geminiOutputDir, "/_tags/"), GEMINI)
|
||||
err = writeTagPage(tag, &pages, filepath.Join(geminiOutputDir, "/_tags/"), GEMINI)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
// TODO recently edited pages
|
||||
// Write Feeds
|
||||
fe, err := feed.ToAtom()
|
||||
if err != nil {
|
||||
|
|
Reference in New Issue