ssg

package module
v0.0.0-...-35c0d5d Latest Latest
Warning

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

Go to latest
Published: Jun 28, 2025 License: MIT Imports: 16 Imported by: 1

README

ssg

A toolkit for Static Site Generators

The Document Pipeline -- What API to use?

THe SSG is a series of transformations on a document.

|-------------------|-----------|-------------|

Transformer Output Input
Golang template io.Writer []byte
Golang gzip, tar io.Writer io.Writer
Goldmark Markdown io.Writer []byte
JSON Encoder io.Writer any
tdewolff/minify io.Writer io.Reader
GoHTML []byte []byte
enescakir/emoji string string

Ok output is easy: Use io.Writer.

For input there are more choices.

If potentially very large (mega or gigebytes or more) or if it could come in vyer slowly (over a network), making a pure io.Write (or io.WriteCloser), like gzip and tar do. This is not the use case here.

Many transformers take a []byte or string. However if use io.Reader:

  • We can use streaming transformers as is.
  • We can do a nifty buffer swap

src :=  bytes.Buffer{}
dest := bytes.Buffer{}

for _,tx := range pipeline {
	tx(dest, src)
	src, dest = dest, src
	dest.reset()
}

No need for channels or go routines.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var MetaHeadEmail = MetaHeadType{
	Name:        "email",
	Prefix:      []byte(""),
	Suffix:      []byte("\n\n\n"),
	KeepMarkers: false,
}
View Source
var MetaHeadJson = MetaHeadType{
	Name:        "json",
	Prefix:      []byte("{\n"),
	Suffix:      []byte("\n}\n"),
	KeepMarkers: true,
}
View Source
var MetaHeadToml = MetaHeadType{
	Name:        "toml",
	Prefix:      []byte("+++\n"),
	Suffix:      []byte("\n+++\n"),
	KeepMarkers: false,
}
View Source
var MetaHeadYaml = MetaHeadType{
	Name:        "yaml",
	Prefix:      []byte("---\n"),
	Suffix:      []byte("\n---\n"),
	KeepMarkers: false,
}

Functions

func ApplyTransforms

func ApplyTransforms(k string, v any, tx []ValueTransformer) (any, error)

func ConfigDefault

func ConfigDefault(config *SiteConfig)

func EmailMarshal

func EmailMarshal(data map[string]any) ([]byte, error)

func EmailUnmarshal

func EmailUnmarshal(src []byte, out map[string]any, tx ...ValueTransformer) error

func Identity

func Identity(wr io.Writer, src io.Reader, data any) error

func Joiner

func Joiner(head MetaHeadType, meta []byte, body []byte) []byte

func LoadContent

func LoadContent(config SiteConfig, out *[]ContentSourceConfig) error

func Main2

func Main2(config SiteConfig, pages *[]ContentSourceConfig) error

func MetaSplitEmail

func MetaSplitEmail(s []byte) ([]byte, []byte)

func MetaSplitJson

func MetaSplitJson(s []byte) ([]byte, []byte)

func MetaSplitToml

func MetaSplitToml(s []byte) ([]byte, []byte)

func MetaSplitYaml

func MetaSplitYaml(s []byte) ([]byte, []byte)

func MultiRender

func MultiRender(renders []Renderer, initial []byte, data any) error

func Set

func Set(kv map[string]any, k string, v any, tx []ValueTransformer) error

func Splitter

func Splitter(head MetaHeadType, s []byte) ([]byte, []byte)

Types

type ContentSourceConfig

type ContentSourceConfig map[string]any

Default ContentSource -- it's just a map any with some specific accessors

func MetaParseJson

func MetaParseJson(s []byte) (ContentSourceConfig, error)

MetaParseJson that reads front matter as JSON, using the golang stdlib, and returns a map[string]any type.

func (ContentSourceConfig) Get

func (csc ContentSourceConfig) Get(key string) string

func (ContentSourceConfig) InputFile

func (csc ContentSourceConfig) InputFile() string

func (ContentSourceConfig) OutputFile

func (csc ContentSourceConfig) OutputFile() string

func (ContentSourceConfig) TemplateName

func (csc ContentSourceConfig) TemplateName() string

type ContentSplitter

type ContentSplitter func(s []byte) ([]byte, []byte)

Splits Input into metadata and the main content/body

type MetaHeadType

type MetaHeadType struct {
	Name        string
	Prefix      []byte
	Suffix      []byte
	KeepMarkers bool
}

type MetaParser

type MetaParser func(s []byte) (ContentSourceConfig, error)

MetaParser parses the front matter and returns a content source

func MetaParseEmail

func MetaParseEmail(tx ...ValueTransformer) MetaParser

MetaParseEmail parsed input as "email headers" (better name TBD)

type Renderer

type Renderer func(wr io.Writer, src io.Reader, data any) error

"Template Macro" processes source and writing output

"data" is optional supplemental data.  It is just passed on to the

underlying implementation. If not used, then data can be nil.

func Must

func Must(r Renderer, err error) Renderer

func NewPageRender

func NewPageRender(tdir string, fns template.FuncMap) (Renderer, error)

func NewTemplateMacro

func NewTemplateMacro(funcs template.FuncMap) Renderer

create a simple macro maker. You pass-in whatever funcs.

each page is parsed as a text/template then executed

func ToBytes

func ToBytes(buf *bytes.Buffer) Renderer

func WriteOutput

func WriteOutput(outdir string) Renderer

type SiteConfig

type SiteConfig struct {
	BaseTemplate string

	ContentDir string

	MetaSplit  ContentSplitter
	MetaParser MetaParser

	InputExt    string // ".md"
	OutputExt   string // ".html"
	IndexSource string // "index.md"
	IndexDest   string // "index.html"

	Pipeline []Renderer
}

type TemplateRouter

type TemplateRouter map[string]*template.Template

func (TemplateRouter) ExecuteTemplate

func (t TemplateRouter) ExecuteTemplate(wr io.Writer, name string, data any) error

type ValueTransformer

type ValueTransformer func(k string, v any) (any, error)

func AsList

func AsList(key string) ValueTransformer

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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