plugin

package
v0.12.3 Latest Latest
Warning

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

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

Documentation

Overview

Package plugin provides canonical type definitions for the huan plugin system.

Package plugin provides canonical type definitions for the huan plugin system.

Both huan internal code and .so plugins import these types, solving Go's cross-module type assertion problem for interface-based capability discovery.

Plugin is the minimal base interface every plugin satisfies. Capability interfaces (e.g. pkg/plugin.PostBuildHook, pkg/plugin.ThemePlugin) embed Plugin and add domain-specific methods. The Registry holds plugins keyed by Name(); Find[T] returns the subset implementing a given capability.

Package plugin provides canonical type definitions for the huan plugin system.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Find

func Find[T any](r *Registry) []T

Find returns all registered plugins implementing capability T, in registration order. T is typically a capability interface such as pkg/plugin.PostBuildHook or pkg/plugin.ThemePlugin.

Example:

hooks := plugin.Find[pkgplugin.PostBuildHook](registry)
for _, h := range hooks { ... }

Types

type ExportFailure added in v0.11.1

type ExportFailure struct {
	Item ExportItem
	Err  string
}

ExportFailure pairs a failed unit with its error.

type ExportItem added in v0.11.1

type ExportItem struct {
	// Path is the output file path relative to the project root.
	Path string
	// Lang is "zh" or "en".
	Lang string
	// Format is "epub", "pdf", or "docx".
	Format string
	// Slug of the source book/practice (empty for complete collections).
	Slug string
}

ExportItem describes one generated artifact.

type ExportRequest added in v0.11.1

type ExportRequest struct {
	// SourceDir is the project root containing huan.yaml and content/.
	SourceDir string

	// Type filters content: "books", "practices", "all".
	Type string

	// Format filters output: "epub", "pdf", "docx", "all".
	Format string

	// Level filters granularity: "individual", "volumes", "complete", "all".
	// ("volumes" and "seasons" are aliases; normalized here to "volumes".)
	Level string

	// Slug restricts to a single book/practice slug (optional).
	Slug string

	// Volume restricts to one volume/season number, 1-based (optional; 0 = all).
	Volume int

	// Force regenerates even when the manifest hash matches.
	Force bool

	// Jobs is the parallelism for per-book generation. 0 = runtime.NumCPU()-1.
	Jobs int
}

ExportRequest carries invocation-time parameters from the CLI.

type ExportResult added in v0.11.1

type ExportResult struct {
	Succeeded []ExportItem
	Failed    []ExportFailure
	// Skipped lists items skipped by the incremental manifest (hash match).
	Skipped []ExportItem
	// Warnings carries non-fatal notes (e.g. missing .en.md sidecars).
	Warnings []string
}

ExportResult reports the batch outcome.

type Exporter added in v0.11.1

type Exporter interface {
	Plugin

	// Export runs the ebook generation batch. Implementations should:
	//   - Honor ctx for cancellation.
	//   - Return a Result with per-item Succeeded/Failed/Skipped lists even on
	//     partial failure (collection-not-interruption).
	//   - Return a non-nil error only when the export cannot proceed at all
	//     (invalid request, missing content root, missing CJK font for pdf).
	Export(ctx context.Context, req ExportRequest) (ExportResult, error)
}

Exporter is the capability interface for plugins that transform site content into offline document formats (epub/pdf/docx). It mirrors the cross-.so sharing pattern of deploy.Deployer: the contract lives under pkg/ so out-of-tree .so plugins import the SAME types as the host binary, making exporters discoverable via plugin.Find[Exporter].

type FieldSchema

type FieldSchema struct {
	Key         string // 字段名,对应 yaml key
	Type        string // "string" | "int" | "bool" | "string_slice" | "map"
	Required    bool   // true = 必填,启动时校验
	Default     any    // 默认值(Required=false 时生效)
	Description string // 人类可读的说明
	Sensitive   bool   // true = 在 CLI info 中 mask 为 ***
	EnvVarHint  string // 建议的环境变量名,仅用于文档提示
}

FieldSchema describes a single config field.

type MetadataProvider

type MetadataProvider interface {
	PluginMetadata() PluginMeta
}

MetadataProvider is an optional interface plugins can implement to provide their metadata. Used by the LifecycleManager.List() and Admin/CLI UI.

type Plugin

type Plugin interface {
	// Name is the plugin's unique identifier. It matches the yaml key under
	// plugins: (e.g. Name()=="seo_injector" pairs with yaml plugins.seo_injector.*).
	Name() string
}

Plugin is the base interface every plugin satisfies. Capability interfaces embed Plugin and add methods.

Plugin intentionally has only Name(): config injection happens via the plugin's constructor (e.g. seoinjector.New(cfg)), not via an Init method.

