extensions

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Feb 18, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CreateAdvancedEvaluateFunc

func CreateAdvancedEvaluateFunc(evalFunc func(*parser.ExtensionNode, ExtensionContext) (interface{}, error)) func(*parser.ExtensionNode, interface{}) (interface{}, error)

CreateAdvancedEvaluateFunc creates an evaluation function with enhanced context access

func CreateEvaluateFunc

func CreateEvaluateFunc(evalFunc func(*parser.ExtensionNode, runtime.Context) (interface{}, error)) func(*parser.ExtensionNode, interface{}) (interface{}, error)

CreateEvaluateFunc creates an evaluation function for extension nodes

func CreateTokensFromString

func CreateTokensFromString(input string) ([]*lexer.Token, error)

Simple helper function to create tokens for testing

Types

type BaseExtension

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

BaseExtension provides a base implementation for common extension functionality

func NewBaseExtension

func NewBaseExtension(name string, tags []string) *BaseExtension

NewBaseExtension creates a new base extension

func NewBlockExtension

func NewBlockExtension(name string, blockTags map[string]string) *BaseExtension

NewBlockExtension creates a new base extension with block tag support

func (*BaseExtension) AfterRender

func (e *BaseExtension) AfterRender(ctx ExtensionContext, templateName string, result interface{}, err error) error

AfterRender is called after template rendering completes

func (*BaseExtension) BeforeRender

func (e *BaseExtension) BeforeRender(ctx ExtensionContext, templateName string) error

BeforeRender is called before template rendering starts

func (*BaseExtension) Configure

func (e *BaseExtension) Configure(config map[string]interface{}) error

Configure sets configuration options for the extension

func (*BaseExtension) Dependencies

func (e *BaseExtension) Dependencies() []string

Dependencies returns a list of extension names this extension depends on

func (*BaseExtension) GetConfig

func (e *BaseExtension) GetConfig() map[string]interface{}

GetConfig returns the current configuration

func (*BaseExtension) GetConfigValue

func (e *BaseExtension) GetConfigValue(key string) (interface{}, bool)

GetConfigValue returns a specific configuration value

func (*BaseExtension) GetEndTag

func (e *BaseExtension) GetEndTag(tagName string) string

GetEndTag returns the corresponding end tag name for a block tag

func (*BaseExtension) IsBlockExtension

func (e *BaseExtension) IsBlockExtension(tagName string) bool

IsBlockExtension returns true if this extension handles block tags

func (*BaseExtension) Name

func (e *BaseExtension) Name() string

Name returns the extension name

func (*BaseExtension) OnLoad

func (e *BaseExtension) OnLoad(env ExtensionEnvironment) error

Lifecycle hook implementations (default no-op implementations) OnLoad is called when the extension is registered

func (*BaseExtension) OnUnload

func (e *BaseExtension) OnUnload() error

OnUnload is called when the extension is being removed (cleanup)

func (*BaseExtension) SetConfigValue

func (e *BaseExtension) SetConfigValue(key string, value interface{})

SetConfigValue sets a specific configuration value

func (*BaseExtension) Tags

func (e *BaseExtension) Tags() []string

Tags returns the handled tags

type CacheExtension

type CacheExtension struct {
	*BaseExtension
}

CacheExtension provides template fragment caching

func NewCacheExtension

func NewCacheExtension() *CacheExtension

NewCacheExtension creates a new cache extension

func (*CacheExtension) ParseTag

func (ce *CacheExtension) ParseTag(tagName string, parser ExtensionParser) (parser.Node, error)

ParseTag handles parsing of cache tags

type Extension

type Extension interface {
	// Name returns the unique name of this extension
	Name() string

	// Tags returns a list of tag names this extension handles
	Tags() []string

	// ParseTag is called when one of the extension's tags is encountered during parsing
	// It should parse the tag and return an ExtensionNode
	ParseTag(tagName string, parser ExtensionParser) (parser.Node, error)

	// IsBlockExtension returns true if this extension handles block tags (with end tags)
	IsBlockExtension(tagName string) bool

	// GetEndTag returns the corresponding end tag name for a block tag
	// For example, "highlight" -> "endhighlight"
	GetEndTag(tagName string) string

	// Configure sets configuration options for the extension
	Configure(config map[string]interface{}) error

	// GetConfig returns the current configuration
	GetConfig() map[string]interface{}

	// Lifecycle hooks
	// OnLoad is called when the extension is registered
	OnLoad(env ExtensionEnvironment) error

	// BeforeRender is called before template rendering starts
	BeforeRender(ctx ExtensionContext, templateName string) error

	// AfterRender is called after template rendering completes
	AfterRender(ctx ExtensionContext, templateName string, result interface{}, err error) error

	// OnUnload is called when the extension is being removed (cleanup)
	OnUnload() error

	// Dependencies returns a list of extension names this extension depends on
	Dependencies() []string
}

Extension defines the interface for template extensions

type ExtensionAwareParser

type ExtensionAwareParser struct {
	*parser.Parser
	// contains filtered or unexported fields
}

ExtensionAwareParser extends the main parser with extension support

func NewExtensionAwareParser

func NewExtensionAwareParser(tokens []*lexer.Token, registry *Registry) *ExtensionAwareParser

NewExtensionAwareParser creates a parser with extension support

func (*ExtensionAwareParser) Parse

func (eap *ExtensionAwareParser) Parse() (*parser.TemplateNode, error)

Parse parses the tokens into a template AST with extension support

func (*ExtensionAwareParser) ParseTopLevelPublic

func (eap *ExtensionAwareParser) ParseTopLevelPublic() (parser.Node, error)

ParseTopLevelPublic overrides the parent method to handle custom tags

type ExtensionContext

type ExtensionContext interface {
	// GetVariable retrieves a variable from the context
	GetVariable(name string) (interface{}, bool)

	// SetVariable sets a variable in the context
	SetVariable(name string, value interface{})

	// GetGlobal retrieves a global variable
	GetGlobal(name string) (interface{}, bool)

	// ApplyFilter applies a filter to a value
	ApplyFilter(name string, value interface{}, args ...interface{}) (interface{}, error)

	// CallMacro calls a macro
	CallMacro(name string, args []interface{}, kwargs map[string]interface{}) (interface{}, error)
}

ExtensionContext provides context access for extensions during evaluation

type ExtensionEnvironment

type ExtensionEnvironment interface {
	// GetExtension retrieves another extension by name
	GetExtension(name string) (Extension, bool)

	// AddGlobal adds a global variable
	AddGlobal(name string, value interface{})

	// AddFilter adds a custom filter
	AddFilter(name string, filter interface{}) error

	// AddTest adds a custom test
	AddTest(name string, test interface{}) error

	// GetConfig gets environment-level configuration
	GetConfig(key string) (interface{}, bool)

	// SetConfig sets environment-level configuration
	SetConfig(key string, value interface{})
}

ExtensionEnvironment provides access to the template environment for extensions

type ExtensionError

type ExtensionError struct {
	ExtensionName string
	TagName       string
	TemplateName  string
	Line          int
	Column        int
	Message       string
	Cause         error
}

ExtensionError provides context-aware error messages for extensions

func NewExtensionError

func NewExtensionError(extensionName, message string, cause error) *ExtensionError

NewExtensionError creates a new extension error with context

func NewExtensionParseError

func NewExtensionParseError(extensionName, tagName, templateName string, line, column int, message string, cause error) *ExtensionError

NewExtensionParseError creates a new extension error with parse context

func NewExtensionTagError

func NewExtensionTagError(extensionName, tagName, message string, cause error) *ExtensionError

NewExtensionTagError creates a new extension error with tag context

func (*ExtensionError) Error

func (ee *ExtensionError) Error() string

func (*ExtensionError) Unwrap

