Skip to content

Commit

Permalink
Change memoization to the scope of markdownFS instead of all page sou…
Browse files Browse the repository at this point in the history
…rces

This should allow for watching filesystem
  • Loading branch information
emad-elsaid committed May 30, 2024
1 parent ed333f5 commit aaed1c1
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 58 deletions.
69 changes: 69 additions & 0 deletions markdown_fs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package xlog

import (
"context"
"errors"
"io/fs"
"path"
"path/filepath"

"github.com/emad-elsaid/memoize"
)

func newMarkdownFS(path string) *markdownFS {
m := markdownFS{path: path}

m._page = memoize.New(func(name string) Page {
if name == "" {
name = INDEX
}

return &page{
name: name,
}
})

return &m
}

// MarkdownCWDFS a current directory markdown pages
type markdownFS struct {
path string
_page func(string) Page
}

// Page Creates an instance of Page with name. if no name is passed it's assumed INDEX
func (m *markdownFS) Page(name string) Page {
return m._page(name)
}

func (m *markdownFS) Each(ctx context.Context, f func(Page)) {
filepath.WalkDir(m.path, func(name string, d fs.DirEntry, err error) error {
if d.IsDir() {
for _, v := range ignoredDirs {
if v.MatchString(name) {
return fs.SkipDir
}
}

return nil
}

select {

case <-ctx.Done():
return errors.New("context stopped")

default:
ext := path.Ext(name)
basename := name[:len(name)-len(ext)]

if ext == ".md" {
f(m.Page(basename))
}

}

return nil
})
}
61 changes: 3 additions & 58 deletions page_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,6 @@ package xlog

import (
"context"
"errors"
"io/fs"
"path"
"path/filepath"

"github.com/emad-elsaid/memoize"
)

type PageSource interface {
Expand All @@ -18,10 +12,10 @@ type PageSource interface {
}

var sources = []PageSource{
&markdownCWDFS{},
newMarkdownFS("."),
}

var NewPage = memoize.New(func(name string) (p Page) {
func NewPage(name string) (p Page) {
for i := range sources {
p = sources[i].Page(name)
if p != nil && p.Exists() {
Expand All @@ -30,57 +24,8 @@ var NewPage = memoize.New(func(name string) (p Page) {
}

return
})
}

func RegisterPageSource(p PageSource) {
sources = append([]PageSource{p}, sources...)
}

// MarkdownCWDFS a current directory markdown pages
type markdownCWDFS struct{}

var cachedPage = memoize.New(func(name string) Page {
if name == "" {
name = INDEX
}

return &page{
name: name,
}
})

// NewPage Creates an instance of Page with name. if no name is passed it's assumed INDEX
func (m *markdownCWDFS) Page(name string) Page {
return cachedPage(name)
}

func (m *markdownCWDFS) Each(ctx context.Context, f func(Page)) {
filepath.WalkDir(".", func(name string, d fs.DirEntry, err error) error {
if d.IsDir() {
for _, v := range ignoredDirs {
if v.MatchString(name) {
return fs.SkipDir
}
}

return nil
}

select {

case <-ctx.Done():
return errors.New("context stopped")

default:
ext := path.Ext(name)
basename := name[:len(name)-len(ext)]

if ext == ".md" {
f(m.Page(basename))
}

}

return nil
})
}

0 comments on commit aaed1c1

Please sign in to comment.