epub

package
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Oct 9, 2026 License: MIT Imports: 41 Imported by: 0

Documentation

Overview

Package epub writes HTML presentation documents as EPUB 3 publications.

EPUB (Electronic Publication) is an open e-book format built on web standards. An EPUB is a ZIP archive that contains publication metadata, navigation, and XHTML content documents that readers can reflow for different screens. Commonly supported HTML tags include a, article, aside, blockquote, br, code, div, em, figure, h1-h6, hr, img, li, ol, p, pre, section, span, strong, table, and ul.

## Considerations

- <https://willcrichton.net/notes/portable-epubs/#epub-content%2FEPUB%2Findex.xhtml$>

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func New

func New(ctx context.Context, w io.Writer, source []byte, node ast.Node, withOptions ...Option) error

New renders a parsed Markdown AST as EPUB 3 and writes the archive to w. source must be the byte slice used to parse node with mdcoach.NewParser.

func NewSources

func NewSources(
	ctx context.Context,
	w io.Writer,
	sources []presentation.Source,
	withOptions ...Option,
) error

NewSources renders parsed Markdown sources together into one EPUB 3 archive. Each source should be parsed with mdcoach.NewParser; source paths are used to resolve relative images unless media options provide a filesystem or path.

Types

type Image

type Image struct {
	Location string
	Hash     string
	Data     []byte
	Width    int
	Height   int
}

Image stores a PNG-encoded image and its source metadata.

func NewImage

func NewImage(location string, source stdImage.Image) (*Image, error)

NewImage encodes source as PNG and records its dimensions and content hash.

type ImageCache

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

ImageCache stores PNG images by source location and content hash.

func NewImageCache

func NewImageCache() *ImageCache

NewImageCache creates an empty, concurrency-safe image cache.

func (*ImageCache) Get

func (c *ImageCache) Get(location string) (*Image, bool)

Get returns the cached image at location, if present.

func (*ImageCache) Set

func (c *ImageCache) Set(image *Image)

Set adds image to the cache by its content hash and source location.

type ImageLoader

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

ImageLoader loads, resizes, and caches images as PNG files.

func NewImageLoader

func NewImageLoader(cache *ImageCache, options MediaOptions) *ImageLoader

NewImageLoader creates a loader that caches PNG images.

func (*ImageLoader) LoadImage

func (l *ImageLoader) LoadImage(ctx context.Context, location string) (*Image, error)

LoadImage loads location, returning a cached image when available.

func (*ImageLoader) LoadImages

func (l *ImageLoader) LoadImages(ctx context.Context, source []byte, tree ast.Node) error

LoadImages downloads image destinations owned by nodes in the Markdown AST.

type MediaOptions

type MediaOptions struct {
	FS          fs.FS
	Path        string
	WidthLimit  int
	HeightLimit int
	HTTPClient  *http.Client
}

MediaOptions configures an ImageLoader's filesystem and output dimensions.

type Option

type Option func(*newOptions) error

Option configures AST-to-EPUB rendering.

func WithAuthor

func WithAuthor(author string) Option

WithAuthor sets the EPUB author.

func WithEmoteFS

func WithEmoteFS(assets fs.FS) Option

WithEmoteFS sets the filesystem used to resolve emote PNGs.

func WithMediaOptions

func WithMediaOptions(media MediaOptions) Option

WithMediaOptions configures local and remote image loading.

func WithMetadata

func WithMetadata(metadata presentation.Frontmatter) Option

WithMetadata sets the EPUB publication metadata.

func WithTitle

func WithTitle(title string) Option

WithTitle sets the EPUB title.

Jump to

Keyboard shortcuts

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