func (ee *ExtensionError) Unwrap() error

type ExtensionNode

type ExtensionNode = parser.ExtensionNode

Type alias to make function signatures cleaner

type ExtensionParser

type ExtensionParser interface {
	// Current returns the current token
	Current() *lexer.Token

	// Advance moves to the next token and returns it
	Advance() *lexer.Token

	// Check returns true if the current token matches the given type
	Check(tokenType lexer.TokenType) bool

	// CheckAny returns true if the current token matches any of the given types
	CheckAny(types ...lexer.TokenType) bool

	// Peek returns the current token without advancing
	Peek() *lexer.Token

	// IsAtEnd returns true if we've reached the end of tokens
	IsAtEnd() bool

	// ParseExpression parses an expression and returns the node
	ParseExpression() (parser.ExpressionNode, error)

	// ParseToEnd parses all tokens until the specified end tag
	ParseToEnd(endTag string) ([]parser.Node, error)

	// ParseBlock parses a block extension body until the end tag
	ParseBlock(endTag string) ([]parser.Node, error)

	// Error creates a parser error with the given message
	Error(message string) error

	// ExpectBlockEnd ensures the current token is a block end (%})
	ExpectBlockEnd() error

	// ExpectEndTag consumes the end tag block
	ExpectEndTag(endTag string) error

	// NewExtensionNode creates a new extension node
	NewExtensionNode(extensionName, tagName string, line, column int) *parser.ExtensionNode

	// ParseArguments parses arguments until block end
	ParseArguments() ([]parser.ExpressionNode, error)
}

ExtensionParser provides parsing utilities for extensions

type HelloExtension

type HelloExtension struct {
	*BaseExtension
}

HelloExtension provides a simple hello tag

func NewHelloExtension

func NewHelloExtension() *HelloExtension

NewHelloExtension creates a new hello extension

func (*HelloExtension) ParseTag

func (he *HelloExtension) ParseTag(tagName string, parser ExtensionParser) (parser.Node, error)

ParseTag handles parsing of hello tags

type HighlightExtension

type HighlightExtension struct {
	*BaseExtension
}

HighlightExtension provides syntax highlighting functionality

func NewHighlightExtension

func NewHighlightExtension() *HighlightExtension

NewHighlightExtension creates a new highlight extension

func (*HighlightExtension) ParseTag

func (he *HighlightExtension) ParseTag(tagName string, parser ExtensionParser) (parser.Node, error)

ParseTag handles parsing of highlight tags

type ParserAdapter

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

ParserAdapter adapts the main parser for use by extensions

func NewParserAdapter

func NewParserAdapter(p *parser.Parser) *ParserAdapter

NewParserAdapter creates a new parser adapter

func (*ParserAdapter) Advance

func (pa *ParserAdapter) Advance() *lexer.Token

Advance moves to the next token and returns it

func (*ParserAdapter) Check

func (pa *ParserAdapter) Check(tokenType lexer.TokenType) bool

Check returns true if the current token matches the given type

func (*ParserAdapter) CheckAny

func (pa *ParserAdapter) CheckAny(types ...lexer.TokenType) bool

CheckAny returns true if the current token matches any of the given types

func (*ParserAdapter) Current

func (pa *ParserAdapter) Current() *lexer.Token

Current returns the current token

func (*ParserAdapter) Error

func (pa *ParserAdapter) Error(message string) error

Error creates a parser error with the given message

func (*ParserAdapter) ExpectBlockEnd

func (pa *ParserAdapter) ExpectBlockEnd() error

ExpectBlockEnd ensures the current token is a block end (%})

func (*ParserAdapter) ExpectEndTag

func (pa *ParserAdapter) ExpectEndTag(endTag string) error

ExpectEndTag consumes the end tag block

func (*ParserAdapter) IsAtEnd

func (pa *ParserAdapter) IsAtEnd() bool

IsAtEnd returns true if we've reached the end of tokens

