Skip to content

Commit

Permalink
refactor into one render object
Browse files Browse the repository at this point in the history
  • Loading branch information
jtarchie committed Mar 31, 2023
1 parent d4baaf8 commit 9ce8e61
Show file tree
Hide file tree
Showing 8 changed files with 217 additions and 334 deletions.
19 changes: 7 additions & 12 deletions builder_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"github.com/jtarchie/builder"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"go.uber.org/zap"
)

func TestBuilder(t *testing.T) {
Expand All @@ -22,7 +21,6 @@ var _ = Describe("Builder", func() {
sourcePath string
buildPath string
cli *builder.CLI
logger *zap.Logger
)

createFile := func(filename, contents string) {
Expand Down Expand Up @@ -67,9 +65,6 @@ var _ = Describe("Builder", func() {
buildPath, err = os.MkdirTemp("", "")
Expect(err).NotTo(HaveOccurred())

logger, err = zap.NewDevelopment()
Expect(err).NotTo(HaveOccurred())

cli = &builder.CLI{
SourcePath: sourcePath,
BuildPath: buildPath,
Expand All @@ -85,7 +80,7 @@ var _ = Describe("Builder", func() {
})

It("renders all files successfully", func() {
err := cli.Run(logger)
err := cli.Run()
Expect(err).NotTo(HaveOccurred())

contents := readFile("index.html")
Expand All @@ -104,7 +99,7 @@ var _ = Describe("Builder", func() {
})

It("renders a title and description", func() {
err := cli.Run(logger)
err := cli.Run()
Expect(err).NotTo(HaveOccurred())

contents := readFile("index.html")
Expand All @@ -118,15 +113,15 @@ var _ = Describe("Builder", func() {
createLayout()
createFile("index.md", "some text")

err := cli.Run(logger)
err := cli.Run()
Expect(err).To(HaveOccurred())
})

It("uses H1 for the title", func() {
createLayout()
createFile("index.md", "# some title")

err := cli.Run(logger)
err := cli.Run()
Expect(err).NotTo(HaveOccurred())

contents := readFile("index.html")
Expand Down Expand Up @@ -164,7 +159,7 @@ title: required
})

It("looks for the latest posts within a directory", func() {
err := cli.Run(logger)
err := cli.Run()
Expect(err).NotTo(HaveOccurred())

contents := readFile("index.html")
Expand All @@ -189,7 +184,7 @@ title: required

When("no layout exists", func() {
It("fails with an error", func() {
err := cli.Run(logger)
err := cli.Run()
Expect(err).To(HaveOccurred())
})
})
Expand All @@ -205,7 +200,7 @@ title: required
LayoutFilename: "layout.html",
}

err = cli.Run(logger)
err = cli.Run()
Expect(err).NotTo(HaveOccurred())

contents, err := os.ReadFile(filepath.Join(buildPath, "markdown.html"))
Expand Down
141 changes: 6 additions & 135 deletions cli.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,7 @@
package builder

import (
"fmt"
"os"
"path/filepath"

"github.com/bmatcuk/doublestar/v4"
cp "github.com/otiai10/copy"
"github.com/yuin/goldmark"
emoji "github.com/yuin/goldmark-emoji"
highlighting "github.com/yuin/goldmark-highlighting"
"github.com/yuin/goldmark/extension"
"github.com/yuin/goldmark/parser"
"github.com/yuin/goldmark/renderer/html"
"go.abhg.dev/goldmark/mermaid"
"go.uber.org/zap"
)

type CLI struct {
Expand All @@ -23,128 +10,12 @@ type CLI struct {
LayoutFilename string `help:"layout file to render" required:"" default:"layout.html"`
}

func (c *CLI) Run(logger *zap.Logger) error {
// rm build dir
err := c.buildSetup(logger)
if err != nil {
return fmt.Errorf("build setup failed: %w", err)
}

// copy public directory files over to top level
err = c.assetSetup(logger)
if err != nil {
return fmt.Errorf("asset setup failed: %w", err)
}

// go through each markdown
templates := NewTemplates(c.SourcePath)
layoutPath := filepath.Join(c.SourcePath, c.LayoutFilename)

layout, err := templates.html(layoutPath)
if err != nil {
return fmt.Errorf("could not get layout (%s): %w", layoutPath, err)
}

logger = logger.Named("markdown")
pattern := filepath.Join(c.SourcePath, "**", "*.md")

logger.Info("glob",
zap.String("pattern", pattern),
)

matches, err := doublestar.FilepathGlob(pattern)
if err != nil {
return fmt.Errorf("could not glob markdown files: %w", err)
}

converter := goldmark.New(
goldmark.WithRendererOptions(
html.WithXHTML(),
html.WithUnsafe(),
),
goldmark.WithExtensions(
extension.GFM,
emoji.Emoji,
&mermaid.Extender{},
highlighting.Highlighting,
extension.DefinitionList,
extension.Footnote,
extension.Typographer,
),
goldmark.WithParserOptions(
parser.WithAutoHeadingID(),
parser.WithAttribute(),
),
)

for _, markdownPath := range matches {
// render to HTML within layout
logger.Info("reading",
zap.String("file", markdownPath),
)

markdown := NewMarkdownDoc(
markdownPath,
c.SourcePath,
c.BuildPath,
logger,
)

// save to correct directory
err := markdown.Write(
layout,
converter,
templates,
)
if err != nil {
return fmt.Errorf("could not render markdown: %w", err)
}
}

return nil
}

func (c *CLI) assetSetup(logger *zap.Logger) error {
assetsPath := filepath.Join(c.SourcePath, "public")

logger.Info("copying",
zap.String("build_path", c.BuildPath),
zap.String("assets_path", assetsPath),
func (c *CLI) Run() error {
renderer := NewRender(
filepath.Join(c.SourcePath, c.LayoutFilename),
c.SourcePath,
c.BuildPath,
)

if _, err := os.Stat(assetsPath); !os.IsNotExist(err) {
err = cp.Copy(assetsPath, c.BuildPath)
if err != nil {
return fmt.Errorf("could not copy assets contents (%s): %w", assetsPath, err)
}
} else {
logger.Info("copying.skipping",
zap.String("build_path", c.BuildPath),
zap.String("assets_path", assetsPath),
)
}

return nil
}

func (c *CLI) buildSetup(logger *zap.Logger) error {
logger.Info("removing",
zap.String("build_path", c.BuildPath),
)

err := os.RemoveAll(c.BuildPath)
if err != nil {
return fmt.Errorf("could not remove build path (%s): %w", c.BuildPath, err)
}

logger.Info("removing",
zap.String("build_path", c.BuildPath),
)

err = os.MkdirAll(c.BuildPath, 0777)
if err != nil {
return fmt.Errorf("could not create build path (%s): %w", c.BuildPath, err)
}

return nil
return renderer.Execute()
}
10 changes: 1 addition & 9 deletions cmd/main.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,14 @@
package main

import (
"log"

"github.com/alecthomas/kong"
"github.com/jtarchie/builder"
"go.uber.org/zap"
)

func main() {
logger, err := zap.NewProduction()
if err != nil {
log.Fatalf("could not start logger: %s", err)
}

cli := &builder.CLI{}
ctx := kong.Parse(cli)
// Call the Run() method of the selected parsed command.
err = ctx.Run(logger)
err := ctx.Run()
ctx.FatalIfErrorf(err)
}
13 changes: 10 additions & 3 deletions docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,35 @@ package builder

import (
"fmt"
"regexp"
"sort"
"strings"

"github.com/bmatcuk/doublestar/v4"
"github.com/samber/lo"
)

type Docs []*Doc

func NewDocs(pattern string, ignore *regexp.Regexp) (Docs, error) {
func NewDocs(
pattern string,
limit int,
) (Docs, error) {
matches, err := doublestar.FilepathGlob(pattern)
if err != nil {
return nil, fmt.Errorf("could not matches (%q): %w", pattern, err)
}

matches = lo.Filter(matches, func(match string, _ int) bool {
return !ignore.MatchString(match)
return !strings.HasPrefix(match, "index.md")
})

sort.Strings(matches)
sort.Sort(sort.Reverse(sort.StringSlice(matches)))

if limit > 0 && len(matches) > limit {
matches = matches[:limit]
}

docs := Docs{}

for _, match := range matches {
Expand Down
Loading

0 comments on commit 9ce8e61

Please sign in to comment.