dictgen

package
v0.3.1 Latest Latest
Warning

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

Go to latest
Published: Jul 8, 2020 License: MIT Imports: 17 Imported by: 0

Documentation

Overview

Package dictgen simplifies creating full-featured dictionaries for Kobo eReaders, with support for images, unicode prefixes, raw html, markdown, and more.

A marisa implementation must be provided by github.com/geek1011/kobodict/marisa or a custom one for this package to work.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ImageFuncFilesystem

func ImageFuncFilesystem(src string) (io.Reader, error)

ImageFuncFilesystem loads an image from the filesystem. If src is relative, it is resolved relative to the current dir.

Types

type DictFile

type DictFile []*DictFileEntry

A DictFile is a high-level representation of a Kobo dictionary.

func ParseDictFile

func ParseDictFile(r io.Reader) (DictFile, error)

ParseDictFile parses a DictFile from it's textual representation (usually stored in a file with the extension .df).

func (DictFile) Prefixed

func (df DictFile) Prefixed() map[string]DictFile

Prefixed shards the DictFile into the different word prefixes. The original DictFile is unchanged, but the entries are still pointers to the originals (i.e. the result will become out of date if you modify the entries).

The DictFile is not validated.

If a variamt has a different prefix, the entire entry is duplicated as necessary.

func (DictFile) Validate

func (df DictFile) Validate() error

Validate validates the entries in the DictFile. Note that duplicate entries are fine, and are encouraged if necessary (Kobo will merge them).

func (DictFile) WriteDictFile

func (df DictFile) WriteDictFile(w io.Writer) error

WriteDictFile validates the DictFile and writes it to w in the dictfile format.

func (DictFile) WriteDictzip

func (df DictFile) WriteDictzip(dw *kobodict.Writer, ih ImageHandler, img ImageFunc) error

WriteDictzip writes the dictfile to a kobodict.Writer, which should not have been used yet. The writer is not closed automatically. If the ImageHandler requires a file to be opened (i.e. not ImageHandlerRemove), the provided ImageFunc will be called.

func (DictFile) WriteKoboHTML

func (df DictFile) WriteKoboHTML(w io.Writer) error

WriteKoboHTML validates the DictFile and writes it to w in the dicthtml format.

type DictFileEntry

type DictFileEntry struct {
	Headword string
	Variant  []string

	NoHeader   bool
	HeaderInfo string

	RawHTML    bool
	Definition string

	PostRawHTML string // will not be parsed or saved, only to be used for runtime additions before generating
	// contains filtered or unexported fields
}

DictFileEntry represents a single entry in the DictFile.

type ImageFunc

type ImageFunc func(src string) (io.Reader, error)

ImageFunc reads an image from the path (it may be absolute or relative) src, and returns an io.Reader for the image contents. If the returned reader implements io.Closer, it will automatically be called after the image has been processed.

type ImageHandler

type ImageHandler interface {
	// Transform transforms an image read from ir, and returns a new value for
	// the img tag's src attribute. As a special case, if an empty string is
	// returned and the error is nil, the image tag is removed entirely. In
	// addition, custom CSS (which must not contain any double quotes) can be
	// returned to be set on the img tag.
	Transform(src string, ir io.Reader, dw *kobodict.Writer) (nsrc string, css string, err error)

	// Description returns a human-readable description of what the handler does.
	Description() string
}

ImageHandler transforms images referenced in a DictFile.

type ImageHandlerBase64

type ImageHandlerBase64 struct {
	// Images will be resized to fit within these dimensions, while preserving
	// aspect ratio. If not specified, the default is 1000x1000.
	MaxSize image.Point
	// NoGrayscale will prevent images from being grayscaled.
	NoGrayscale bool
	// JPEGQuality sets the JPEG quality for the encoded images. If not set, it
	// defaults to 60.
	JPEGQuality int
}

ImageHandlerBase64 optimizes the image and encodes it as base64. This is the most compatible option, but it comes at the expense of space and speed. In addition, if there are too many images, it can lead to nickel running out of memory when parsing the dictionary (and sickel should reboot it).

In addition, it adds CSS to fix sizing issues (by default, images appear really small when rendered in the dictionary due to default styling).

This is currently the recommended option for adding images.

You must import image/* yourself for format support.

func (*ImageHandlerBase64) Description

func (ih *ImageHandlerBase64) Description() string

Description implements ImageHandler.

func (*ImageHandlerBase64) Transform

func (ih *ImageHandlerBase64) Transform(src string, ir io.Reader, dw *kobodict.Writer) (string, string, error)

Transform implements ImageHandler.

type ImageHandlerEmbed

type ImageHandlerEmbed struct{}

ImageHandlerEmbed adds the images to the dictzip without any additional modifications. Usually, this would be the best choice, but unfortunately, it is too buggy as of firmware 4.19.14123.

func (*ImageHandlerEmbed) Description

func (*ImageHandlerEmbed) Description() string

Description implements ImageHandler.

func (*ImageHandlerEmbed) Transform

func (*ImageHandlerEmbed) Transform(src string, ir io.Reader, dw *kobodict.Writer) (string, string, error)

Transform implements ImageHandler.

type ImageHandlerRemove

type ImageHandlerRemove struct{}

ImageHandlerRemove removes images from the dicthtml.

func (*ImageHandlerRemove) Description

func (*ImageHandlerRemove) Description() string

Description implements ImageHandler.

func (*ImageHandlerRemove) Transform

Transform implements ImageHandler.

Jump to

Keyboard shortcuts

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