epub parsing spec
This commit is contained in:
2 files changed
+1051
-2
No files matched your search
@@ -0,0 +1,483 @@
|
||||
-- allium: 3
|
||||
-- epub-parsing.allium
|
||||
--
|
||||
-- Scope: EPUB package & spine parsing orchestration (EpubReader)
|
||||
-- Includes:
|
||||
-- - Container discovery (META-INF/container.xml -> OPF rootfile path)
|
||||
-- - OPF package parsing (Dublin Core metadata, manifest items, spine order)
|
||||
-- - Cover image resolution (EPUB 3 cover-image property, EPUB 2 meta cover, ID fallback)
|
||||
-- - Full open vs metadata-only loading modes
|
||||
-- - Embedded font registration (OPF manifest font media types and CSS @font-face rules)
|
||||
-- - Spine XHTML processing (XML entity sanitisation, external/inline CSS cascade, image URL rewriting, ContentChunk generation)
|
||||
-- - Empty-spine fallback chunk creation
|
||||
-- Excludes:
|
||||
-- - ZIP archive byte extraction mechanics (ZipByteSource / IByteSource implementations)
|
||||
-- - Detailed CSS selector specificity and property value parsing (deferred to CssCascade)
|
||||
-- - Detailed HTML DOM to BlockItem and ContentChunk partitioning algorithms (deferred to HtmlBlockParser)
|
||||
-- - Off-thread Knuth-Plass line breaking and QML rendering
|
||||
|
||||
------------------------------------------------------------
|
||||
-- External Entities
|
||||
------------------------------------------------------------
|
||||
|
||||
external entity ArchiveSource {
|
||||
file_path: String
|
||||
is_readable: Boolean
|
||||
content_hash: String
|
||||
}
|
||||
|
||||
------------------------------------------------------------
|
||||
-- Value Types
|
||||
------------------------------------------------------------
|
||||
|
||||
value ManifestItem {
|
||||
id: String
|
||||
href: String
|
||||
resolved_path: String
|
||||
media_type: String
|
||||
properties: Set<String>
|
||||
|
||||
is_cover_property: `cover-image` in properties
|
||||
is_image: starts_with(media_type, "image/")
|
||||
is_font:
|
||||
starts_with(media_type, "font/")
|
||||
or starts_with(media_type, "application/font")
|
||||
or starts_with(media_type, "application/x-font")
|
||||
or media_type = "application/vnd.ms-opentype"
|
||||
}
|
||||
|
||||
value SpineItem {
|
||||
idref: String
|
||||
resolved_path: String
|
||||
}
|
||||
|
||||
value StyleSheet {
|
||||
source_path: String?
|
||||
rules_data: String
|
||||
font_face_urls: List<String>
|
||||
}
|
||||
|
||||
value ContentChunk {
|
||||
section_href: String
|
||||
word_count: Integer
|
||||
char_count: Integer
|
||||
unnested_containers: Integer
|
||||
is_empty_fallback: Boolean
|
||||
}
|
||||
|
||||
------------------------------------------------------------
|
||||
-- Contracts
|
||||
------------------------------------------------------------
|
||||
|
||||
contract ByteSourceAccess {
|
||||
open_archive: (file_path: String) -> Boolean
|
||||
read_entry: (source: ArchiveSource, entry_path: String) -> String?
|
||||
compute_md5: (source: ArchiveSource) -> String
|
||||
|
||||
@invariant DeterministicHash
|
||||
-- compute_md5 returns the same MD5 digest for identical archive contents
|
||||
-- regardless of file path or rename history.
|
||||
}
|
||||
|
||||
contract CssProcessing {
|
||||
parse_stylesheet: (css_text: String) -> StyleSheet
|
||||
cascade_and_rewrite_dom: (xhtml_dom: String, stylesheet: StyleSheet, scheme: String, archive_path: String, xhtml_dir: String) -> String
|
||||
|
||||
@invariant StylesheetCaching
|
||||
-- External stylesheets within a single book open operation are parsed at most once per resolved path.
|
||||
}
|
||||
|
||||
contract HtmlChunkPartitioning {
|
||||
parse_from_dom: (cascaded_dom: String, section_href: String, target_chars: Integer) -> List<ContentChunk>
|
||||
parse_from_raw_xhtml: (raw_xhtml: String, section_href: String, target_chars: Integer) -> List<ContentChunk>
|
||||
|
||||
@invariant PreservesSectionHref
|
||||
-- Every returned ContentChunk has section_href equal to the spine item's resolved_path.
|
||||
}
|
||||
|
||||
------------------------------------------------------------
|
||||
-- Enumerations
|
||||
------------------------------------------------------------
|
||||
|
||||
enum ParseStatus {
|
||||
unopened
|
||||
loading_metadata
|
||||
loading_full
|
||||
metadata_ready
|
||||
ready
|
||||
failed
|
||||
}
|
||||
|
||||
enum FailureReason {
|
||||
source_open_failed
|
||||
missing_container_xml
|
||||
missing_opf_rootfile
|
||||
unreadable_opf_package
|
||||
}
|
||||
|
||||
------------------------------------------------------------
|
||||
-- Entities and Variants
|
||||
------------------------------------------------------------
|
||||
|
||||
entity EpubDocument {
|
||||
scheme: String
|
||||
status: ParseStatus
|
||||
|
||||
source: ArchiveSource when status = loading_metadata | loading_full | metadata_ready | ready
|
||||
file_path: String when status = loading_metadata | loading_full | metadata_ready | ready
|
||||
content_hash: String when status = loading_full | ready
|
||||
error_reason: FailureReason when status = failed
|
||||
|
||||
opf_path: String?
|
||||
opf_dir: String?
|
||||
title: String?
|
||||
author: String?
|
||||
cover_entry_path: String?
|
||||
cover_image_url: String?
|
||||
|
||||
manifest: Set<ManifestItem>
|
||||
spine: List<SpineItem>
|
||||
chunks: List<ContentChunk> when status = ready
|
||||
loaded_fonts: Set<String> when status = loading_full | ready
|
||||
|
||||
transitions status {
|
||||
unopened -> loading_metadata
|
||||
unopened -> loading_full
|
||||
loading_metadata -> metadata_ready
|
||||
loading_metadata -> failed
|
||||
loading_full -> ready
|
||||
loading_full -> failed
|
||||
metadata_ready -> loading_metadata
|
||||
metadata_ready -> loading_full
|
||||
ready -> loading_metadata
|
||||
ready -> loading_full
|
||||
failed -> loading_metadata
|
||||
failed -> loading_full
|
||||
terminal: metadata_ready, ready, failed
|
||||
}
|
||||
|
||||
-- Derived values
|
||||
has_cover: cover_image_url != null
|
||||
reading_mode: `text`
|
||||
}
|
||||
|
||||
------------------------------------------------------------
|
||||
-- Config
|
||||
------------------------------------------------------------
|
||||
|
||||
config {
|
||||
container_xml_path: String = "META-INF/container.xml"
|
||||
default_scheme: String = "epub"
|
||||
target_chunk_chars: Integer = 512
|
||||
}
|
||||
|
||||
------------------------------------------------------------
|
||||
-- Rules
|
||||
------------------------------------------------------------
|
||||
|
||||
-- 1. Initiating Full Open vs Metadata-Only Load
|
||||
|
||||
rule StartFullOpen {
|
||||
when: OpenEpubRequested(doc, source)
|
||||
requires: doc.status in {unopened, metadata_ready, ready, failed}
|
||||
requires: source.is_readable
|
||||
|
||||
ensures:
|
||||
doc.status = loading_full
|
||||
doc.source = source
|
||||
doc.file_path = source.file_path
|
||||
doc.content_hash = source.content_hash
|
||||
doc.error_reason = null
|
||||
doc.opf_path = null
|
||||
doc.opf_dir = null
|
||||
doc.title = null
|
||||
doc.author = null
|
||||
doc.cover_entry_path = null
|
||||
doc.cover_image_url = null
|
||||
doc.manifest = {}
|
||||
doc.spine = []
|
||||
doc.chunks = null
|
||||
doc.loaded_fonts = {}
|
||||
ensures: ContainerDiscoveryRequested(doc: doc)
|
||||
}
|
||||
|
||||
rule StartMetadataLoad {
|
||||
when: LoadEpubMetadataRequested(doc, source)
|
||||
requires: doc.status in {unopened, metadata_ready, ready, failed}
|
||||
requires: source.is_readable
|
||||
|
||||
ensures:
|
||||
doc.status = loading_metadata
|
||||
doc.source = source
|
||||
doc.file_path = source.file_path
|
||||
doc.content_hash = null
|
||||
doc.error_reason = null
|
||||
doc.opf_path = null
|
||||
doc.opf_dir = null
|
||||
doc.title = null
|
||||
doc.author = null
|
||||
doc.cover_entry_path = null
|
||||
doc.cover_image_url = null
|
||||
doc.manifest = {}
|
||||
doc.spine = []
|
||||
doc.chunks = null
|
||||
doc.loaded_fonts = null
|
||||
ensures: ContainerDiscoveryRequested(doc: doc)
|
||||
}
|
||||
|
||||
rule FailOnUnreadableSource {
|
||||
when: OpenEpubRequested(doc, source)
|
||||
requires: doc.status in {unopened, metadata_ready, ready, failed}
|
||||
requires: not source.is_readable
|
||||
|
||||
ensures:
|
||||
doc.status = failed
|
||||
doc.error_reason = source_open_failed
|
||||
doc.source = null
|
||||
doc.file_path = null
|
||||
doc.content_hash = null
|
||||
doc.chunks = null
|
||||
doc.loaded_fonts = null
|
||||
}
|
||||
|
||||
rule FailMetadataOnUnreadableSource {
|
||||
when: LoadEpubMetadataRequested(doc, source)
|
||||
requires: doc.status in {unopened, metadata_ready, ready, failed}
|
||||
requires: not source.is_readable
|
||||
|
||||
ensures:
|
||||
doc.status = failed
|
||||
doc.error_reason = source_open_failed
|
||||
doc.source = null
|
||||
doc.file_path = null
|
||||
doc.content_hash = null
|
||||
doc.chunks = null
|
||||
doc.loaded_fonts = null
|
||||
}
|
||||
|
||||
-- 2. Container Discovery (META-INF/container.xml)
|
||||
|
||||
rule DiscoverOpfFromContainer {
|
||||
when: ContainerDiscoveryRequested(doc)
|
||||
requires: doc.status in {loading_metadata, loading_full}
|
||||
|
||||
let container_bytes = read_entry(doc.source, config.container_xml_path)
|
||||
let rootfile_path = extract_rootfile_full_path(container_bytes)
|
||||
|
||||
requires: container_bytes != null
|
||||
requires: rootfile_path != null
|
||||
|
||||
ensures:
|
||||
doc.opf_path = rootfile_path
|
||||
doc.opf_dir = parent_directory(rootfile_path)
|
||||
ensures: OpfParseRequested(doc: doc)
|
||||
}
|
||||
|
||||
rule FailOnMissingContainerXml {
|
||||
when: ContainerDiscoveryRequested(doc)
|
||||
requires: doc.status in {loading_metadata, loading_full}
|
||||
|
||||
let container_bytes = read_entry(doc.source, config.container_xml_path)
|
||||
requires: container_bytes = null
|
||||
|
||||
ensures:
|
||||
doc.status = failed
|
||||
doc.error_reason = missing_container_xml
|
||||
doc.source = null
|
||||
doc.file_path = null
|
||||
doc.content_hash = null
|
||||
doc.chunks = null
|
||||
doc.loaded_fonts = null
|
||||
}
|
||||
|
||||
rule FailOnMissingRootfilePath {
|
||||
when: ContainerDiscoveryRequested(doc)
|
||||
requires: doc.status in {loading_metadata, loading_full}
|
||||
|
||||
let container_bytes = read_entry(doc.source, config.container_xml_path)
|
||||
let rootfile_path = extract_rootfile_full_path(container_bytes)
|
||||
|
||||
requires: container_bytes != null
|
||||
requires: rootfile_path = null
|
||||
|
||||
ensures:
|
||||
doc.status = failed
|
||||
doc.error_reason = missing_opf_rootfile
|
||||
doc.source = null
|
||||
doc.file_path = null
|
||||
doc.content_hash = null
|
||||
doc.chunks = null
|
||||
doc.loaded_fonts = null
|
||||
}
|
||||
|
||||
-- 3. OPF Package Parsing (Metadata, Manifest, Cover, Spine)
|
||||
|
||||
rule FailOnUnreadableOpf {
|
||||
when: OpfParseRequested(doc)
|
||||
requires: doc.status in {loading_metadata, loading_full}
|
||||
|
||||
let opf_bytes = read_entry(doc.source, doc.opf_path)
|
||||
requires: opf_bytes = null
|
||||
|
||||
ensures:
|
||||
doc.status = failed
|
||||
doc.error_reason = unreadable_opf_package
|
||||
doc.source = null
|
||||
doc.file_path = null
|
||||
doc.content_hash = null
|
||||
doc.chunks = null
|
||||
doc.loaded_fonts = null
|
||||
}
|
||||
|
||||
rule CompleteMetadataOnlyParse {
|
||||
when: OpfParseRequested(doc)
|
||||
requires: doc.status = loading_metadata
|
||||
|
||||
let opf_bytes = read_entry(doc.source, doc.opf_path)
|
||||
requires: opf_bytes != null
|
||||
|
||||
let parsed_title = extract_first_dc_title(opf_bytes)
|
||||
let parsed_author = extract_first_dc_creator(opf_bytes)
|
||||
let items = extract_manifest_items(opf_bytes, doc.opf_dir)
|
||||
let spine_items = extract_spine_items(opf_bytes, items)
|
||||
let cover_entry = resolve_cover_entry(opf_bytes, items, doc.opf_dir)
|
||||
|
||||
ensures:
|
||||
doc.status = metadata_ready
|
||||
doc.title = parsed_title ?? file_base_name(doc.file_path)
|
||||
doc.author = parsed_author ?? ""
|
||||
doc.manifest = items
|
||||
doc.spine = spine_items
|
||||
doc.cover_entry_path = cover_entry
|
||||
doc.cover_image_url =
|
||||
if cover_entry != null:
|
||||
build_image_url(doc.scheme, cover_entry, doc.file_path)
|
||||
else:
|
||||
null
|
||||
}
|
||||
|
||||
rule CompleteFullParse {
|
||||
when: OpfParseRequested(doc)
|
||||
requires: doc.status = loading_full
|
||||
|
||||
let opf_bytes = read_entry(doc.source, doc.opf_path)
|
||||
requires: opf_bytes != null
|
||||
|
||||
let parsed_title = extract_first_dc_title(opf_bytes)
|
||||
let parsed_author = extract_first_dc_creator(opf_bytes)
|
||||
let items = extract_manifest_items(opf_bytes, doc.opf_dir)
|
||||
let spine_items = extract_spine_items(opf_bytes, items)
|
||||
let cover_entry = resolve_cover_entry(opf_bytes, items, doc.opf_dir)
|
||||
let manifest_fonts = filter_manifest_fonts(items, doc.source)
|
||||
let spine_chunks = build_all_spine_chunks(doc.source, spine_items, doc.scheme, doc.file_path, config.target_chunk_chars)
|
||||
let css_fonts = extract_loaded_css_fonts(doc.source, spine_items)
|
||||
|
||||
ensures:
|
||||
doc.status = ready
|
||||
doc.title = parsed_title ?? file_base_name(doc.file_path)
|
||||
doc.author = parsed_author ?? ""
|
||||
doc.manifest = items
|
||||
doc.spine = spine_items
|
||||
doc.cover_entry_path = cover_entry
|
||||
doc.cover_image_url =
|
||||
if cover_entry != null:
|
||||
build_image_url(doc.scheme, cover_entry, doc.file_path)
|
||||
else:
|
||||
null
|
||||
doc.loaded_fonts = manifest_fonts + css_fonts
|
||||
doc.chunks =
|
||||
if spine_chunks.count > 0:
|
||||
spine_chunks
|
||||
else:
|
||||
[ fallback_empty_chunk(doc.file_path) ]
|
||||
|
||||
@guidance
|
||||
-- Cover resolution priority in resolve_cover_entry:
|
||||
-- 1. Manifest item with properties containing "cover-image" (EPUB 3) or
|
||||
-- <meta name="cover" content="<id>"/> / <meta content="cover" name="<id>"/> (EPUB 2),
|
||||
-- where the later declaration in document order wins if multiple exist.
|
||||
-- 2. Fallback: first manifest item whose id contains "cover" (case-insensitive)
|
||||
-- and whose media_type starts with "image/".
|
||||
-- Spine processing in build_all_spine_chunks:
|
||||
-- For each SpineItem in spine order:
|
||||
-- 1. Convert HTML named character references to XML numeric references via HtmlEntities::toXmlSafe.
|
||||
-- 2. If XML DOM parsing succeeds:
|
||||
-- a. Load and cache linked <link rel="stylesheet"> CSS files and embedded <style> blocks.
|
||||
-- b. Register @font-face src fonts once per unique resolved archive path.
|
||||
-- c. Cascade CSS declarations into inline style attributes on each element.
|
||||
-- d. Rewrite <img> and <image> (src, xlink:href, href) URLs to image://<scheme>/<entry>?src=<file_path>
|
||||
-- or image://web/<http-url>.
|
||||
-- e. Partition the cascaded DOM into ContentChunks via HtmlBlockParser::parseChunks.
|
||||
-- 3. If XML DOM parsing fails on the sanitised XHTML, fall back directly to
|
||||
-- HtmlBlockParser::parseChunks on the raw XHTML string.
|
||||
}
|
||||
|
||||
------------------------------------------------------------
|
||||
-- Invariants
|
||||
------------------------------------------------------------
|
||||
|
||||
invariant ReadyDocumentAlwaysHasChunks {
|
||||
for doc in EpubDocuments:
|
||||
doc.status = ready implies doc.chunks.count >= 1
|
||||
}
|
||||
|
||||
invariant MetadataOnlySkipsContentHashAndChunks {
|
||||
for doc in EpubDocuments:
|
||||
doc.status = metadata_ready implies doc.title != null
|
||||
}
|
||||
|
||||
invariant ReadyDocumentHasTitle {
|
||||
for doc in EpubDocuments:
|
||||
doc.status in {metadata_ready, ready} implies doc.title != null
|
||||
}
|
||||
|
||||
------------------------------------------------------------
|
||||
-- Actor Declarations
|
||||
------------------------------------------------------------
|
||||
|
||||
actor BookSubsystem {
|
||||
identified_by: ArchiveSource where is_readable = true
|
||||
}
|
||||
|
||||
------------------------------------------------------------
|
||||
-- Surfaces
|
||||
------------------------------------------------------------
|
||||
|
||||
surface EpubFormatReader {
|
||||
facing caller: BookSubsystem
|
||||
context doc: EpubDocument
|
||||
|
||||
exposes:
|
||||
doc.status
|
||||
doc.reading_mode
|
||||
doc.file_path when doc.status in {loading_metadata, loading_full, metadata_ready, ready}
|
||||
doc.content_hash when doc.status in {loading_full, ready}
|
||||
doc.title
|
||||
doc.author
|
||||
doc.has_cover
|
||||
doc.cover_image_url
|
||||
doc.chunks when doc.status = ready
|
||||
doc.error_reason when doc.status = failed
|
||||
|
||||
provides:
|
||||
OpenEpubRequested(doc, caller)
|
||||
when doc.status in {unopened, metadata_ready, ready, failed}
|
||||
LoadEpubMetadataRequested(doc, caller)
|
||||
when doc.status in {unopened, metadata_ready, ready, failed}
|
||||
|
||||
contracts:
|
||||
demands ByteSourceAccess
|
||||
demands CssProcessing
|
||||
demands HtmlChunkPartitioning
|
||||
|
||||
@guarantee NonEmptyChunksWhenReady
|
||||
-- Opening a valid EPUB in full mode always produces at least one ContentChunk,
|
||||
-- synthesizing a single empty paragraph chunk if the spine yields no blocks.
|
||||
}
|
||||
|
||||
------------------------------------------------------------
|
||||
-- Deferred Specifications
|
||||
------------------------------------------------------------
|
||||
|
||||
deferred CssCascade.cascade_and_serialize -- see: src/backend/reading/layout/CssParser.h
|
||||
deferred HtmlBlockParser.parse_and_partition -- see: src/backend/reading/layout/HtmlBlockParser.h
|
||||
Reference in new issue
Block a user