refactor: some architecture rework, added a single processing context that will be used to track dependencies

This commit is contained in:
2023-01-16 01:20:10 +01:00
parent 3f491e3e8f
commit f5af79c7d9
16 changed files with 103 additions and 62 deletions
+3 -1
View File
@@ -53,7 +53,9 @@ func (op *Categorize) Configure(config map[string]any) error {
return nil
}
func (op *Categorize) ProcessList(contents []cabret.Content) ([]cabret.Content, error) {
var _ cabret.ListOperation = &Categorize{}
func (op *Categorize) ProcessList(ctx *cabret.Context, contents []cabret.Content) ([]cabret.Content, error) {
key := strcase.ToCamel(op.Key)
categories := map[string][]cabret.Content{}
+3 -1
View File
@@ -30,7 +30,9 @@ func (op *Chunk) Configure(options map[string]any) error {
return nil
}
func (op *Chunk) ProcessList(items []cabret.Content) ([]cabret.Content, error) {
var _ cabret.ListOperation = &Chunk{}
func (op *Chunk) ProcessList(ctx *cabret.Context, items []cabret.Content) ([]cabret.Content, error) {
totalChunks := len(items) / op.Size
chunks := make([][]cabret.Content, totalChunks, totalChunks+1)
+3 -1
View File
@@ -23,7 +23,9 @@ func (op *Frontmatter) Configure(config map[string]any) error {
return nil
}
func (op *Frontmatter) ProcessItem(content cabret.Content) (*cabret.Content, error) {
var _ cabret.ItemOperation = &Frontmatter{}
func (op *Frontmatter) ProcessItem(ctx *cabret.Context, content cabret.Content) (*cabret.Content, error) {
md := goldmark.New(
goldmark.WithExtensions(
meta.Meta,
+7 -5
View File
@@ -64,7 +64,9 @@ func (op *Layout) Configure(config map[string]any) error {
return fmt.Errorf(`invalid config`)
}
func (op Layout) ProcessItem(content cabret.Content) (*cabret.Content, error) {
var _ cabret.ItemOperation = &Layout{}
func (op Layout) ProcessItem(ctx *cabret.Context, content cabret.Content) (*cabret.Content, error) {
// expand glob patterns
tmplFiles := []string{}
for _, pat := range op.TemplatePatterns {
@@ -82,17 +84,17 @@ func (op Layout) ProcessItem(content cabret.Content) (*cabret.Content, error) {
return nil, err
}
ctx := util.CloneMap(content.Metadata)
metadata := util.CloneMap(content.Metadata)
if content.Type == HtmlMimeType {
ctx["Content"] = goHtmlTemplate.HTML(content.Data)
metadata["Content"] = goHtmlTemplate.HTML(content.Data)
} else {
ctx["Content"] = content.Data
metadata["Content"] = content.Data
}
log.Printf(`[operation.Layout] rendering into layout "%s"`, strings.Join(op.TemplatePatterns, ", "))
data, err := tmpl.Render(ctx)
data, err := tmpl.Render(metadata)
if err != nil {
return nil, err
}
+3 -1
View File
@@ -24,7 +24,9 @@ func (op *Markdown) Configure(config map[string]any) error {
return nil
}
func (op Markdown) ProcessItem(content cabret.Content) (*cabret.Content, error) {
var _ cabret.ItemOperation = &Markdown{}
func (op Markdown) ProcessItem(ctx *cabret.Context, content cabret.Content) (*cabret.Content, error) {
md := goldmark.New(
goldmark.WithExtensions(
extension.GFM,
+3 -1
View File
@@ -107,7 +107,9 @@ func (op *Program) Configure(options map[string]any) error {
return nil
}
func (op *Program) ProcessItem(item cabret.Content) (*cabret.Content, error) {
var _ cabret.ItemOperation = &Program{}
func (op *Program) ProcessItem(ctx *cabret.Context, item cabret.Content) (*cabret.Content, error) {
ioFmt, ok := ioProgramFormats[op.IOFormat]
if !ok {
return nil, fmt.Errorf(`unknown io format "%s"`, op.IOFormat)
+8
View File
@@ -2,6 +2,7 @@ package operation
import (
"fmt"
"log"
"reflect"
"github.com/aziis98/cabret"
@@ -13,6 +14,13 @@ const ShortFormValueKey = "value"
var registry = map[string]reflect.Type{}
func registerType(name string, op cabret.Operation) {
switch op.(type) {
case cabret.ItemOperation:
case cabret.ListOperation:
default:
log.Fatal(`operation must also satisfy one of cabret.ListOperation or cabret.ItemOperation`)
}
typ := reflect.TypeOf(op).Elem()
registry[name] = typ
}
+3 -1
View File
@@ -36,7 +36,9 @@ func (op *Slice) Configure(options map[string]any) error {
return nil
}
func (op *Slice) ProcessList(items []cabret.Content) ([]cabret.Content, error) {
var _ cabret.ListOperation = &Slice{}
func (op *Slice) ProcessList(ctx *cabret.Context, items []cabret.Content) ([]cabret.Content, error) {
from := op.From
to := op.To
+7 -5
View File
@@ -53,22 +53,24 @@ func (op *Source) Configure(config map[string]any) error {
return fmt.Errorf(`invalid config for "source": %#v`, config)
}
func (op Source) ProcessList(contents []cabret.Content) ([]cabret.Content, error) {
files, err := cabret.FindFiles([]string{})
var _ cabret.ListOperation = &Source{}
func (op Source) ProcessList(ctx *cabret.Context, contents []cabret.Content) ([]cabret.Content, error) {
files, err := cabret.FindFiles()
if err != nil {
return nil, err
}
matches := []cabret.MatchResult{}
for _, patternStr := range op.Patterns {
pat, err := path.ParsePattern(patternStr)
for _, rawPattern := range op.Patterns {
pattern, err := path.ParsePattern(rawPattern)
if err != nil {
return nil, err
}
for _, f := range files {
if ok, captures, _ := pat.Match(f); ok {
if ok, captures, _ := pattern.Match(f); ok {
matches = append(matches, cabret.MatchResult{
File: f,
Captures: captures,
+3 -1
View File
@@ -36,7 +36,9 @@ func (op *Target) Configure(config map[string]any) error {
return fmt.Errorf(`invalid config for "target": %#v`, config)
}
func (op Target) ProcessItem(c cabret.Content) (*cabret.Content, error) {
var _ cabret.ItemOperation = &Target{}
func (op Target) ProcessItem(ctx *cabret.Context, c cabret.Content) (*cabret.Content, error) {
log.Printf(`[operation.Target] expanding pattern "%s"`, op.PathTemplate)
target := op.PathTemplate
+3 -1
View File
@@ -32,7 +32,9 @@ func (op *Template) Configure(options map[string]any) error {
return nil
}
func (op *Template) ProcessList(items []cabret.Content) ([]cabret.Content, error) {
var _ cabret.ListOperation = &Template{}
func (op *Template) ProcessList(ctx *cabret.Context, items []cabret.Content) ([]cabret.Content, error) {
var t bytes.Buffer
// concatenate all templates