output

package
v0.8.0 Latest Latest
Warning

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

Go to latest
Published: Jul 23, 2026 License: MIT Imports: 29 Imported by: 0

Documentation

Overview

epub.go generates EPUB 3 ebooks. The resulting .epub file is a ZIP archive containing XHTML, CSS, OPF metadata, and both EPUB 3 navigation and NCX files for wider reader compatibility.

epub_cover.go draws the default EPUB cover image.

A book without a cover-image item shows up as a blank tile in Apple Books, Kobo and every other library UI. Rendering a raster cover would drag in an image toolchain; SVG is an EPUB 3 core media type, so the same title, subtitle and author the title page uses can be drawn directly.

epub_xhtml.go turns chapter HTML into well-formed XHTML for EPUB packaging.

EPUB reading systems parse chapter documents with a strict XML parser: a single unquoted attribute value or unbalanced tag makes the whole book unopenable. Chapter HTML can contain arbitrary author-written raw HTML, so the conversion is done by parsing the fragment into a DOM and re-serializing it under XHTML rules, rather than by patching the markup with regexes.

Package output implements non-PDF output generators such as HTML, ePub, and site.

output.go defines the shared output interfaces and registry. New output formats can be added through OutputFormat without changing the core build flow.

site.go generates a multi-page static site similar to GitBook. It includes sidebar navigation, previous/next links, search, and responsive layout.

site_assets.go turns embedded images back into files, and copies a project's static/ directory into the site output.

The chapter pipeline embeds local images as base64 data URIs, which is right for the single-file HTML and for Chrome's PDF rendering but wrong for a deployed site: a 1 MB screenshot becomes ~1.4 MB of markup *on every page that references it*, nothing is cacheable across pages, and loading="lazy" is a no-op on a data: URI so everything blocks first paint. Extracting the images here keeps the pipeline single-pass and confines the difference to the one format that needs it.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ChapterContent

type ChapterContent struct {
	// Title is the chapter title.
	Title string
	// ID is the unique chapter identifier.
	ID string
	// HTML is the chapter HTML without the outer document shell.
	HTML string
	// Filename is the suggested output filename, for example "ch_001.xhtml".
	Filename string
}

ChapterContent stores rendered content for a single chapter.

type DocumentMeta

type DocumentMeta struct {
	Title    string
	Author   string
	Language string
	Version  string
}

DocumentMeta stores document metadata.

type EpubChapter

type EpubChapter struct {
	Title     string
	ID        string
	Filename  string
	HTML      string // XHTML body content.
	SourceDir string // Source directory used to resolve relative asset paths.
	// Depth is the chapter's nesting level (0 = top level). Reading systems
	// render the navigation document as a tree, so a book using `sections:`
	// needs this to keep its hierarchy instead of showing one flat list.
	Depth int
}

EpubChapter stores one EPUB chapter.

type EpubGenerator

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

EpubGenerator builds an EPUB file.

func NewEpubGenerator

func NewEpubGenerator(meta EpubMeta) *EpubGenerator

NewEpubGenerator creates an ePub generator.

func (*EpubGenerator) AddChapter

func (g *EpubGenerator) AddChapter(ch EpubChapter)

AddChapter appends a chapter.

func (*EpubGenerator) Generate

func (g *EpubGenerator) Generate(outputPath string) error

Generate writes the EPUB file to disk.

func (*EpubGenerator) SetBookRoot added in v0.7.12

func (g *EpubGenerator) SetBookRoot(root string)

