Skip to content

Commit

Permalink
Simplify template parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
Bios-Marcel committed Feb 24, 2021
1 parent 9a73a9b commit a105e50
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 47 deletions.
45 changes: 6 additions & 39 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func main() {
}
}

baseTemplate, parseError := template.ParseFS(skeletonFS, "skeletons/base.html")
parsedTemplates, parseError := template.ParseFS(skeletonFS, "skeletons/*.html")
if parseError != nil {
panic(parseError)
}
Expand All @@ -92,16 +92,10 @@ func main() {
panic(readError)
}

customPageSkeleton, parseError := template.ParseFS(skeletonFS, "skeletons/page.html")
if parseError != nil {
panic(parseError)
}
addSubTemplates(customPageSkeleton, baseTemplate)

customPageTemplates := make(map[string]*template.Template)
var customPages []*customPageEntry
for _, customPage := range customPageFiles {
customPageSkeletonClone, cloneError := customPageSkeleton.Clone()
customPageSkeletonClone, cloneError := parsedTemplates.Lookup("page").Clone()
if cloneError != nil {
panic(cloneError)
}
Expand All @@ -121,13 +115,6 @@ func main() {
})
}

articleSkeleton, parseError := template.ParseFS(skeletonFS, "skeletons/article.html")
if parseError != nil {
panic(parseError)
}

addSubTemplates(articleSkeleton, baseTemplate)

articles, readError := ioutil.ReadDir(filepath.Join(*input, "articles"))
if readError != nil {
panic(readError)
Expand All @@ -142,7 +129,7 @@ func main() {
continue
}

newArticleSkeleton, cloneError := articleSkeleton.Clone()
newArticleSkeleton, cloneError := parsedTemplates.Lookup("article").Clone()
if cloneError != nil {
panic(cloneError)
}
Expand Down Expand Up @@ -228,14 +215,8 @@ func main() {
}, fileName)
}

indexSkeleton, parseError := template.ParseFS(skeletonFS, "skeletons/index.html")
if parseError != nil {
panic(parseError)
}
addSubTemplates(indexSkeleton, baseTemplate)

//Main Index with all articles.
writeTemplateToFile(indexSkeleton, &indexData{
writeTemplateToFile(parsedTemplates.Lookup("index"), &indexData{
pageConfig: loadedPageConfig,
Tags: tags,
CustomPages: customPages,
Expand All @@ -255,7 +236,7 @@ func main() {
}
}

writeTemplateToFile(indexSkeleton, &indexData{
writeTemplateToFile(parsedTemplates.Lookup("index"), &indexData{
pageConfig: loadedPageConfig,
Tags: tags,
FilterTag: tag,
Expand Down Expand Up @@ -283,12 +264,7 @@ func main() {

copy.Copy(filepath.Join(*input, "media"), filepath.Join(*output, "media"))

notFoundSkeleton, parseError := template.ParseFS(skeletonFS, "skeletons/404.html")
if parseError != nil {
panic(parseError)
}
addSubTemplates(notFoundSkeleton, baseTemplate)
writeTemplateToFile(notFoundSkeleton, &customPageData{
writeTemplateToFile(parsedTemplates.Lookup("404"), &customPageData{
pageConfig: loadedPageConfig,
CustomPages: customPages,
}, "404.html")
Expand Down Expand Up @@ -417,15 +393,6 @@ type indexData struct {
IndexedArticles []*indexedArticle
}

func addSubTemplates(targetTemplate *template.Template, sourceTemplates *template.Template) {
for _, subTemplate := range sourceTemplates.Templates() {
_, addError := targetTemplate.AddParseTree(subTemplate.Name(), subTemplate.Tree)
if addError != nil {
panic(addError)
}
}
}

func templateToString(temp *template.Template) string {
buffer := &bytes.Buffer{}
executionError := temp.Execute(buffer, nil)
Expand Down
4 changes: 2 additions & 2 deletions skeletons/404.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<!DOCTYPE html>
{{define "404"}}<!DOCTYPE html>
<html lang="en-GB">
<head>
{{template "base-header" .}}
Expand All @@ -15,4 +15,4 @@ <h1>404</h1>
{{template "footer" .}}
</footer>
</body>
</html>
</html>{{end}}
4 changes: 2 additions & 2 deletions skeletons/article.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<!DOCTYPE html>
{{define "article"}}<!DOCTYPE html>
<html lang="en-GB">
<head>
{{template "base-header" .}}
Expand Down Expand Up @@ -36,4 +36,4 @@ <h1>{{template "title"}}</h1>
{{template "footer" .}}
</footer>
</body>
</html>
</html>{{end}}
4 changes: 2 additions & 2 deletions skeletons/index.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<!DOCTYPE html>
{{define "index"}}<!DOCTYPE html>
<html lang="en-GB">
<head>
{{template "base-header" .}}
Expand All @@ -25,4 +25,4 @@ <h2>Tags</h2><div class="tag-list">{{$BasePath := .BasePath}}{{range .Tags}}
{{template "footer" .}}
</footer>
</body>
</html>
</html>{{end}}
4 changes: 2 additions & 2 deletions skeletons/page.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<!DOCTYPE html>
{{define "page"}}<!DOCTYPE html>
<html lang="en-GB">
<head>
{{template "base-header" .}}
Expand All @@ -17,4 +17,4 @@ <h1>{{template "title"}}</h1>
{{template "footer" .}}
</footer>
</body>
</html>
</html>{{end}}

0 comments on commit a105e50

Please sign in to comment.