type PluginMeta

type PluginMeta struct {
	Version    string   `json:"version"`
	Author     string   `json:"author"`
	RepoURL    string   `json:"repoURL"`
	License    string   `json:"license"`
	Tags       []string `json:"tags"`
	IsOfficial bool     `json:"isOfficial"`
}

PluginMeta carries human-readable metadata for a plugin.

type PostBuildHook added in v0.8.0

type PostBuildHook interface {
	Plugin

	// OnOutputWritten is called after all output files are written, before the
	// build result is finalized. Receives the output directory for
	// post-processing (e.g. inject SEO meta, enhance sitemap, inject HTML).
	// Collection-not-interruption: a returned error logs a warning and does
	// not abort the build.
	OnOutputWritten(ctx context.Context, outputDir string) error
}

PostBuildHook is the .so-facing build hook. Only OnOutputWritten crosses a .so boundary usefully: page-mutating hooks would receive an opaque interface{} the plugin cannot inspect (content.Page is not importable from pkg/ or .so plugin modules). Rich page-level hooks stay in internal/build.Hook, which is compiled-in only.

type Registry

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

Registry holds plugins keyed by Name(). The order slice preserves registration order for deterministic iteration in Find[T] and All.

func NewRegistry

func NewRegistry() *Registry

NewRegistry returns an empty Registry.

func (*Registry) All

func (r *Registry) All() []Plugin

All returns all registered plugins in registration order. The returned slice is a copy; callers may mutate it without affecting the registry.

func (*Registry) Get

func (r *Registry) Get(name string) (Plugin, bool)

Get returns the plugin with the given name and a found flag.

func (*Registry) Names

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

Names returns all registered plugin names in registration order.

func (*Registry) Register

func (r *Registry) Register(p Plugin) error

Register adds a plugin to the registry. Returns an error if a plugin with the same Name() is already registered — duplicate registration is treated as a programming error rather than silently overwritten.

func (*Registry) SortedNames

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

SortedNames returns registered plugin names in lexicographic order. Useful for CLI listing where deterministic alphabetical output is preferred over registration order.

func (*Registry) Unregister

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

Unregister removes a plugin by name. Returns false if the name wasn't registered. After Unregister, the plugin is no longer returned by Get, All, Names, or Find[T].

type Schema

type Schema struct {
	Fields []FieldSchema
}

Schema describes the full config shape a plugin expects.

type SchemaProvider

type SchemaProvider interface {
	ConfigSchema() Schema
}

SchemaProvider is an optional interface plugins can implement to declare their config schema. Used by the registry for config validation.

type ShortcodeContext

type ShortcodeContext struct {
	Params map[string]string
	Inner  string
}

ShortcodeContext carries the parameters and context for a shortcode invocation.

type ShortcodeHandler

type ShortcodeHandler func(ctx ShortcodeContext) (string, error)

ShortcodeHandler is a function that renders a shortcode.

type ShortcodeProvider

type ShortcodeProvider interface {
	Shortcodes() map[string]ShortcodeHandler
}

ShortcodeProvider is an optional interface that themes can implement to register custom shortcodes.

type TemplateEntry

type TemplateEntry struct {
	Path    string // Logical path, e.g. "index.html"
	Content string // Template content
}

TemplateEntry describes a single template file.

type ThemeHooks

type ThemeHooks interface {
	BeforeRender(ctx context.Context) error
	AfterRender(ctx context.Context) error
}

ThemeHooks is an optional interface that themes can implement to inject lifecycle hooks into the render pipeline.

type ThemeInfo

type ThemeInfo struct {
	Name        string   `json:"name"`
	Version     string   `json:"version"`
	Author      string   `json:"author"`
	Description string   `json:"description"`
	Screenshot  string   `json:"screenshot,omitempty"`
	Tags        []string `json:"tags,omitempty"`
	MinHuanVer  string   `json:"minHuanVer,omitempty"`
}

ThemeInfo carries theme metadata.

type ThemePlugin

type ThemePlugin interface {
	Plugin

	// Info returns the theme's metadata as a key-value map.
	Info() map[string]any

	// Templates returns the list of templates the theme provides.
	// Each entry has "path" and "content" keys.
	Templates() []map[string]string

	// FuncMap returns the theme's custom template functions.
	FuncMap() template.FuncMap

	// Assets returns the theme's static asset filesystem.
	Assets() fs.FS
}

ThemePlugin is the core capability interface for theme plugins. Themes provide templates, template functions, and static assets for site rendering.

Note: Methods use interface{} and built-in types (not concrete structs) so that .so plugins loaded via Go's plugin.Open can satisfy this interface across module boundaries. Concrete struct types would be different named types in each module and fail type assertions.

Jump to

Keyboard shortcuts

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