lifecycle

package
v0.4.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 24, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package lifecycle provides the lifecycle management system for markata-go.

The lifecycle system orchestrates the build process through 9 stages:

  • configure: Load configuration and initialize plugins
  • validate: Validate configuration before processing
  • glob: Discover content files
  • load: Parse files into posts
  • transform: Pre-render processing (jinja-md, etc.)
  • render: Convert markdown to HTML
  • collect: Build feeds, navigation, and aggregated content
  • write: Write output files
  • cleanup: Release resources

Plugin System

Plugins implement optional interfaces corresponding to each stage:

type MyPlugin struct{}

func (p *MyPlugin) Name() string { return "my-plugin" }

func (p *MyPlugin) Load(m *Manager) error {
    // Process files into posts
    return nil
}

func (p *MyPlugin) Render(m *Manager) error {
    // Render posts to HTML
    return nil
}

Priority Ordering

Plugins can implement PriorityPlugin to control execution order:

func (p *MyPlugin) Priority(stage Stage) int {
    if stage == StageRender {
        return PriorityLast // Run after other render plugins
    }
    return PriorityDefault
}

Usage

Basic usage:

m := lifecycle.NewManager()
m.RegisterPlugin(&MyGlobPlugin{})
m.RegisterPlugin(&MyLoadPlugin{})
m.RegisterPlugin(&MyRenderPlugin{})

if err := m.Run(); err != nil {
    log.Fatal(err)
}

Running up to a specific stage:

// Run only up to load stage (useful for preview/watch mode)
if err := m.RunTo(lifecycle.StageLoad); err != nil {
    log.Fatal(err)
}

// Later, continue from where we left off
if err := m.Run(); err != nil {
    log.Fatal(err)
}

Filtering and mapping posts:

// Get published posts
posts, _ := m.Filter("published==true")

// Get all titles of posts with tag "golang", sorted by date
titles, _ := m.Map("title", "tags contains golang", "date", true)

Index

Constants

View Source
const (
	// PriorityFirst ensures a plugin runs before most others.
	PriorityFirst = -1000

	// PriorityEarly ensures a plugin runs early in the stage.
	PriorityEarly = -100

	// PriorityDefault is the default priority.
	PriorityDefault = 0

	// PriorityLate ensures a plugin runs late in the stage.
	PriorityLate = 100

	// PriorityLast ensures a plugin runs after most others.
	PriorityLast = 1000
)

Priority constants for common ordering scenarios.

Variables

StageOrder defines the execution order of all lifecycle stages.

Functions

func IsValidStage

func IsValidStage(s Stage) bool

IsValidStage checks if the given stage is a valid lifecycle stage.

func StageIndex

func StageIndex(s Stage) int

StageIndex returns the index of a stage in the execution order. Returns -1 if the stage is not found.

Types

type Cache

type Cache interface {
	Get(key string) (interface{}, bool)
	Set(key string, value interface{})
	Delete(key string)
	Clear()
}

Cache is an interface for caching data between stages.

type CleanupPlugin

type CleanupPlugin interface {
	Plugin
	Cleanup(m *Manager) error
}

CleanupPlugin is implemented by plugins that participate in the cleanup stage. This stage is used to release resources and perform cleanup tasks.

type CollectPlugin

type CollectPlugin interface {
	Plugin
	Collect(m *Manager) error
}

CollectPlugin is implemented by plugins that participate in the collect stage. This stage is used to build feeds, navigation, and other aggregated content.

type Config

type Config struct {
	// ContentDir is the directory containing content files.
	ContentDir string

	// OutputDir is the directory for generated output.
	OutputDir string

	// GlobPatterns are patterns to match content files.
	GlobPatterns []string

	// Extra holds additional configuration options.
	Extra map[string]interface{}
}

Config holds markata configuration.

func NewConfig

func NewConfig() *Config

NewConfig creates a new Config with default values.

type ConfigurePlugin

type ConfigurePlugin interface {
	Plugin
	Configure(m *Manager) error
}

ConfigurePlugin is implemented by plugins that participate in the configure stage. This stage is used to load configuration and initialize plugin state.

type Feed

type Feed struct {
	// Name is the feed identifier.
	Name string

	// Title is the feed title.
	Title string

	// Posts are the posts included in the feed.
	Posts []*models.Post

	// Content is the generated feed content.
	Content string

	// Path is the output path for the feed.
	Path string
}

Feed represents a generated feed (RSS, Atom, etc.).

type GlobPlugin

type GlobPlugin interface {
	Plugin
	Glob(m *Manager) error
}

GlobPlugin is implemented by plugins that participate in the glob stage. This stage is used to discover content files.

type HookError

type HookError struct {
	Stage    Stage
	Plugin   string
	Err      error
	Critical bool
}

HookError represents an error that occurred during hook execution.

func (*HookError) Error

func (e *HookError) Error() string

func (*HookError) Unwrap

func (e *HookError) Unwrap() error

type HookErrors

type HookErrors struct {
	Errors []*HookError
}

HookErrors is a collection of errors from hook execution.

func (*HookErrors) Add

func (e *HookErrors) Add(stage Stage, plugin string, err error, critical bool)

Add adds an error to the collection.

func (*HookErrors) Error

func (e *HookErrors) Error() string

func (*HookErrors) HasCritical

func (e *HookErrors) HasCritical() bool

HasCritical returns true if any error is marked as critical.

type LoadPlugin

type LoadPlugin interface {
	Plugin
	Load(m *Manager) error
}

LoadPlugin is implemented by plugins that participate in the load stage. This stage is used to parse files into posts.

type Manager

type Manager struct {
	// contains filtered or unexported fields
}

Manager orchestrates the lifecycle stages and plugin execution.

func NewManager

func NewManager() *Manager

NewManager creates a new lifecycle Manager with default settings.

func (*Manager) AddFeed

func (m *Manager) AddFeed(feed *Feed)

AddFeed adds a feed to the feeds slice.

func (*Manager) AddFile

func (m *Manager) AddFile(file string)

AddFile adds a file path to the files slice.

func (*Manager) AddPost

func (m *Manager) AddPost(post *models.Post)

AddPost adds a post to the posts slice.

func (*Manager) Cache

func (m *Manager) Cache() Cache

Cache returns the cache instance.

func (*Manager) Concurrency

func (m *Manager) Concurrency() int

Concurrency returns the concurrency level.

func (*Manager) Config

func (m *Manager) Config() *Config

Config returns the current configuration.

func (*Manager) CurrentStage

func (m *Manager) CurrentStage() Stage

CurrentStage returns the currently executing stage.

func (*Manager) Feeds

func (m *Manager) Feeds() []*Feed

Feeds returns a copy of the feeds slice.

func (*Manager) Files

func (m *Manager) Files() []string

Files returns a copy of the discovered file paths.

func (*Manager) Filter

func (m *Manager) Filter(expr string) ([]*models.Post, error)

Filter returns posts matching the given expression. The expression supports simple field comparisons:

  • "published==true" - field equals value
  • "draft!=true" - field not equals value
  • "tags contains golang" - slice contains value
  • Multiple conditions can be combined with " and " or " or "

func (*Manager) HasRun

func (m *Manager) HasRun(stage Stage) bool

HasRun returns true if the given stage has completed.

func (*Manager) Map

func (m *Manager) Map(field, filter, sortField string, reverse bool) ([]interface{}, error)

Map extracts field values from posts, with optional filtering and sorting. Parameters:

  • field: the field to extract (supports dot notation for nested fields)
  • filter: optional filter expression (same syntax as Filter)
  • sortField: field to sort by (empty for no sorting)
  • reverse: if true, sort in descending order

func (*Manager) Plugins

func (m *Manager) Plugins() []Plugin

Plugins returns a copy of the registered plugins.

func (*Manager) Posts

func (m *Manager) Posts() []*models.Post

Posts returns a copy of the posts slice.

func (*Manager) ProcessPostsConcurrently

func (m *Manager) ProcessPostsConcurrently(fn func(*models.Post) error) error

ProcessPostsConcurrently processes posts concurrently using the given function. This is useful for plugins that need to process multiple posts in parallel.

func (*Manager) RegisterPlugin

func (m *Manager) RegisterPlugin(p Plugin)

RegisterPlugin adds a plugin to the manager. Plugins are executed in registration order within the same priority level.

func (*Manager) RegisterPlugins

func (m *Manager) RegisterPlugins(plugins ...Plugin)

RegisterPlugins adds multiple plugins to the manager.

func (*Manager) Reset

func (m *Manager) Reset()

Reset clears the manager state, allowing stages to be run again.

func (*Manager) Run

func (m *Manager) Run() error

Run executes all lifecycle stages in order.

func (*Manager) RunTo

func (m *Manager) RunTo(stage Stage) error

RunTo executes lifecycle stages up to and including the specified stage. Already completed stages are skipped.

func (*Manager) SetCache

func (m *Manager) SetCache(cache Cache)

SetCache sets a custom cache implementation.

func (*Manager) SetConcurrency

func (m *Manager) SetConcurrency(n int)

SetConcurrency sets the concurrency level for parallel processing.

func (*Manager) SetConfig

func (m *Manager) SetConfig(config *Config)

SetConfig sets the configuration.

func (*Manager) SetFeeds

func (m *Manager) SetFeeds(feeds []*Feed)

SetFeeds sets the feeds slice.

func (*Manager) SetFiles

func (m *Manager) SetFiles(files []string)

SetFiles sets the discovered file paths.

func (*Manager) SetPosts

func (m *Manager) SetPosts(posts []*models.Post)

SetPosts sets the posts slice.

func (*Manager) Warnings

func (m *Manager) Warnings() []*HookError

Warnings returns collected non-critical errors.

type Plugin

type Plugin interface {
	// Name returns the unique name of the plugin.
	Name() string
}

Plugin is the base interface that all plugins must implement.

type PriorityPlugin

type PriorityPlugin interface {
	Plugin
	// Priority returns the plugin's priority for a given stage.
	// Lower values run first. Default priority is 0.
	// Use negative values for "tryfirst" behavior.
	// Use positive values for "trylast" behavior.
	Priority(stage Stage) int
}

PriorityPlugin can be implemented by plugins to control execution order within a stage. Plugins with lower priority values run first.

type RenderPlugin

type RenderPlugin interface {
	Plugin
	Render(m *Manager) error
}

RenderPlugin is implemented by plugins that participate in the render stage. This stage is used to convert markdown to HTML.

type Stage

type Stage string

Stage represents a lifecycle stage in the markata build process.

const (
	// StageConfigure loads config and initializes plugins.
	StageConfigure Stage = "configure"

	// StageValidate validates the configuration.
	StageValidate Stage = "validate"

	// StageGlob discovers content files.
	StageGlob Stage = "glob"

	// StageLoad parses files into posts.
	StageLoad Stage = "load"

	// StageTransform performs pre-render processing (jinja-md, etc.).
	StageTransform Stage = "transform"

	// StageRender converts markdown to HTML.
	StageRender Stage = "render"

	// StageCollect builds feeds and navigation.
	StageCollect Stage = "collect"

	// StageWrite writes output files.
	StageWrite Stage = "write"

	// StageCleanup releases resources.
	StageCleanup Stage = "cleanup"
)

func StagesBefore

func StagesBefore(s Stage) []Stage

StagesBefore returns all stages that come before the given stage.

func StagesUpTo

func StagesUpTo(s Stage) []Stage

StagesUpTo returns all stages up to and including the given stage.

type TransformPlugin

type TransformPlugin interface {
	Plugin
	Transform(m *Manager) error
}

TransformPlugin is implemented by plugins that participate in the transform stage. This stage is used for pre-render processing (jinja-md, etc.).

type ValidatePlugin

type ValidatePlugin interface {
	Plugin
	Validate(m *Manager) error
}

ValidatePlugin is implemented by plugins that participate in the validate stage. This stage is used to validate configuration before processing begins.

type WritePlugin

type WritePlugin interface {
	Plugin
	Write(m *Manager) error
}

WritePlugin is implemented by plugins that participate in the write stage. This stage is used to write output files.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL