Documentation
¶
Overview ¶
Package sg is a static site generator that reads markdown and HTML pages with YAML frontmatter, applies Go HTML templates, and outputs a rendered site. It supports both one-shot rendering (OfflineRenderer) and watch-mode rendering (OnlineRenderer) with filesystem watching and a built-in HTTP server.
Index ¶
- Constants
- Variables
- func InjectLiveReload(next http.Handler) http.Handler
- func SetLogger(l *zap.SugaredLogger)
- func ToChannel(ctx RenderContext, s *Site) rss.Channel
- func ToItem(ctx RenderContext, p *Page) (rss.Item, error)
- type ColorFormatter
- type Config
- type LiveReload
- type Message
- type MsgKind
- type OfflineRenderer
- type OnlineRenderer
- type OutputFormatter
- type Page
- type PlainFormatter
- type RenderContext
- func (ctx RenderContext) PageReference(slug string) (*Page, error)
- func (ctx RenderContext) Pages() []*Page
- func (ctx RenderContext) RenderFeed() ([]byte, error)
- func (ctx RenderContext) RenderPage(p *Page) ([]byte, error)
- func (ctx RenderContext) SlugURL(slug string) (string, error)
- func (ctx RenderContext) Tags() []string
- func (ctx RenderContext) WriteFeed(fs *fsutil) error
- type Renderer
- type Set
- type Site
- func (s *Site) AddPage(p *Page)
- func (s *Site) AddTemplate(key string, byts []byte) error
- func (s *Site) PageByPath(path string) (*Page, error)
- func (s *Site) PageBySlug(slug string) (*Page, error)
- func (s *Site) Pages() []*Page
- func (s *Site) RemovePageByPath(path string) *Page
- func (s *Site) Tag(tag string) ([]*Page, error)
- func (s *Site) Tags() []string
- func (s *Site) Template(key string) (*template.Template, error)
- func (s *Site) Type(typ string) ([]*Page, error)
Constants ¶
const LiveReloadScript = `` /* 356-byte string literal not displayed */
LiveReloadScript is the JavaScript snippet injected into served HTML pages. It connects to the WebSocket endpoint and reloads the page on receiving a message.
Variables ¶
var ( // Rendering errors ErrTemplateNotFound = errors.New("template not found") ErrSlugNotFound = errors.New("slug not found") ErrPathNotFound = errors.New("path not found") ErrTagNotFound = errors.New("tag not found") ErrTypeNotFound = errors.New("type not found") // Page validation/parsing errors. ErrMissingSlug = errors.New("invalid slug") ErrMissingType = errors.New("invalid type") ErrMissingPath = errors.New("missing path") ErrMissingTitle = errors.New("missing title") ErrMissingFrontmatter = errors.New("missing frontmatter") // Frontmatter delimiter FrontmatterDelimiter = []byte("---") )
Functions ¶
func InjectLiveReload ¶
InjectLiveReload wraps an http.Handler to inject the LiveReload script into HTML responses before the closing </body> tag.
func SetLogger ¶
func SetLogger(l *zap.SugaredLogger)
SetLogger sets the package-level logger. If l is nil, a no-op logger is used.
Types ¶
type ColorFormatter ¶
type ColorFormatter struct {
// contains filtered or unexported fields
}
ColorFormatter prints colored, symbol-prefixed output.
func NewColorFormatter ¶
func NewColorFormatter() *ColorFormatter
func (*ColorFormatter) FormatError ¶
func (f *ColorFormatter) FormatError(err error)
func (*ColorFormatter) FormatMessage ¶
func (f *ColorFormatter) FormatMessage(m Message)
type Config ¶
type Config struct {
InputDir string
OutputDir string
UseLocalRootUrl bool // if true, use the local root url for serving
QuiescentSecs int // period to wait before re-rendering pages on a template change
Logger *zap.SugaredLogger // optional; nil means no-op
Drafts bool // if true, include draft pages in the rendered output
Port int // port for the local server; used to construct localRootUrl
}
Config holds the configuration for site rendering.
type LiveReload ¶
type LiveReload struct {
// contains filtered or unexported fields
}
LiveReload manages WebSocket connections for automatic browser refresh.
func NewLiveReload ¶
func NewLiveReload() *LiveReload
NewLiveReload creates a new LiveReload instance.
func (*LiveReload) Reload ¶
func (lr *LiveReload) Reload()
Reload sends a reload message to all connected WebSocket clients.
func (*LiveReload) ServeHTTP ¶
func (lr *LiveReload) ServeHTTP(w http.ResponseWriter, r *http.Request)
ServeHTTP handles the WebSocket upgrade handshake using the standard library.
type OfflineRenderer ¶
type OfflineRenderer struct {
Config Config
}
OfflineRenderer renders a site in one-shot mode.
func (*OfflineRenderer) Render ¶
func (r *OfflineRenderer) Render() error
Renders the site and returns any error generated during rendering. Renders in one-shot and returns when rendering is complete.
type OnlineRenderer ¶
type OnlineRenderer struct {
Config Config
// OnReload is called after a successful render. Use this to trigger
// LiveReload notifications.
OnReload func()
// Formatter controls how watch-mode messages are printed. If nil, a
// PlainFormatter is used.
Formatter OutputFormatter
// contains filtered or unexported fields
}
Renders a site in online mode. Watches the input directory for changes and renders them as files are changed. When a template changes, all pages are re-rendered currently. Re-rendering after a template change waits for a quiescence period to allow time for intital rendering of the site.
TODO: only re-render when a page is affected by the template change.
func (*OnlineRenderer) Render ¶
func (r *OnlineRenderer) Render() (err error)
Start rendering. Watches the filesystem for any updates and renders updates as they arrive. Does not return unless rendering failed to start.
type OutputFormatter ¶
OutputFormatter formats watch-mode messages and errors for display.
type Page ¶
type Page struct {
// Required fields.
FilePath string // filename relative to the input directory
UrlPath string // url relative to the site root
Slug string `yaml:"slug"`
Title string `yaml:"title"`
Type string `yaml:"type"`
IsRaw bool
OrderHint int
// Fields for markdown pages.
Draft bool `yaml:"draft"`
Date time.Time `yaml:"date"`
Tags []string `yaml:"tags"`
Ordering string `yaml:"ordering"`
Description string `yaml:"description"`
// contains filtered or unexported fields
}
Page represents a single page in the site. Markdown pages are parsed from files with YAML frontmatter; raw pages (HTML, images, etc.) are copied directly.
func (*Page) Content ¶
Return the content of the page, converted to HTML. Reads page from filesystem.
func (*Page) RawContent ¶
Get the raw content from the file. Reads the file each call.
type PlainFormatter ¶
type PlainFormatter struct {
// contains filtered or unexported fields
}
PlainFormatter prints plain-text output (no color, no symbols).
func NewPlainFormatter ¶
func NewPlainFormatter() *PlainFormatter
func (*PlainFormatter) FormatError ¶
func (f *PlainFormatter) FormatError(err error)
func (*PlainFormatter) FormatMessage ¶
func (f *PlainFormatter) FormatMessage(m Message)
type RenderContext ¶
type RenderContext struct {
Site *Site
// Used for rendering a single page; set by RenderPage.
Page *Page
// contains filtered or unexported fields
}
Context within which a page is rendered and defines top-level functions that can be called from the template.
func (RenderContext) PageReference ¶
func (ctx RenderContext) PageReference(slug string) (*Page, error)
Get a page, given a slug.
func (RenderContext) RenderFeed ¶
func (ctx RenderContext) RenderFeed() ([]byte, error)
func (RenderContext) RenderPage ¶
func (ctx RenderContext) RenderPage(p *Page) ([]byte, error)
func (RenderContext) SlugURL ¶
func (ctx RenderContext) SlugURL(slug string) (string, error)
URL for a given slug.
func (RenderContext) WriteFeed ¶
func (ctx RenderContext) WriteFeed(fs *fsutil) error
type Renderer ¶
type Renderer interface {
Render() error
}
Renderer is the interface for site renderers.
type Set ¶
type Set[T comparable] struct { sync.RWMutex // contains filtered or unexported fields }
Set is a generic, concurrency-safe set implementation.
func NewSet ¶
func NewSet[T comparable]() *Set[T]
type Site ¶
type Site struct {
sync.Mutex
Title string `yaml:"title"`
RootUrl string `yaml:"root_url"`
Description string `yaml:"description"`
// Relative to Site root, optional.
FeedUrl string `yaml:"feed_url"`
FeedTag string `yaml:"feed_tag"`
// contains filtered or unexported fields
}
Site holds the state of a parsed site, including all pages, templates, and indexes by slug, path, tag, and type. It is safe for concurrent use.
func (*Site) AddTemplate ¶
Add a template keyed by the given string.
func (*Site) PageByPath ¶
Lookup page by input path.
func (*Site) PageBySlug ¶
Lookup page by slug.
func (*Site) RemovePageByPath ¶
Remove the page with the given path and return the removed page. If page does not exist, returns nil.