Skip to content

Tolerate malformed EPUB XHTML - #38

Open
m7pod wants to merge 1 commit into
aeosynth:masterfrom
m7pod:fix/malformed-epub
Open

m7pod wants to merge 1 commit into
aeosynth:masterfrom
m7pod:fix/malformed-epub

Conversation

@m7pod

@m7pod m7pod commented Sep 13, 2026

Copy link
Copy Markdown

Real world EPUBs (especially calibre output) often contain content that is not well formed XML, which made bk panic while parsing a chapter:

called Result::unwrap() on an Err value:
MalformedEntityReference(TextPos { row: 95, col: 43 })

Two cases are handled now:

  • bare/unescaped & such as "Graham&James", plus HTML named entities like   that XML does not define. Known entities are converted to numeric character references and stray & are escaped.
  • unbalanced HTML style tags, e.g. a missing

    , are repaired by an HTML-style tidier that closes open tags and drops stray closing tags.

repair_xml() tries the raw document first, then entity fixes, then tag fixes, so well formed EPUBs and entities declared in a DOCTYPE keep working exactly as before.

Real world EPUBs (especially calibre output) often contain content that
is not well formed XML, which made bk panic while parsing a chapter:

  called Result::unwrap() on an Err value:
  MalformedEntityReference(TextPos { row: 95, col: 43 })

Two cases are handled now:

- bare/unescaped & such as "Graham&James", plus HTML named entities
  like   that XML does not define. Known entities are converted to
  numeric character references and stray & are escaped.
- unbalanced HTML style tags, e.g. a missing </p>, are repaired by an
  HTML-style tidier that closes open tags and drops stray closing tags.

repair_xml() tries the raw document first, then entity fixes, then tag
fixes, so well formed EPUBs and entities declared in a DOCTYPE keep
working exactly as before.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant