medusa

package module
v0.3.1 Latest Latest
Warning

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

Go to latest
Published: Apr 26, 2025 License: MIT Imports: 10 Imported by: 0

README

WARNING: This package is still in it's infancy. Breaking features/fixes will be introduced and bugs will be prevalent.

medusa

a static site generator based on transformer functions

This SSG works by chaining together transformer functions. It is early in development, so expect: bugs, poor design choices, and breaking changes.

Concurrency is not implemented at the moment. I might explore it in the future.

package medusa

// ...imports...

func main() {
	b := medusa.NewBuilder()
	b.Source("./src")
	b.Destination("./build")

	// add values to global store
	b.Use(metadata.New(
		map[string]any{
			"title":       "My Blog",
			"description": "The blog where I blog.",
		},
	))

	// adds "Collections" to global store
	b.Use(collections.New(collections.CollectionConfig{
		Name: "Blog",
		Store: map[string]any{
			"heading": "My awesome posts!",
		},
		Patterns: []string{"blog/*.md"},
	}))

	// transpile all md to html
	b.Use(markdown.New())

	// apply layouts to content
	b.Use(layouts.New(layouts.Config{
		LayoutPatterns:  []string{"template/*"},
		ContentPatterns: []string{"**/*.html"},
	}))

	// run all transformer functions
	err := b.Build()
	if err != nil {
		panic(err)
	}
}

Metalsmith

This project is heaviliy inspired by and modelled after Metalsmith.

Support

Please feel free to send an email to ~relay/medusa@lists.sr.ht to submit patches or ask questions.

Documentation

Overview

Pacakge medusa is a very simple static site generator that works by chaining functions together. When Builder.Build is called, It reads all the files in the source directory and represents them all as a slice of File. A pointer to this slice, along with a pointer to the global Store, is passed to every Transformer sequencially. At the end of the chain, it writes the state of the File slice to the destination.

Each file also has a Frontmatter field where yaml/toml/json frontmatter is parsed and stored. The builder returns ErrFrontmatter if it failes to parse fronmatter of a file. Frontmatter parsing can be skipped via Config.

Configuration defaults generally rely on Go's zero values (e.g., false for booleans, nil for pointers, "" for strings), with specific overrides in NewBuilder for fields like WorkingDir (defaults to "./") and Logger (defaults to a discard logger).

Example Usage

package main

import (
	"log"
	"log/slog"
	"os"

	"git.sr.ht/~relay/medusa"
)

func main() {
	// Example with custom configuration
	config := medusa.Config{
		Logger: slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{
			Level: slog.LevelDebug,
		})),
		AllowOverwrite: true, // Skip overwrite prompts
	}
	b := medusa.NewBuilder(config)

	// Example with default configuration
	// b := medusa.NewBuilder()

	b.Source("./src")
	b.Destination("./build")

	// Add transformers if needed
	// b.Use(...)

	err := b.Build()
	if err != nil {
		panic(err)
	}
}

Index

Constants

This section is empty.

Variables

View Source
var ErrDestinationExists = errors.New("destination directory exists and overwrite not permitted")

ErrDestinationExists indicates that the destination directory exists and overwriting was not explicitly allowed via Config.AllowOverwrite.

Functions

This section is empty.

Types

type Builder

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

func NewBuilder

func NewBuilder(optionalConfig ...Config) *Builder

Creates a new builder struct. The config parameter is optional. If more than one config is passed, it uses the first one. Defaults are applied based on Go's zero values where applicable: - WorkingDir: Defaults to "./" if empty. - AllowOverwrite: Defaults to false. If true, allows overwriting the destination. - Logger: Defaults to a discard logger if nil. - SkipFrontmatterParsing: Defaults to false.

func (*Builder) Build

func (b *Builder) Build() error

Build applies the transformers in the stack to the contents of every file in the source directory, and writes them to the destination. It returns ErrDestinationExists if the destination directory exists and Config.AllowOverwrite is false.

func (*Builder) Destination

func (b *Builder) Destination(destination string)

Defines the destination directory, relative to WorkingDir as specified in Config.

func (*Builder) Source

func (b *Builder) Source(source string)

Defines the source directory, relative to WorkingDir as specified in Config.

func (*Builder) Use

func (b *Builder) Use(transformer Transformer)

Adds a transformer function to the stack.

type Config

type Config struct {
	// Defines the working directory.
	// It is used to find the source and
	// destination directory.
	//
	// Optional. Defaults to the current directory ("./") if empty.
	WorkingDir string

	// Whether prompts like
	// "overwrite warnings" should be skipped.
	//
	// Optional. Defaults to false.
	AllowOverwrite bool

	// Defines which logger to use.
	//
	// Optional. Defaults to a discard logger if nil.
	Logger *slog.Logger

	// Whether frontmatter parsing should be skipped.
	//
	// Optional. Defaults to false.
	SkipFrontmatterParsing bool
}

type ErrFrontmatter

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

func (ErrFrontmatter) Error

func (e ErrFrontmatter) Error() string

type File

type File struct {
	Path     string
	FileInfo fs.FileInfo
	Store    Store

	// the yaml/toml/json frontmatter of the file
	Frontmatter Store
	// contains filtered or unexported fields
}

func (*File) Content

func (f *File) Content() []byte

Get the contents of the file

func (*File) SetContent

func (f *File) SetContent(bytes []byte)

Set the contents of the file

type Store

type Store map[string]any

Used by transformers to hold arbitrary data.

type Transformer

type Transformer func(files *[]File, store *Store) error

A function that change files and a global store as a part of a larger chain.

func ErrTransformer

func ErrTransformer(err error) Transformer

Returns empty transformer with error

Directories

Path Synopsis
transformers
collections
Package collections is a medusa transformer that group files together in collections.
Package collections is a medusa transformer that group files together in collections.
layouts
Package layouts provides a Medusa transformer for applying Go html/template layouts and partials to content files.
Package layouts provides a Medusa transformer for applying Go html/template layouts and partials to content files.

Jump to

Keyboard shortcuts

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