liber

package module
v0.0.0-...-c10d344 Latest Latest
Warning

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

Go to latest
Published: Apr 22, 2026 License: MIT Imports: 10 Imported by: 0

README

liber

Go library for creating EPUB files

Description

  • Requires Go 1.26+
  • This library provides a high-level, ergonomic API for creating EPUB files (2.0.1).
  • It covers all epubcheck validations

Installation

go get -u codeberg.org/caskstrength/go-liber@latest

Example

package main

import (
  "log"
  "os"

  "codeberg.org/caskstrength/go-liber"
  "codeberg.org/caskstrength/go-liber/body"
  "codeberg.org/caskstrength/go-liber/ident"
  "codeberg.org/caskstrength/go-liber/lang"
  "codeberg.org/caskstrength/go-liber/reftype"
  "codeberg.org/caskstrength/go-liber/resource"
)

func main() {
  f, err := os.Create("book.epub")
  if err != nil {
	  panic(err)
  }
  defer f.Close()

  e := liber.EpubBuilder(
	  liber.MetadataBuilder("My Book", lang.Spanish, ident.Default()).
		  Creator("Johan Gambolputty").
		  Build(),
  ).
	  Stylesheet(body.Raw("body {}")).
	  CoverImage(resource.JpgFile("/path/cats.jpg")).
	  AddResources(resource.PngFile("/path/cs.png")).
	  AddContents(
		  liber.ContentBuilder(body.Raw(
          `<body>
              <h1>Chapter 1</h1>
              <h2 id="id01">Section 1.1</h2>
              <h2 id="id02">Section 1.1.1</h2>
              <h2 id="id03">Section 1.2</h2>
          </body>`,
          ), reftype.Text("Chapter 1")).
			  AddContentReferences(liber.ContentReferenceBuilder("Section 1.1").
				  AddChildren(liber.ContentReferenceBuilder("Section 1.1.1").Build()).
				  Build()).
			  AddContentReferences(liber.ContentReferenceBuilder("Section 1.2").Build()).
			  AddChildren(
				  liber.ContentBuilder(body.Raw("<body><h1>Chapter 2</h1></body>"), reftype.Text("Chapter 2")).Build(),
				  liber.ContentBuilder(body.Raw("<body><h1>Chapter 3</h1></body>"), reftype.Text("Chapter 3")).
					  AddChildren(liber.ContentBuilder(body.Raw("<body><h1>Chapter 4</h1></body>"), reftype.Text("Chapter 4")).
						  Build()).
					  Build(),
			  ).
			  Build(),
	  ).
	  Build()

  if err := liber.Create(&e, f); err != nil {
      os.Remove("book.epub")
	  log.Fatal(err)
  }
}

Details

  • Every content is a xhtml. The entire xhml text or only the body could be added as content (the latter is more practical and secure because follows the standard). See examples)
  • Content (Ex: Chapter) and ContentReference (Ex: Chapter#ref1) could be named with filename and id methods respectively. If none is set, Content will be sequential c{number}.xhtml (c01.xhtml, c02.xhtml...) and ContentReferences will be id{number} (id01, id02...) corresponding to the Content.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ContentBuilder

func ContentBuilder(body body.Body, rt reftype.ReferenceType) *contentBuilder

ContentBuilder initializes a new builder with the provided body and reference type.

func ContentReferenceBuilder

func ContentReferenceBuilder(title string) *contentReferenceBuilder

ContentReferenceBuilder initializes a new builder with a specific title.

func Create

func Create(e *epub.Epub, writer io.Writer) error

Create takes a populated Epub structure and writes the finalized EPUB file to the provided io.Writer using the internal output creator.

func EpubBuilder

func EpubBuilder(m epub.Metadata) *epubBuilder

EpubBuilder initializes a new builder with the provided metadata.

func MakeSlice

func MakeSlice[T interface {
	epub.Content | epub.ContentReference
}](t ...T) []T

This function allows the creation of a slice of Content or ContentReference. Both types are private to Liber so this could be useful to create slice and append values dynamically.

func MetadataBuilder

func MetadataBuilder(title string, l lang.Language, i ident.Identifier) *metadataBuilder

MetadataBuilder initializes a new builder with the mandatory Dublin Core elements: title, language, and a unique identifier (UUID or ISBN).

Types

This section is empty.

Directories

Path Synopsis
examples
basic command
custom command
internal

Jump to

Keyboard shortcuts

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