mobi

package module
v0.0.0-...-7268a21 Latest Latest
Warning

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

Go to latest
Published: Dec 17, 2022 License: MIT Imports: 10 Imported by: 0

README

Go MOBI

Github Actions CI Go Report Card Go Reference

This package implements facilities to create KF8-formatted MOBI and AZW3 books. We also export the raw PalmDB writer and various PalmDoc, MOBI and KF8 components as subpackages, which can be used to implement other formats that build on these standards.

This fork

This fork was created from the original to add support for vertical right-to-left text (and also vertical left-to-right text for completeness). (in addition to support for horizontal RTL which seems to be in the code, though I haven't tested it and didn't modify it's behavior in this fork). This allows this library to generate japanese azw3 ebooks with the same text orientation as in typical Japanese printed novels to enjoy them on a Kindle reader.

Also, I added ExtraHead field to Book to allow adding e.g. extra styles directly to the generated html files.

Usage

In order to get vertical RTL text, use mobi.Book's RightToLeft=true and Vertical=true:

  mb := mobi.Book{
    // ...
    RightToLeft: true,
    Vertical:    true,
  }

In order to get emphasis markers from yomukaku work use the following for Book's

  mb := mobi.Book{
    // ...
    ExtraHead: `
      <style> 
        .emphasisDots {
          font-weight: normal;
          text-emphasis-style: dot;
          -webkit-text-emphasis-style: dot;
          }
      </style>`
  }
Possible improvements of this fork

It would probably be cleaner to introduce the necessary styles as CSS files and refer to them in each html file using Book.CSSFlows instead of this dirty yet easy hack in templating.go. The same goes for injecting e.g. the style for emphasisDots class via ExtraHead.

Known issues

  • Chapters are supported but subchapters are not
  • Old readers without KF8 are not supported (Kindle 1, 2 and DX)
  • Books without any text content are always malformed
  • Errors during template expansion result in a panic

References

Huge thanks to the authors of these resources.

License

MIT © Leo Gaskin 2020-2022

Documentation

Overview

Package mobi implements writing KF8-style formatted MOBI and AZW3 books.

Index

Examples

Constants

This section is empty.

Variables

View Source
var SupportedLocales []language.Tag

SupportedLocales is a list of locales supported by MOBI, intended for use with Go language matching facilities.

BUG(leotaku): While the MOBI language format supports regions, this implementation does not and explicitly removes region information from the locale stored in the generated MOBI file.

Functions

This section is empty.

Types

type Book

type Book struct {
	Title         string
	Authors       []string
	Contributors  []string
	Publisher     string
	Subject       string
	CreatedDate   time.Time
	PublishedDate time.Time
	DocType       string
	Language      language.Tag
	FixedLayout   bool
	RightToLeft   bool
	Vertical      bool
	Chapters      []Chapter
	CSSFlows      []string
	Images        []image.Image
	CoverImage    image.Image
	ThumbImage    image.Image
	UniqueID      uint32
	ExtraHead     string
	// contains filtered or unexported fields
}

Book represents all the information necessary to generate a KF8-style formatted MOBI or AZW3 book.

You are expected to initialize the Book struct using the required variables and/or builder pattern, then convert the resulting structure into a PalmDB database. This database can then be written out to any io.Writer.

Example
// Create simple book with chapter
ch := mobi.Chapter{
	Title:  "Chapter 1",
	Chunks: mobi.Chunks(`Lorem ipsum dolor sit amet, consetetur sadipscing elitr.`),
}
mb := mobi.Book{
	Title:       "De vita Caesarum librus",
	Authors:     []string{"Sueton"},
	CreatedDate: time.Now(),
	Language:    language.Italian,
	Chapters:    []mobi.Chapter{ch},
	UniqueID:    rand.Uint32(),
}

// Convert book to PalmDB database
db := mb.Realize()

// Write database to file
f, _ := os.Create("test.azw3")
defer f.Close()
err := db.Write(f)
if err != nil {
	panic(err)
}
Output:

func (Book) GetThumbFilename

func (m Book) GetThumbFilename() string

GetThumbFilename returns the filename a JPEG image in the Kindle thumbnails folder would need to have in order to be correctly associated with the generated book.

Newer Kindle readers no longer respect the thumbnail embedded in the Palm database but instead try to download the cover art from Amazon's servers using the embedded ASIN identification string. However, we can still get covers for sideloaded books by embedding a fake ASIN identification string and manually copying an image to the corresponding location inside the Kindle thumbnails folder.

func (*Book) OverrideTemplate

func (m *Book) OverrideTemplate(tpl template.Template) Book

OverrideTemplate overrides the template used in order to generate the skeleton section of a KF8 HTML chunk.

During conversion to a PalmDB database, this template is passed the internal inventory type. If the template cannot successfully be applied, the conversion will panic.

The skeleton section generally consists of a complete HTML document including head and body, with the body tag expected to contain an 'aid' attribute that indicates the identifier of the corresponding chunk. As it is relatively easy to end up with an invalid KF8 document by generating invalid skeleton sections, this option is private and hidden behind a setter function.

func (Book) Realize

func (m Book) Realize() pdb.Database

Realize converts a MobiBook to a PalmDB Database.

type Chapter

type Chapter struct {
	Title  string
	Chunks []Chunk
}

Chapter represents a chapter in a MobiBook book.

type Chunk

type Chunk struct {
	Body string
}

Chunk represents a chunk of text in a MobiBook Chapter.

Chunks are mostly an implementation detail that is exposed for maximum control over the final book output. Generally, you should use one of the various "Chunks" functions in order to generate the correct amount of chunks for a chapter.

func Chunks

func Chunks(ss ...string) []Chunk

Chunks produces a list of chunks from one or more strings.

In the resulting list of chunks, each chunk exactly corresponds the given string with the same index. In almost all cases, this is the preferred method of converting KF8 HTML into a list of chunks.

Notes

Bugs

  • While the MOBI language format supports regions, this implementation does not and explicitly removes region information from the locale stored in the generated MOBI file.

Directories

Path Synopsis
Package jfif implements writing JPEG images with fixed JFIF header.
Package jfif implements writing JPEG images with fixed JFIF header.
Package pdb implements reading and writing PalmDB databases.
Package pdb implements reading and writing PalmDB databases.
Package records contains facilities to create MOBI formatted books.
Package records contains facilities to create MOBI formatted books.
Package types contains types and constants to create MOBI formatted books.
Package types contains types and constants to create MOBI formatted books.

Jump to

Keyboard shortcuts

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