ebook_parser library

Parse EPUB and FB2 ebooks into one shared document model — with lazy sentence and word segmentation, a cheap metadata-only path, and transparent .fb2.zip handling.

The entry point is bookParserFor: pass the file path and the bytes you hold, get back an IBookParser or null. Everything a parse returns is the shared model rooted at BookDocument; expected failures arrive as ParseErr, never as exceptions.

JSON serialization is a separate opt-in import: package:ebook_parser/serialization.dart.

Classes

ArchiveContent
What a picked file turned out to be.
Block
A content block: exactly one of ParagraphBlock, HeadingBlock, or ImageBlock.
BookDocument
A fully parsed book: metadata plus an ordered list of chapters.
BookMetadata
Cheap-to-extract book metadata: what parseMetadata returns and what parse puts on the document, so the two paths cannot answer differently.
Chapter
One chapter of a BookDocument.
EpubArchive
A zip that is the book: EPUB. The EPUB parser reads the container itself, so unwrapping it would be undoing the format.
HeadingBlock
A heading line inside the flow.
IBookParser
Parses a book file's bytes into the shared document model.
ImageBlock
An inline or illustration image.
ImageData
Image bytes exactly as the file stores them, plus the media type it declared.
NoBookInside
A zip with nothing in it we can read.
NotAnArchive
Not a zip — an ordinary FB2 (or anything else); parse it as it is.
ParagraphBlock
A paragraph of body text.
ParseErr<T>
A failed parse carrying its failure.
ParseFailure
Describes a failed parse.
ParseOk<T>
A successful parse carrying its value.
ParseResult<T>
The result of a parse: ParseOk with a value, or ParseErr with a ParseFailure.
RuleBasedSegmenter
The built-in TextSegmenter: rule-based, decided by writing system rather than by language.
Sentence
A sentence span within its ParagraphBlock.text.
SeveralBooksInside
A zip holding several books. Refused rather than guessed: picking the first one silently imports a book nobody chose.
TextSegmenter
Splits one paragraph's text into Sentences, each carrying its Words, with offsets relative to that paragraph's text.
Word
A single word span within its ParagraphBlock.text.
WrappedBook
A zip holding exactly one book file, already extracted.

Enums

ParseFailureKind
Why a parse failed.

Extensions

BookDocumentSample on BookDocument
Body sampling for language detection.

Constants

importableBookExtensions → const List<String>
Extensions a file picker or an "Open with" filter should accept.
supportedBookExtensions → const List<String>
Formats the package can parse (lowercase, no dot).

Functions

bookParserFor(String filePath, Uint8List bytes) IBookParser?
Returns the parser for the book at filePath with content bytes, or null when no parser matches — an unrecognised file is an expected outcome, not an error.
inspectBookArchive(Uint8List bytes) ArchiveContent
Looks inside bytes and reports what to import.
isZipArchive(Uint8List bytes) bool
Whether bytes starts with the zip magic PK\x03\x04.
normalizeLanguageCode(String? declared, {required String fallback}) String
Reduces declared to an ISO-639-1 code, or returns the normalized fallback when it does not reduce to one.