ugarit

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

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

Go to latest
Published: May 21, 2017 License: MPL-2.0 Imports: 6 Imported by: 0

README

Package ugarit is a simple golang package to help e-book generating.

GoDoc

Ugarit is an eBook generator interface{}. Specific eBook formats and versions are handled by interface{} implementations on other packages. EPub 2.0/3.0 packages are provided in subdirectories. Although the uharit interface{} is thought to leave room for formats other than EPub, no efforts nor plans are in the way for them. Contributions are welcome!

This is a first barely usable version of an EPub eBook generator. It still has many improvements to be done. Work now is focused on generation of multi-level TOCs.

Documentation is poor but exists, check godoc button.

Example:

In your initialization code, you may set the log levels of debug messages:

   ...

   var err error
   var b ugarit.Book
   var target io.WriteCloser
   var gen ugarit.IndexGenerator
   var coverjpg *os.File
   var newId string

   ...

   newId = time.Now().Format("20060102150405")

   // The cover thumbnail is cached by iBooks and apparently, is referenced by
   // the epub filename. So, changing the filename when generating a new eBook
   // version (with a new cover) was the only way I found to make iBooks update
   // the book cover
   target, err = os.OpenFile("test-"+newId+".epub", os.O_CREATE|os.O_RDWR, 0600)
   if err != nil {
      fmt.Printf("Open error: %s\n", err)
      os.Exit(0)
   }

   b, err = epub30.New(
      target, // io.Writer where to save the epub contents
      []string{"My Amazing Book Title"}, // eBook title
      []string{"en"},                    // eBook language
      []string{newId},                   // eBook ID
      []epub30.Author{
         epub30.Author{Data: "Me"},
         epub30.Author{Data: "Myself"},
      }, // eBook author(s)
      []string{"MY Dear Publisher"},                  // eBook Publisher
      []epub30.Date{epub30.Date{Data: "2013-12-20"}}, // Date published
      epub30.Signature{},
      []epub30.Metatag{ // Any metatags here
         epub30.Metatag{Property: "dcterms:dateCopyrighted", Data: "9999-01-01"},
      },
      "ltr",              // PageProgression use "ltr" (left-to-right) or "rtl" (right-to-left)
      epub30.Versioner{}) // provide a versioner interface or use the package provided one
                          // which just generate a version string using the current time

   // Start the book first providing the cover
   coverjpg, err = os.Open("cover.jpg")
   if err != nil {
      fmt.Printf("Open jpeg error: %s\n", err)
      os.Exit(0)
   }

   _, _, err = b.AddCover(
      "cover.jpg",  // internal pathname where it will be stored
      "image/jpeg", // the mimetype
      coverjpg,     // the contents
      nil)
   if err != nil {
      fmt.Printf("AddCover error: %s\n", err)
      os.Exit(0)
   }

   // Then add pages to your book
   _, _, _, err = b.AddPage(
      "p1.xhtml",              // internal pathname where it will be stored
      "application/xhtml+xml", // the mimetype
      // the contents:
      // if provided it will be entirely read and closed
      // if not provided a new empty file will be created and an io.Writer will be returned
      strings.NewReader("<html xmlns=\"http://www.w3.org/1999/xhtml\"><head></head><body>hello</body></html>"),
      "", // The optional page id, it will be generated if left empty
      &epub30.EPubOptions{
         TOCTitle:     "Summary",       // TOC Title must be provided only once
         TOCItemTitle: "Hello Chapter", // Title for the page being added
      })
   if err != nil {
      fmt.Printf("AddPage error: %s\n", err)
      os.Exit(0)
   }

   // Create a TOC compatible with epub3
   gen, err = epub30.NewIndexGenerator()
   if err != nil {
      fmt.Printf("IndexGenerator 3.0 error: %s\n", err)
      os.Exit(0)
   }
   b.AddTOC(gen, "")

   // Create a TOC compatible with epub2
   gen, err = epub20.NewIndexGenerator(
      "en",  // Language
      newId, // Id
      "My Amazing Book Title", // Book title
      "Me", // Author
      b)    // The book object
   if err != nil {
      fmt.Printf("IndexGenerator 2.01 error: %s\n", err)
      os.Exit(0)
   }
   b.AddTOC(gen, "")

   // You MUST close the eBook, some important operations are done upon closing
   b.Close()


   ...

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrorInvalidOptionType error = errors.New("Invalid option type")
View Source
var ErrorInvalidPathname error = errors.New("Invalid pathname")
View Source
var ErrorReservedId error = errors.New("Reserved Id")
View Source
var ErrorTOCItemTitleNotFound error = errors.New("TOC item title not found")

Functions

This section is empty.

Types

type ArabicNumbering

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

func NewArabicNumbering

func NewArabicNumbering(prefix string, useHierarchy bool) ArabicNumbering

func (ArabicNumbering) Number

func (ss ArabicNumbering) Number(root string, number int) string

func (ArabicNumbering) Prefix

