theme

package
v0.11.0 Latest Latest
Warning

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

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

Documentation

Overview

Package theme implements go-pretty-pdf's theme engine: a set of professional built-in themes plus a customization layer (colors, fonts, section toggles, density) that composes on top of them via CSS custom properties, and a YAML format for user-defined custom themes.

Index

Constants

View Source
const (
	NameDefault    = "default"
	NameMinimal    = "minimal"
	NameModern     = "modern"
	NameClassic    = "classic"
	NameCorporate  = "corporate"
	NameDark       = "dark"
	NameAcademic   = "academic"
	NameEditorial  = "editorial"
	NameSepia      = "sepia"
	NameTerminal   = "terminal"
	NameBlueprint  = "blueprint"
	NameIvy        = "ivy"
	NameGovernment = "government"
	NameResume     = "resume"
	NameLegal      = "legal"
	NameLatex      = "latex"
	NameGruvbox    = "gruvbox"
)

Builtin theme names, usable with Get/ResolveByName and as extends: values in custom theme YAML.

View Source
const ChromaClassPrefix = "chroma-"

ChromaClassPrefix is the CSS class prefix the mdx parser's syntax highlighter must use for code-block token spans, so the class names it emits line up with the stylesheet chromaCSSFor generates here.

View Source
const ThemeFileSuffix = ".theme.yml"

ThemeFileSuffix is the required suffix for custom theme files.

Variables

This section is empty.

Functions

func BoolPtr added in v0.4.0

func BoolPtr(b bool) *bool

BoolPtr is a small helper for constructing Sections literals, e.g. theme.Sections{Cover: theme.BoolPtr(false)}.

func ExtractCSSVars added in v0.8.0

func ExtractCSSVars(css string) map[string]string

ExtractCSSVars parses every `--pdf-<name>: <value>;` custom property *declaration* out of css (a single theme's CSS, or a fully composed document's <style> block) into a name->value map. It only matches declarations, never var(--pdf-x, ...) usages, since those have no colon right after the property name.

When a property is declared more than once (e.g. Resolve() appends a :root{} override block after the base theme CSS for user-customized colors), the last occurrence wins — matching normal CSS cascade order for rules of equal specificity.

func FindCustom added in v0.4.0

func FindCustom(cwd, name string) (string, bool)

FindCustom looks for a "<name>.theme.yml" file, checking the project directory first and then the global one.

func ProjectThemesDir added in v0.4.0

func ProjectThemesDir(cwd string) string

ProjectThemesDir returns the project-local themes directory, searched before the global one.

func ResolveByNameForEPUB added in v0.10.0

func ResolveByNameForEPUB(name string, opts Options, cwd string) (string, error)

ResolveByNameForEPUB resolves a theme by name (same dispatch as ResolveByName) and returns its EPUB-ready CSS.

func ResolveForEPUB added in v0.10.0

func ResolveForEPUB(t Theme, opts Options) (string, error)

ResolveForEPUB builds the final CSS for theme t customized by opts, using the EPUB structural skeleton (relative units, no print-only rules) instead of the PDF base stylesheet. The CSS is assembled as: optional Google Fonts @import, the EPUB base stylesheet, the theme's own CSS, and a :root override block for any customized colors/fonts/density.

Unlike Resolve, this does not return ResolvedSections — EPUB has its own cover-image mechanism and nav.xhtml for navigation, so the PDF section toggles (cover, TOC, page numbers, header) do not apply.

func ScaffoldYAML added in v0.4.0

func ScaffoldYAML(name string, from Theme) string

ScaffoldYAML returns a starter .theme.yml file for `theme new`, extending the given base theme with every field left blank (so it inherits the base's own values) and commented hints for what to fill in.

func UserThemesDir added in v0.4.0

func UserThemesDir() (string, error)

UserThemesDir returns the global, cross-project directory custom themes can be installed into (e.g. ~/.config/pretty-pdf/themes on Linux).

Types

type Colors added in v0.4.0

type Colors struct {
	Primary    string `yaml:"primary"`
	Accent     string `yaml:"accent"`
	Text       string `yaml:"text"`
	Muted      string `yaml:"muted"`
	Background string `yaml:"background"`
}

Colors overrides the CSS custom properties a theme's palette is built from. Empty fields fall back to the theme's own defaults.

type CustomTheme added in v0.4.0

type CustomTheme struct {
	Name        string   `yaml:"name"`
	Description string   `yaml:"description"`
	Extends     string   `yaml:"extends"`
	Colors      Colors   `yaml:"colors"`
	Fonts       Fonts    `yaml:"fonts"`
	Sections    Sections `yaml:"sections"`
	Density     Density  `yaml:"density"`
	CSS         string   `yaml:"css"`
}

CustomTheme is the schema for a user-defined `<name>.theme.yml` file. It extends a builtin theme by name and overrides its colors, fonts, section toggles, and density, with an escape hatch for raw CSS appended last.

func LoadCustomTheme added in v0.4.0

func LoadCustomTheme(path string) (*CustomTheme, error)

LoadCustomTheme reads and parses a .theme.yml file.

func (*CustomTheme) Resolve added in v0.4.0

func (c *CustomTheme) Resolve(opts Options) (string, ResolvedSections, error)

Resolve builds the final CSS for a custom theme. The theme's own YAML fields (colors, fonts, sections, density) act as defaults; opts (usually CLI flags or go-pretty-pdf.yml's theme_options) take priority whenever a field is explicitly set. The theme's raw css field, if any, is appended last so it always wins.

