renderer

package
v2.0.0 Latest Latest
Warning

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

Go to latest
Published: Aug 27, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package renderer renders the given AST to certain formats.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config[W any, C any] struct {
	// contains filtered or unexported fields
}

A Config struct holds configuration for Renderer.

type Context

type Context interface {
	// Get returns a value associated with the given key.
	Get(ContextKey) any

	// ComputeIfAbsent computes a value if a value associated with the given key is absent and returns the value.
	ComputeIfAbsent(ContextKey, func() any) any

	// Set sets the given value to the context.
	Set(ContextKey, any)

	// Render renders the given node using the renderer associated with this context.
	// If no rendering function has been set, it is a no-op and returns nil.
	Render(w any, source []byte, n ast.Node) error

	// Clone returns a new Context that is a copy of this context.
	Clone() Context
}

A Context interface holds information that is necessary to render Markdown text.

func NewContext

func NewContext(opts ...ContextOption) Context

NewContext returns a new rendering Context.

type ContextKey

type ContextKey int

ContextKey is a key that is used to set arbitrary values to the rendering context.

func NewContextKey

func NewContextKey() ContextKey

NewContextKey returns a new ContextKey value.

type ContextOption

type ContextOption func(*renderContext)

ContextOption is a functional option for NewContext.

func WithRenderFunc

func WithRenderFunc(f func(any, []byte, ast.Node, Context) error) ContextOption

WithRenderFunc sets the rendering function used by Context.Render.

type Extension

type Extension[C any] interface {
	// RendererOptions returns the options for the Renderer.
	RendererOptions(*C) []Option[C]
}

Extension is an interface that represents an extension for Renderer.

type Helper

type Helper[W any, C any] struct {
	// contains filtered or unexported fields
}

Helper is a helper struct for implementing Renderer.

func (*Helper[W, C]) Config

func (r *Helper[W, C]) Config() *C

Config returns the configuration of this Helper.

func (*Helper[W, C]) Render

func (r *Helper[W, C]) Render(w W, source []byte, n ast.Node, opts ...RenderOption) error

Render renders the given AST node to the given writer with the given Renderer.

func (*Helper[W, C]) RenderStringSource

func (r *Helper[W, C]) RenderStringSource(w W, source string, n ast.Node, opts ...RenderOption) error

RenderStringSource renders the given AST node to the given writer with the given Renderer and string source.

type HelperBuilder

type HelperBuilder[W any, C any] struct {
	// contains filtered or unexported fields
}

HelperBuilder is a builder for creating a Helper.

func (*HelperBuilder[W, C]) Build

func (b *HelperBuilder[W, C]) Build() *Helper[W, C]

Build builds a Helper with the given options.

func (*HelperBuilder[W, C]) OnBeforeRender

func (b *HelperBuilder[W, C]) OnBeforeRender(
	f func(w W, source []byte, n ast.Node, rc Context) error) *HelperBuilder[W, C]

OnBeforeRender sets a callback function that is called before rendering process starts.

func (*HelperBuilder[W, C]) Options

func (b *HelperBuilder[W, C]) Options(opts ...Option[C]) *HelperBuilder[W, C]

Options sets the options for the HelperBuilder.

type NodeRenderer

type NodeRenderer[W any] interface {
	Render(w W, source []byte, n ast.Node, entering bool, rc Context) (ast.WalkStatus, error)
}

A NodeRenderer interface is used for rendering a given node.

func NodeRendererFunc

func NodeRendererFunc[W any](f func(w W, source []byte,
	n ast.Node, entering bool, rc Context) (ast.WalkStatus, error)) NodeRenderer[W]

NodeRendererFunc is a function that implements NodeRenderer interface.

type NodeRendererDecorator

type NodeRendererDecorator[W any] = func(next NodeRenderer[W]) NodeRenderer[W]

A NodeRendererDecorator is a function type used for decorating a NodeRenderer.

type Option

type Option[C any] interface {
	SetFormatOption(*C)
}

Option is a functional option for configuring a Renderer, typically passed to a format-specific constructor (e.g. html.New).

func NewOptionFunc

func NewOptionFunc[C any](f func(*C)) Option[C]

NewOptionFunc returns a new Option that applies the given function to the configuration.

func WithExtensions

func WithExtensions[W any, C any](ext ...Extension[C]) Option[C]

WithExtensions sets the extensions for the Renderer.

func WithNodeRenderer

func WithNodeRenderer[W any, C any](kind ast.NodeKind, nodeRenderer NodeRenderer[W]) Option[C]

WithNodeRenderer sets a node renderer for the given node kind.

func WithNodeRendererDecorator

func WithNodeRendererDecorator[W any, C any](kind ast.NodeKind, decorator NodeRendererDecorator[W]) Option[C]

WithNodeRendererDecorator sets a node renderer decorator for the given node kind.

If a decorator is already set for the node kind, the new decorator will be applied to the existing one.

func WithNodeRendererDecorators

func WithNodeRendererDecorators[W any, C any](
	nodeRendererDecorators map[ast.NodeKind]NodeRendererDecorator[W]) Option[C]

WithNodeRendererDecorators sets the node renderer decorators for the Renderer.

If a decorator is already set for a node kind, the new decorator will be applied to the existing one.

func WithNodeRenderers

func WithNodeRenderers[W any, C any](nodeRenderers map[ast.NodeKind]NodeRenderer[W]) Option[C]

WithNodeRenderers sets the node renderers for the Renderer.

type RenderOption

type RenderOption interface {
	SetRenderOption(*renderConfig)
}

RenderOption is an interface that represents an option for rendering.

func WithContext

func WithContext(context Context) RenderOption

WithContext sets the context for rendering.

type Renderer

type Renderer[W any] interface {
	Render(w W, source []byte, n ast.Node, opts ...RenderOption) error
	RenderStringSource(w W, source string, n ast.Node, opts ...RenderOption) error
}

A Renderer interface is used for rendering a given AST node to a certain format.

Directories

Path Synopsis
Package html implements renderer that outputs HTMLs.
Package html implements renderer that outputs HTMLs.

Jump to

Keyboard shortcuts

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