func (ss ArabicNumbering) Prefix() string

type Book

type Book interface {

	// AddPage must create/truncate the file specified by path,
	// register as having the provided mimetype and making to figure in the book index.
	// If src is nil, it has to return a valid io.Writer.
	// If src is not nil, it must copy its content to the file and return a nil io.Writer.
	// If path collides with a reserved path, it must return an error.
	// If the page is to be added to the TOC, it must return a TOCRef object pointing
	// to the TOC entry that refers to the added page, otherwise, TOCRef must be nil
	AddPage(path string, mimetype string, src io.Reader, id string, options interface{}) (string, io.Writer, TOCRef, error)

	// AddFile must create/truncate the file specified by path,
	// register as having the provided mimetype.
	// If src is nil, it has to return a valid io.WriteCloser.
	// If src is not nil, it must copy its contents to the file and return a nil io.Writer.
	// If path collides with a reserved path, it must return an error.
	AddFile(path string, mimetype string, src io.Reader, id string, options interface{}) (string, io.Writer, error)

	// AddTOC must create/truncate the TOC file.
	// The file generated must be in html format.
	// If IndexGenerator != nil,  the generated html nodes
	// must be passed through it before saved in the file.
	// If id!="" it must set the TOC id
	AddTOC(gen IndexGenerator, id string) (string, error)

	// AddCover must create/truncate the file specified by path,
	// register as having the provided mimetype and making it be the book cover.
	// If src is nil, it has to return a valid io.Writer.
	// If src is not nil, it must copy its contents to the file and return a nil io.Writer.
	// If path collides with a reserved path, it must return an error.
	AddCover(path string, mimetype string, src io.Reader, options interface{}) (string, io.Writer, error)

	// SpineAttr must set any spine attribute named as key with val
	// Error checking is allowed but it must fail silently.
	//  This method may be deprecated if someday it is considered
	// too much epub addicted
	SpineAttr(key, val string)

	// Closes the Ebook
	Close() error
}

type IndexGenerator

type IndexGenerator interface {
	// AddItem must append the item to the end of the table of contents.
	// The item is provided as a link (the HTML 'A' element) with the
	// href attribute pointing to the page file, the text content of the
	// element has the title of the page and, if provided when AddPage
	// called, the attribute id.
	AddItem(item *html.Node) error

	// GetDocument must return the entire TOC document
	GetDocument() (io.Reader, error)

	// GetMimeType returns the mimetype of the TOC file.
	GetMimeType() string

	// GetPathName returns the relative pathname of the TOC file
	GetPathName() string

	// GetPropertyValue returns the property value for the TOC
	// E.G. EPub 3.0 returns 'nav' and EPub 2.o returns ”
	GetPropertyValue() string

	// GetId returns the Id value for the TOC file in the manifest
	// E.G. EPub 3.0 returns 'nav' and EPub 2.o returns 'ncx'
	GetId() string
}

type LowerLetterNumbering

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

func NewLowerLetterNumbering

func NewLowerLetterNumbering(prefix string, useHierarchy bool) LowerLetterNumbering

func (LowerLetterNumbering) Number

func (ss LowerLetterNumbering) Number(root string, number int) string

func (LowerLetterNumbering) Prefix

func (ss LowerLetterNumbering) Prefix() string

type LowerRomanNumbering

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

func NewLowerRomanNumbering

func NewLowerRomanNumbering(prefix string, useHierarchy bool) LowerRomanNumbering

func (LowerRomanNumbering) Number

func (ss LowerRomanNumbering) Number(root string, number int) string

func (LowerRomanNumbering) Prefix

func (ss LowerRomanNumbering) Prefix() string

type SectionStyle

type SectionStyle interface {
	Prefix() string
	Number(root string, number int) string
}

type TOC

type TOC interface {
	TOCChild(n int) TOC
	TOCLen() int
}

type TOCItem

type TOCItem interface {
	ItemRef() string
	ItemTitle() string
	ContentRef() string
	SubSectionStyle(SectionStyle)
}

type TOCRef

type TOCRef interface {
	TOC
	TOCItem
}

type UpperLetterNumbering

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

func NewUpperLetterNumbering

func NewUpperLetterNumbering(prefix string, useHierarchy bool) UpperLetterNumbering

func (UpperLetterNumbering) Number

func (ss UpperLetterNumbering) Number(root string, number int) string

func (UpperLetterNumbering) Prefix

func (ss UpperLetterNumbering) Prefix() string

type UpperRomanNumbering

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

func NewUpperRomanNumbering

func NewUpperRomanNumbering(prefix string, useHierarchy bool) UpperRomanNumbering

func (UpperRomanNumbering) Number

func (ss UpperRomanNumbering) Number(root string, number int) string

func (UpperRomanNumbering) Prefix

func (ss UpperRomanNumbering) Prefix() string

type Versioner

type Versioner interface {
	// The versioner compliant object musst provide a Next method
	// which must provide a new unique version string each time
	// it is called
	Next() string
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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