func (*CustomTheme) ResolveForEPUB added in v0.10.0

func (c *CustomTheme) ResolveForEPUB(opts Options) (string, error)

ResolveForEPUB builds the final EPUB CSS for a custom theme. Same merge semantics as Resolve, but uses the EPUB structural skeleton instead of the PDF one and does not return ResolvedSections.

type CustomThemeInfo added in v0.4.0

type CustomThemeInfo struct {
	Name   string
	Path   string
	Global bool
}

CustomThemeInfo describes a discovered custom theme file.

func ListCustom added in v0.4.0

func ListCustom(cwd string) ([]CustomThemeInfo, error)

ListCustom enumerates every custom theme visible from cwd: project-local themes first, then global ones (a project theme shadows a global theme of the same name).

type Density added in v0.4.0

type Density string

Density adjusts overall spacing/line-height. The empty string means "normal" (the theme's own defaults, no adjustment).

const (
	DensityCompact Density = "compact"
	DensityNormal  Density = "normal"
	DensityRelaxed Density = "relaxed"
)

type Fonts added in v0.4.0

type Fonts struct {
	Heading       string   `yaml:"heading"`
	Body          string   `yaml:"body"`
	Code          string   `yaml:"code"`
	GoogleImports []string `yaml:"google_fonts"`
}

Fonts overrides the font-family custom properties a theme uses. Empty fields fall back to the theme's own defaults. GoogleImports is only honored when Options.AllowNetworkFonts is true.

type Options added in v0.4.0

type Options struct {
	Colors            Colors
	Fonts             Fonts
	Sections          Sections
	Density           Density
	AllowNetworkFonts bool
}

Options customizes a Theme at resolve time: colors, fonts, section toggles, density, and whether network-fetched (Google) fonts are allowed.

type ResolvedSections added in v0.4.0

type ResolvedSections struct {
	Cover       bool
	TOC         bool
	PageNumbers bool
	Header      bool
}

ResolvedSections is Sections after defaults have been applied — every field has a concrete value.

func Resolve added in v0.4.0

func Resolve(t Theme, opts Options) (string, ResolvedSections, error)

Resolve builds the final CSS for theme t customized by opts, and returns the section toggles after defaults have been applied. The CSS is assembled as: optional Google Fonts @import, the shared base stylesheet, the theme's own CSS, a :root override block for any customized colors/fonts/density, and CSS for any disabled sections (cover/TOC). Page numbers and the running header are not CSS-controlled; callers wire ResolvedSections.PageNumbers/Header into render.Options.

func ResolveByName added in v0.4.0

func ResolveByName(name string, opts Options, cwd string) (string, ResolvedSections, error)

ResolveByName resolves a theme by name — a builtin ("default", "corporate", ...), the name of a custom theme discovered in the project-local or global themes directory, or a direct path to a ".theme.yml"/".css" file — and returns its final CSS plus the resolved section toggles.

type Sections added in v0.4.0

type Sections struct {
	Cover       *bool `yaml:"cover"`
	TOC         *bool `yaml:"toc"`
	PageNumbers *bool `yaml:"page_numbers"`
	Header      *bool `yaml:"header"`
}

Sections toggles document sections on or off. A nil pointer means "use the theme's default"; a non-nil pointer always wins.

type Theme

type Theme struct {
	Name        string
	Description string
	Category    string
	CSS         string
	// Sections holds this theme's own section defaults (all true unless a
	// theme has a good reason to ship differently).
	Sections ResolvedSections
	// Accented marks themes that use their accent color as a bold,
	// structural design element (a border on the cover/heading, an
	// accent-colored blockquote, etc.) rather than reserving it for links
	// only. Consumers that want to echo a theme's visual identity outside
	// the PDF itself (e.g. the docs website's theme switcher) can read this
	// instead of guessing from the CSS or hardcoding a list.
	Accented bool
}

Theme is a built-in (or synthetic) theme: a name plus the CSS that implements its palette/typography and any structural deltas on top of the shared base stylesheet.

func Get added in v0.4.0

func Get(name string) (Theme, bool)

Get looks up a builtin theme by name.

func List added in v0.4.0

func List() []Theme

List returns every builtin theme in a stable, curated order.

Jump to

Keyboard shortcuts

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