]> git.earlybird.gay Git - today/commitdiff
remember to commit from the base directory
authorearly <me@earlybird.gay>
Mon, 2 Sep 2024 18:47:13 +0000 (12:47 -0600)
committerearly <me@earlybird.gay>
Mon, 2 Sep 2024 18:47:13 +0000 (12:47 -0600)
include/include.go
page/page.go

index 874e62a6c024236bad889780baf04e55b7fc5ded..9678d53519ce49b8cfd19d757ae49aabaaec9db6 100644 (file)
@@ -11,26 +11,41 @@ type Opener interface {
        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
 }
index a619ba1d47cdd08879674cb3dc17dd4b99e96a25..65c2f052bac822cbca96c1b3b2a433b6a7c6f44f 100644 (file)
@@ -4,6 +4,8 @@ package page
 import (
        "context"
        "html/template"
+       "path/filepath"
+       "strings"
 
        "git.earlybird.gay/today-engine/htmltree"
        "git.earlybird.gay/today-engine/include"
@@ -101,6 +103,14 @@ func New(name string, source string, optional ...func(*Page)) *Page {
        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{