Use io/fs

This commit is contained in:
adnano 2022-02-09 13:18:11 -05:00
parent 3aee5385e9
commit 74cb6c4c4f
4 changed files with 41 additions and 47 deletions

View file

@ -70,7 +70,7 @@ func (site *Site) run() error {
func (s *Site) runTask(task *Task) error { func (s *Site) runTask(task *Task) error {
// Read content // Read content
s.Root = &Page{Path: "/", FilePath: "", URL: task.URL + "/"} s.Root = &Page{Path: "/", FilePath: ".", URL: task.URL + "/"}
if err := s.Root.read("content", task, s); err != nil { if err := s.Root.read("content", task, s); err != nil {
return err return err
} }

23
page.go
View file

@ -4,7 +4,7 @@ import (
"bytes" "bytes"
"fmt" "fmt"
"io" "io"
"io/ioutil" "io/fs"
"log" "log"
"os" "os"
"os/exec" "os/exec"
@ -36,11 +36,11 @@ type Page struct {
// read reads from a directory and indexes the files and directories within it. // read reads from a directory and indexes the files and directories within it.
func (p *Page) read(srcDir string, task *Task, cfg *Site) error { func (p *Page) read(srcDir string, task *Task, cfg *Site) error {
return p._read(srcDir, "", task, cfg) return p._read(os.DirFS(srcDir), ".", task, cfg)
} }
func (p *Page) _read(srcDir, path string, task *Task, cfg *Site) error { func (p *Page) _read(fsys fs.FS, path string, task *Task, cfg *Site) error {
entries, err := ioutil.ReadDir(pathpkg.Join(srcDir, path)) entries, err := fs.ReadDir(fsys, path)
if err != nil { if err != nil {
return err return err
} }
@ -59,7 +59,7 @@ func (p *Page) _read(srcDir, path string, task *Task, cfg *Site) error {
FilePath: path, FilePath: path,
URL: task.URL + dirPath, URL: task.URL + dirPath,
} }
if err := dir._read(srcDir, path, task, cfg); err != nil { if err := dir._read(fsys, path, task, cfg); err != nil {
return err return err
} }
p.Dirs = append(p.Dirs, dir) p.Dirs = append(p.Dirs, dir)
@ -70,8 +70,7 @@ func (p *Page) _read(srcDir, path string, task *Task, cfg *Site) error {
continue continue
} }
srcPath := pathpkg.Join(srcDir, path) content, err := fs.ReadFile(fsys, path)
content, err := ioutil.ReadFile(srcPath)
if err != nil { if err != nil {
return err return err
} }
@ -168,10 +167,10 @@ func (p *Page) process(cfg *Site, task *Task) error {
if task.TemplateExt != "" { if task.TemplateExt != "" {
// Create index // Create index
if p.index { if p.index {
tmpl, ok := cfg.templates.FindTemplate(p.Path, "index"+task.TemplateExt) tmpl, ok := cfg.templates.FindTemplate(p.FilePath, "index"+task.TemplateExt)
if !ok { if !ok {
// Try the base template // Try the base template
tmpl, ok = cfg.templates.FindTemplate(p.Path, "base"+task.TemplateExt) tmpl, ok = cfg.templates.FindTemplate(p.FilePath, "base"+task.TemplateExt)
} }
if ok { if ok {
var b strings.Builder var b strings.Builder
@ -185,10 +184,10 @@ func (p *Page) process(cfg *Site, task *Task) error {
// Process pages // Process pages
for i := range p.Pages { for i := range p.Pages {
var b strings.Builder var b strings.Builder
tmpl, ok := cfg.templates.FindTemplate(p.Path, "page"+task.TemplateExt) tmpl, ok := cfg.templates.FindTemplate(p.FilePath, "page"+task.TemplateExt)
if !ok { if !ok {
// Try the base template // Try the base template
tmpl, ok = cfg.templates.FindTemplate(p.Path, "base"+task.TemplateExt) tmpl, ok = cfg.templates.FindTemplate(p.FilePath, "base"+task.TemplateExt)
} }
if ok { if ok {
if err := tmpl.Execute(&b, p.Pages[i]); err != nil { if err := tmpl.Execute(&b, p.Pages[i]); err != nil {
@ -218,7 +217,7 @@ func (p *Page) buildFeed(cfg *Site, feed Feed) ([]byte, error) {
Pages []*Page Pages []*Page
} }
tmpl, ok := cfg.templates.FindTemplate(p.Path, feed.Template) tmpl, ok := cfg.templates.FindTemplate(p.FilePath, feed.Template)
if !ok { if !ok {
return nil, fmt.Errorf("failed to generate feed %q: missing feed template %q", feed.Title, feed.Template) return nil, fmt.Errorf("failed to generate feed %q: missing feed template %q", feed.Title, feed.Template)
} }

View file

@ -3,6 +3,7 @@ package main
import ( import (
"fmt" "fmt"
"os" "os"
"path"
"text/template" "text/template"
"time" "time"
@ -99,7 +100,7 @@ func LoadSite(config string) (*Site, error) {
// Populate feeds // Populate feeds
task.feeds = map[string][]Feed{} task.feeds = map[string][]Feed{}
for _, feed := range task.Feeds { for _, feed := range task.Feeds {
dir := feed.InputDir dir := path.Clean(feed.InputDir)
task.feeds[dir] = append(task.feeds[dir], feed) task.feeds[dir] = append(task.feeds[dir], feed)
} }

View file

@ -5,10 +5,8 @@ import (
htemplate "html/template" htemplate "html/template"
"io" "io"
"io/fs" "io/fs"
"io/ioutil"
"os" "os"
pathpkg "path" pathpkg "path"
"path/filepath"
"strings" "strings"
"text/template" "text/template"
"text/template/parse" "text/template/parse"
@ -67,55 +65,51 @@ func (t *Templates) Funcs(funcs map[string]interface{}) {
} }
// LoadTemplate loads a template from the provided filenames. // LoadTemplate loads a template from the provided filenames.
func (t *Templates) LoadTemplate(name string, filenames ...string) error { func (t *Templates) LoadTemplate(fsys fs.FS, path string) error {
if t.tmpls == nil { if t.tmpls == nil {
t.tmpls = map[string]Template{} t.tmpls = map[string]Template{}
} }
if ext := pathpkg.Ext(name); ext == ".html" || ext == ".xml" { if ext := pathpkg.Ext(path); ext == ".html" || ext == ".xml" {
return t.loadHTMLTemplate(name, filenames...) return t.loadHTMLTemplate(fsys, path)
} }
return t.loadTextTemplate(name, filenames...) return t.loadTextTemplate(fsys, path)
} }
func (t *Templates) loadTextTemplate(name string, filenames ...string) error { func (t *Templates) loadTextTemplate(fsys fs.FS, path string) error {
tmpl := template.New(name).Funcs(t.funcs) tmpl := template.New(path).Funcs(t.funcs)
for i := range filenames { b, err := fs.ReadFile(fsys, path)
b, err := ioutil.ReadFile(filenames[i]) if err != nil {
if err != nil { return err
return err
}
if _, err := tmpl.Parse(string(b)); err != nil {
return err
}
} }
t.tmpls[name] = textTemplate{tmpl} if _, err := tmpl.Parse(string(b)); err != nil {
return err
}
t.tmpls[path] = textTemplate{tmpl}
return nil return nil
} }
func (t *Templates) loadHTMLTemplate(name string, filenames ...string) error { func (t *Templates) loadHTMLTemplate(fsys fs.FS, path string) error {
tmpl := htemplate.New(name).Funcs(t.funcs) tmpl := htemplate.New(path).Funcs(t.funcs)
for i := range filenames { b, err := fs.ReadFile(fsys, path)
b, err := ioutil.ReadFile(filenames[i]) if err != nil {
if err != nil { return err
return err
}
if _, err := tmpl.Parse(string(b)); err != nil {
return err
}
} }
t.tmpls[name] = htmlTemplate{tmpl} if _, err := tmpl.Parse(string(b)); err != nil {
return err
}
t.tmpls[path] = htmlTemplate{tmpl}
return nil return nil
} }
// Load loads templates from the provided directory. // Load loads templates from the provided directory.
func (t *Templates) Load(dir string, exts []string) error { func (t *Templates) Load(dir string, exts []string) error {
err := filepath.WalkDir(dir, func(path string, d fs.DirEntry, err error) error { fsys := os.DirFS(dir)
err := fs.WalkDir(fsys, ".", func(path string, d fs.DirEntry, err error) error {
if err != nil { if err != nil {
return err return err
} }
if d.Type().IsRegular() { if d.Type().IsRegular() {
name := strings.TrimPrefix(path, dir) if err := t.LoadTemplate(fsys, path); err != nil {
if err := t.LoadTemplate(name, path); err != nil {
return err return err
} }
} }
@ -152,7 +146,7 @@ func (t *Templates) FindTemplate(path string, tmpl string) (Template, bool) {
if t, ok := t.tmpls[tmplPath]; ok { if t, ok := t.tmpls[tmplPath]; ok {
return t, true return t, true
} }
if t, ok := t.tmpls[pathpkg.Join("/_default", tmpl)]; ok { if t, ok := t.tmpls[pathpkg.Join("_default", tmpl)]; ok {
return t, true return t, true
} }
// Failed to find template // Failed to find template
@ -161,7 +155,7 @@ func (t *Templates) FindTemplate(path string, tmpl string) (Template, bool) {
// FindPartial returns the partial template of the given name. // FindPartial returns the partial template of the given name.
func (t *Templates) FindPartial(name string) (Template, bool) { func (t *Templates) FindPartial(name string) (Template, bool) {
if t, ok := t.tmpls[pathpkg.Join("/_partials", name)]; ok { if t, ok := t.tmpls[pathpkg.Join("_partials", name)]; ok {
return t, true return t, true
} }
return nil, false return nil, false