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 {
// 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 {
return err
}

23
page.go
View file

@ -4,7 +4,7 @@ import (
"bytes"
"fmt"
"io"
"io/ioutil"
"io/fs"
"log"
"os"
"os/exec"
@ -36,11 +36,11 @@ type Page struct {
// read reads from a directory and indexes the files and directories within it.
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 {
entries, err := ioutil.ReadDir(pathpkg.Join(srcDir, path))
func (p *Page) _read(fsys fs.FS, path string, task *Task, cfg *Site) error {
entries, err := fs.ReadDir(fsys, path)
if err != nil {
return err
}
@ -59,7 +59,7 @@ func (p *Page) _read(srcDir, path string, task *Task, cfg *Site) error {
FilePath: path,
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
}
p.Dirs = append(p.Dirs, dir)
@ -70,8 +70,7 @@ func (p *Page) _read(srcDir, path string, task *Task, cfg *Site) error {
continue
}
srcPath := pathpkg.Join(srcDir, path)
content, err := ioutil.ReadFile(srcPath)
content, err := fs.ReadFile(fsys, path)
if err != nil {
return err
}
@ -168,10 +167,10 @@ func (p *Page) process(cfg *Site, task *Task) error {
if task.TemplateExt != "" {
// Create 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 {
// 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 {
var b strings.Builder
@ -185,10 +184,10 @@ func (p *Page) process(cfg *Site, task *Task) error {
// Process pages
for i := range p.Pages {
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 {
// 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 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
}
tmpl, ok := cfg.templates.FindTemplate(p.Path, feed.Template)
tmpl, ok := cfg.templates.FindTemplate(p.FilePath, feed.Template)
if !ok {
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 (
"fmt"
"os"
"path"
"text/template"
"time"
@ -99,7 +100,7 @@ func LoadSite(config string) (*Site, error) {
// Populate feeds
task.feeds = map[string][]Feed{}
for _, feed := range task.Feeds {
dir := feed.InputDir
dir := path.Clean(feed.InputDir)
task.feeds[dir] = append(task.feeds[dir], feed)
}

View file

@ -5,10 +5,8 @@ import (
htemplate "html/template"
"io"
"io/fs"
"io/ioutil"
"os"
pathpkg "path"
"path/filepath"
"strings"
"text/template"
"text/template/parse"
@ -67,55 +65,51 @@ func (t *Templates) Funcs(funcs map[string]interface{}) {
}
// 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 {
t.tmpls = map[string]Template{}
}
if ext := pathpkg.Ext(name); ext == ".html" || ext == ".xml" {
return t.loadHTMLTemplate(name, filenames...)
if ext := pathpkg.Ext(path); ext == ".html" || ext == ".xml" {
return t.loadHTMLTemplate(fsys, path)
}
return t.loadTextTemplate(name, filenames...)
return t.loadTextTemplate(fsys, path)
}
func (t *Templates) loadTextTemplate(name string, filenames ...string) error {
tmpl := template.New(name).Funcs(t.funcs)
for i := range filenames {
b, err := ioutil.ReadFile(filenames[i])
if err != nil {
return err
}
if _, err := tmpl.Parse(string(b)); err != nil {
return err
}
func (t *Templates) loadTextTemplate(fsys fs.FS, path string) error {
tmpl := template.New(path).Funcs(t.funcs)
b, err := fs.ReadFile(fsys, path)
if 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
}
func (t *Templates) loadHTMLTemplate(name string, filenames ...string) error {
tmpl := htemplate.New(name).Funcs(t.funcs)
for i := range filenames {
b, err := ioutil.ReadFile(filenames[i])
if err != nil {
return err
}
if _, err := tmpl.Parse(string(b)); err != nil {
return err
}
func (t *Templates) loadHTMLTemplate(fsys fs.FS, path string) error {
tmpl := htemplate.New(path).Funcs(t.funcs)
b, err := fs.ReadFile(fsys, path)
if 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
}
// Load loads templates from the provided directory.
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 {
return err
}
if d.Type().IsRegular() {
name := strings.TrimPrefix(path, dir)
if err := t.LoadTemplate(name, path); err != nil {
if err := t.LoadTemplate(fsys, path); err != nil {
return err
}
}
@ -152,7 +146,7 @@ func (t *Templates) FindTemplate(path string, tmpl string) (Template, bool) {
if t, ok := t.tmpls[tmplPath]; ok {
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
}
// 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.
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 nil, false