From: early Date: Mon, 2 Sep 2024 18:47:13 +0000 (-0600) Subject: remember to commit from the base directory X-Git-Url: https://git.earlybird.gay/?a=commitdiff_plain;h=d6fa456eb603603e7aad7bf3d7bf0f23bef71903;p=today remember to commit from the base directory --- diff --git a/include/include.go b/include/include.go index 874e62a..9678d53 100644 --- a/include/include.go +++ b/include/include.go @@ -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 } diff --git a/page/page.go b/page/page.go index a619ba1..65c2f05 100644 --- a/page/page.go +++ b/page/page.go @@ -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{