SetBookRoot sets the containment base directory used to resolve relative image paths. Relative images (including those above a chapter's own directory, such as a shared ../images) are packaged as long as they resolve inside this root. When unset, the common ancestor of all chapter source directories is used as the containment base.

func (*EpubGenerator) SetCSS

func (g *EpubGenerator) SetCSS(css string)

SetCSS sets the user's custom CSS. It is appended after the generator's own theme-derived stylesheet when writing OEBPS/style.css so custom rules win.

func (*EpubGenerator) SetTheme added in v0.7.14

func (g *EpubGenerator) SetTheme(thm *theme.Theme)

SetTheme sets the active document theme used to derive EPUB-appropriate styling. A nil theme leaves the explicitly set CSS as-is.

type EpubMeta

type EpubMeta struct {
	Title          string
	Subtitle       string
	Author         string
	Language       string
	Version        string
	Description    string
	IncludeCover   bool
	CoverImagePath string
	// CoverBackground is the configured cover background color (may be empty).
	CoverBackground string
	// Publishing metadata. Reading systems and library software index these:
	// without them a book shows up with no publisher, no publication date and
	// no subjects in every catalog it lands in.
	Publisher string
	ISBN      string
	// Date is the publication date, ideally ISO 8601 (YYYY or YYYY-MM-DD).
	Date     string
	Rights   string
	Subjects []string
}

EpubMeta contains EPUB metadata.

type HTMLGenerator

type HTMLGenerator struct{}

HTMLGenerator writes rendered HTML into a static site directory.

func NewHTMLGenerator

func NewHTMLGenerator() *HTMLGenerator

NewHTMLGenerator creates an HTML generator.

func (*HTMLGenerator) Generate

func (g *HTMLGenerator) Generate(fullHTML string, outputDir string, chapterHTMLs map[string]string) error

Generate writes the full HTML and optional chapter pages into a static site directory.

type OutputFormat

type OutputFormat interface {
	// Name returns the format name, for example "pdf", "html", "epub", or "site".
	Name() string

	// Generate writes output using the provided render request.
	Generate(ctx context.Context, req *RenderRequest, outputPath string) error

	// Description returns a short human-readable description.
	Description() string
}

OutputFormat is the shared interface implemented by each output backend.

type Registry

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

Registry stores registered output formats.

func NewRegistry

func NewRegistry() *Registry

NewRegistry creates an empty output format registry.

func (*Registry) Get

func (r *Registry) Get(name string) (OutputFormat, error)

Get returns an output format by name.

func (*Registry) Has

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

Has reports whether a format name is registered.

func (*Registry) List

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

List returns the names of all registered formats.

func (*Registry) Register

func (r *Registry) Register(f OutputFormat)

Register adds or replaces an output format implementation.

type RenderRequest

type RenderRequest struct {
	// FullHTML is the assembled full HTML document for PDF and standalone HTML output.
	FullHTML string

	// Chapters contains per-chapter content for ePub and site output.
	Chapters []ChapterContent

	// CSS is the merged theme CSS and custom CSS.
	CSS string

	// Meta contains document metadata.
	Meta DocumentMeta
}

RenderRequest contains all data needed to render any output format.

type SiteChapter

type SiteChapter struct {
	Title    string
	ID       string
	Filename string // Output HTML filename, for example "ch01.html".
	Content  string // Rendered HTML content.
	Markdown string // Source markdown after variable expansion.
	// SourcePath is the chapter's markdown source path relative to the book
	// root (e.g. "chapter01/section1.md"), used to build "edit this page"
	// links. When empty, the path is derived from Filename as a best effort.
	SourcePath string
	// Section is an optional group label rendered above this chapter in the
	// sidebar, from SUMMARY.md "## Heading" lines or book.yaml's `section:`.
	Section  string
	Depth    int
	Headings []SiteNavHeading
	Children []SiteChapter
}

SiteChapter stores rendered chapter data for site output.

type SiteGenerator

type SiteGenerator struct {
	Meta     SiteMeta
	Chapters []SiteChapter
	CSS      string // Theme CSS plus custom CSS.
	// BookRoot is the project directory. It is used to locate the optional
	// static/ directory whose contents are copied into the site root. Empty
	// disables that copy.
	BookRoot string
}

SiteGenerator generates the static site.

func NewSiteGenerator

func NewSiteGenerator(meta SiteMeta) *SiteGenerator

NewSiteGenerator creates a site generator.

func (*SiteGenerator) AddChapter

func (g *SiteGenerator) AddChapter(ch SiteChapter)

AddChapter appends a chapter.

func (*SiteGenerator) Generate

func (g *SiteGenerator) Generate(outputDir string) error

Generate generates the static site pages, sitemap, and search index.

func (*SiteGenerator) SetCSS

func (g *SiteGenerator) SetCSS(css string)

SetCSS sets the site CSS, sanitizing it to prevent style-tag breakout.

type SiteMeta

type SiteMeta struct {
	Title       string
	Subtitle    string
	Description string
	Author      string
	Language    string
	Theme       string // CSS theme name.
	// ThemeDescription is no longer rendered. It used to be the theme badge's
	// tooltip, which meant mdPress's own marketing copy about its themes sat
	// on the sidebar of every published site. Kept so existing callers still
	// compile; drop it together with the caller that sets it.
	ThemeDescription string
	// SiteURL is the public base URL of the deployed site (e.g.
	// https://user.github.io/repo). When set, an absolute-URL sitemap.xml is
	// generated. Empty disables the sitemap.
	SiteURL string
	// EditBase is the base URL for "edit this page" links (e.g.
	// https://github.com/user/repo/edit/main/). Empty disables the links.
	EditBase string

	// Branding. Each of these was previously unconfigurable: the favicon was
	// always mdPress's book emoji, there was no way to show a project logo,
	// and the footer and theme badge could only be removed with a CSS hack.
	//
	// Favicon and Logo are either a path relative to BookRoot (copied into the
	// site's asset directory) or an absolute URL / site-root path, used as-is.
	Favicon string
	// Copyright is rendered in each page's footer above the mdPress line.
	Copyright string
	// FooterHTML replaces the default "Built with mdPress" line. nil keeps the
	// default; a non-nil empty string removes the line altogether.
	FooterHTML *string
	// ShowThemeBadge renders the theme name in the sidebar. Off by default.
	ShowThemeBadge bool
}

SiteMeta stores site-wide metadata.

type SiteNavHeading struct {
	Title    string
	ID       string
	Children []SiteNavHeading
}

SiteNavHeading stores an in-chapter navigation tree.

Jump to

Keyboard shortcuts

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