Skip to content

Commit

Permalink
add feed glob
Browse files Browse the repository at this point in the history
  • Loading branch information
jtarchie committed Nov 2, 2023
1 parent 54a4b78 commit 647423e
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 15 deletions.
24 changes: 24 additions & 0 deletions builder_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,5 +289,29 @@ title: required
Expect(contents).To(gbytes.Say(`https://example.com/other.html`))
Expect(contents).To(gbytes.Say(`https://example.com/dir/index.html`))
})

It("creates feeds for a certain glob", func() {
createLayout()
createFile("dir/index.md", "# some text")
createFile("other.md", "# some text")
createFile("dir/test.html", "some other text")

cli.BaseURL = "https://example.com"
cli.FeedGlob = "dir/**/*.md"
err := cli.Run()
Expect(err).NotTo(HaveOccurred())

contents := string(readFile("rss.xml").Contents())
Expect(contents).ToNot(ContainSubstring(`https://example.com/other.html`))
Expect(contents).To(ContainSubstring(`https://example.com/dir/index.html`))

contents = string(readFile("atom.xml").Contents())
Expect(contents).ToNot(ContainSubstring(`https://example.com/other.html`))
Expect(contents).To(ContainSubstring(`https://example.com/dir/index.html`))

contents = string(readFile("sitemap.xml").Contents())
Expect(contents).ToNot(ContainSubstring(`https://example.com/other.html`))
Expect(contents).To(ContainSubstring(`https://example.com/dir/index.html`))
})
})
})
24 changes: 19 additions & 5 deletions cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@ import (
)

type CLI struct {
AssetsPath string `help:"path to static assets (default with be source-path/public)"`
BaseURL string `help:"the URL which the contents will be served from, this is only used for generating feeds"`
BuildPath string `help:"where generated content should go" required:"" type:"path"`
FeedGlob string `help:"glob patterns for documents to feature in feeds"`
LayoutFilename string `default:"layout.html" help:"layout file to render" required:""`
Serve bool `help:"serve when done building"`
SourcePath string `help:"source of all files" required:"" type:"path"`
AssetsPath string `help:"path to static assets (default with be source-path/public)"`
BaseURL string `help:"the URL which the contents will be served from, this is only used for generating feeds"`
}

func (c *CLI) Run() error {
Expand All @@ -35,15 +36,24 @@ func (c *CLI) Run() error {

markdownGlob := filepath.Join(c.SourcePath, "**", "*.md")

err := renderer.Execute(markdownGlob)
if c.FeedGlob == "" {
c.FeedGlob = markdownGlob
} else {
c.FeedGlob = filepath.Join(c.SourcePath, c.FeedGlob)
}

err := renderer.Execute(
markdownGlob,
c.FeedGlob,
)
if err != nil {
return fmt.Errorf("could not execute render: %w", err)
}

if c.Serve {
watcher := NewWatcher(c.SourcePath)

go c.startWatcher(watcher, renderer, markdownGlob)
go c.startWatcher(watcher, renderer, markdownGlob, c.FeedGlob)

e := echo.New()
e.Use(middleware.Logger())
Expand All @@ -62,6 +72,7 @@ func (c *CLI) startWatcher(
watcher *Watcher,
renderer *Render,
markdownGlob string,
feedGlob string,
) {
allGlob := filepath.Join(c.SourcePath, "**", "{*.md,*.html,*.js,*.css}")

Expand All @@ -71,7 +82,10 @@ func (c *CLI) startWatcher(
if matched {
slog.Info("rebuilding markdown files", slog.String("filename", filename))

err := renderer.Execute(markdownGlob)
err := renderer.Execute(
markdownGlob,
feedGlob,
)
if err != nil {
slog.Error("could not rebuild markdown files", slog.String("error", err.Error()))
}
Expand Down
36 changes: 26 additions & 10 deletions render.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"time"

"github.com/Masterminds/sprig/v3"
"github.com/bmatcuk/doublestar/v4"
"github.com/gorilla/feeds"
"github.com/gosimple/slug"
"github.com/microcosm-cc/bluemonday"
Expand Down Expand Up @@ -83,13 +84,16 @@ func NewRender(
}

//nolint:funlen,cyclop
func (r *Render) Execute(pattern string) error {
func (r *Render) Execute(
docsGlob string,
feedGlob string,
) error {
err := r.copyAssets()
if err != nil {
return fmt.Errorf("copying assets issue: %w", err)
}

docs, err := NewDocs(r.sourcePath, pattern, 0, false)
docs, err := NewDocs(r.sourcePath, docsGlob, 0, false)
if err != nil {
return fmt.Errorf("could not glob markdown files: %w", err)
}
Expand Down Expand Up @@ -134,23 +138,31 @@ func (r *Render) Execute(pattern string) error {
})
}

if r.baseURL != "" {
group.Go(func() error {
err = r.generateFeeds(docs, feedGlob, funcMap)
if err != nil {
return fmt.Errorf("could not render feeds: %w", err)
}

return nil
})
}

err = group.Wait()
if err != nil {
return fmt.Errorf("could not render: %w", err)
}

if r.baseURL != "" {
err = r.generateFeeds(docs, funcMap)
if err != nil {
return fmt.Errorf("could not render feeds: %w", err)
}
}

return nil
}

//nolint:funlen
func (r *Render) generateFeeds(docs Docs, funcMap template.FuncMap) error {
func (r *Render) generateFeeds(
docs Docs,
feedGlob string,
funcMap template.FuncMap,
) error {
now := time.Now().UTC()

feed := &feeds.Feed{
Expand All @@ -169,6 +181,10 @@ func (r *Render) generateFeeds(docs Docs, funcMap template.FuncMap) error {
sanitizer := bluemonday.UGCPolicy()

for _, doc := range docs {
if matched, _ := doublestar.Match(feedGlob, doc.Filename()); !matched {
continue
}

modifiedTime := doc.Timespec.ModTime().UTC()
createdTime := modifiedTime

Expand Down

0 comments on commit 647423e

Please sign in to comment.