func (*ParserAdapter) NewExtensionNode

func (pa *ParserAdapter) NewExtensionNode(extensionName, tagName string, line, column int) *parser.ExtensionNode

NewExtensionNode creates a new extension node

func (*ParserAdapter) ParseArguments

func (pa *ParserAdapter) ParseArguments() ([]parser.ExpressionNode, error)

Helper method to parse arguments until block end

func (*ParserAdapter) ParseBlock

func (pa *ParserAdapter) ParseBlock(endTag string) ([]parser.Node, error)

ParseBlock parses a block extension body until the end tag

func (*ParserAdapter) ParseExpression

func (pa *ParserAdapter) ParseExpression() (parser.ExpressionNode, error)

ParseExpression parses an expression and returns the node

func (*ParserAdapter) ParseToEnd

func (pa *ParserAdapter) ParseToEnd(endTag string) ([]parser.Node, error)

ParseToEnd parses all tokens until the specified end tag

func (*ParserAdapter) Peek

func (pa *ParserAdapter) Peek() *lexer.Token

Peek returns the current token without advancing

type Registry

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

Registry manages all registered extensions

func NewRegistry

func NewRegistry() *Registry

NewRegistry creates a new extension registry

func (*Registry) AfterRender

func (r *Registry) AfterRender(ctx ExtensionContext, templateName string, result interface{}, renderErr error) error

AfterRender calls AfterRender on all registered extensions

func (*Registry) BeforeRender

func (r *Registry) BeforeRender(ctx ExtensionContext, templateName string) error

BeforeRender calls BeforeRender on all registered extensions

func (*Registry) GetAllExtensions

func (r *Registry) GetAllExtensions() []Extension

GetAllExtensions returns all registered extensions

func (*Registry) GetDependencies

func (r *Registry) GetDependencies(name string) []string

GetDependencies returns the dependencies for a given extension

func (*Registry) GetExtension

func (r *Registry) GetExtension(name string) (Extension, bool)

GetExtension returns the extension with the given name

func (*Registry) GetExtensionForTag

func (r *Registry) GetExtensionForTag(tagName string) (Extension, bool)

GetExtensionForTag returns the extension that handles the given tag

func (*Registry) GetLoadOrder

func (r *Registry) GetLoadOrder() []string

GetLoadOrder returns the order in which extensions were loaded

func (*Registry) IsCustomTag

func (r *Registry) IsCustomTag(tagName string) bool

IsCustomTag returns true if the tag is handled by an extension

func (*Registry) Register

func (r *Registry) Register(extension Extension) error

Register registers an extension with dependency checking and lifecycle hooks

func (*Registry) SetEnvironment

func (r *Registry) SetEnvironment(env ExtensionEnvironment)

SetEnvironment sets the environment reference for the registry

func (*Registry) Unregister

func (r *Registry) Unregister(name string) error

Unregister removes an extension and calls its OnUnload hook

type SimpleTimestampExtension

type SimpleTimestampExtension struct {
	*BaseExtension
}

SimpleTimestampExtension provides basic timestamp functionality

func NewSimpleTimestampExtension

func NewSimpleTimestampExtension() *SimpleTimestampExtension

NewSimpleTimestampExtension creates a new simple timestamp extension

func (*SimpleTimestampExtension) ParseTag

func (ste *SimpleTimestampExtension) ParseTag(tagName string, parser ExtensionParser) (parser.Node, error)

ParseTag handles parsing of timestamp tags

type VersionExtension

type VersionExtension struct {
	*BaseExtension
	// contains filtered or unexported fields
}

VersionExtension provides version information

func NewVersionExtension

func NewVersionExtension(version string) *VersionExtension

NewVersionExtension creates a new version extension

func (*VersionExtension) ParseTag

func (ve *VersionExtension) ParseTag(tagName string, parser ExtensionParser) (parser.Node, error)

ParseTag handles parsing of version tags

Jump to

Keyboard shortcuts

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