Skip to content

Commit

Permalink
Use embed.FS for templates (#5303)
Browse files Browse the repository at this point in the history
Co-authored-by: sourcery-ai[bot] <58596630+sourcery-ai[bot]@users.noreply.github.com>
  • Loading branch information
utgwkk and sourcery-ai[bot] authored May 9, 2024
1 parent 9743b6d commit 7e67bfc
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main

import (
"bytes"
"embed"
"errors"
"fmt"
template2 "html/template"
Expand Down Expand Up @@ -42,11 +43,11 @@ var staticFiles = []string{
"tmpl/robots.txt",
}

// TODO: embed
// Templates
var tplIndex = template.Must(template.ParseFiles("tmpl/index.tmpl.html"))
var tplCategoryIndex = template.Must(template.ParseFiles("tmpl/category-index.tmpl.html"))
var tplSitemap = template.Must(template.ParseFiles("tmpl/sitemap.tmpl.xml"))
//go:embed tmpl/*.tmpl.html tmpl/*.tmpl.xml
var tplFs embed.FS

var tpl = template.Must(template.ParseFS(tplFs, "tmpl/*.tmpl.html", "tmpl/*.tmpl.xml"))

// Output files
const outDir = "out/" // NOTE: trailing slash is required
Expand Down Expand Up @@ -152,7 +153,7 @@ func renderCategories(categories map[string]Category) error {
fmt.Printf("Write category Index file: %s\n", categoryIndexFilename)

buf := bytes.NewBuffer(nil)
if err := tplCategoryIndex.Execute(buf, category); err != nil {
if err := tpl.Lookup("category-index.tmpl.html").Execute(buf, category); err != nil {
return fmt.Errorf("render category `%s`: %w", categoryDir, err)
}

Expand Down Expand Up @@ -186,7 +187,7 @@ func renderSitemap(categories map[string]Category) error {

fmt.Printf("Render Sitemap to: %s\n", outSitemapFile)

if err := tplSitemap.Execute(f, categories); err != nil {
if err := tpl.Lookup("sitemap.tmpl.xml").Execute(f, categories); err != nil {
return fmt.Errorf("render sitemap: %w", err)
}

Expand Down Expand Up @@ -352,7 +353,7 @@ func renderIndex(srcFilename, outFilename string) error {
data := map[string]interface{}{
"Body": template2.HTML(body),
}
if err := tplIndex.Execute(f, data); err != nil {
if err := tpl.Lookup("index.tmpl.html").Execute(f, data); err != nil {
return err
}

Expand Down

0 comments on commit 7e67bfc

Please sign in to comment.