Open() (io.ReadCloser, error)
}
+type FileOpener interface {
+ Opener
+ FileName() string
+}
+
type OpenerFunc func() (io.ReadCloser, error)
func (opener OpenerFunc) Open() (io.ReadCloser, error) {
return opener()
}
+type fileOpener struct {
+ absPath string
+ alwaysErr error
+}
+
+func (opener *fileOpener) Open() (io.ReadCloser, error) {
+ if opener.alwaysErr != nil {
+ return nil, opener.alwaysErr
+ }
+ return os.Open(opener.absPath)
+}
+
+func (fopener *fileOpener) FileName() string {
+ return fopener.absPath
+}
+
// File returns an Opener that Opens() a file relative to the calling package.
-func File(relativePath string, ignorePackages ...string) Opener {
- var root string
- var alwaysErr error
+func File(relativePath string, ignorePackages ...string) FileOpener {
+ opener := new(fileOpener)
caller, err := getCallStackButt(ignorePackages)
if err != nil {
- alwaysErr = err
+ opener.alwaysErr = err
} else {
- root = path.Dir(caller)
+ opener.absPath = path.Join(path.Dir(caller), relativePath)
}
- return OpenerFunc(func() (io.ReadCloser, error) {
- if alwaysErr != nil {
- return nil, alwaysErr
- }
- return os.Open(path.Join(root, relativePath))
- })
+ return opener
}
import (
"context"
"html/template"
+ "path/filepath"
+ "strings"
"git.earlybird.gay/today-engine/htmltree"
"git.earlybird.gay/today-engine/include"
return p
}
+// Static returns a new Page that is just the passed source file, with no
+// additional options/parts/etc.
+func Static(source string) *Page {
+ filename := filepath.Base(source)
+ name := strings.TrimSuffix(filename, filepath.Ext(filename))
+ return New(name, source)
+}
+
// With returns a shallow copy of p with all of optional applied to it.
func (p *Page) With(optional ...func(*Page)) *Page {
q := &Page{