Skip to content

fix(twkan): wrap chapter text in paragraphs - #2979

Merged
dteviot merged 1 commit into
dteviot:ExperimentalTabModefrom
KitKat31337:fix/twkan-paragraph-markup
Sep 12, 2026
Merged

dteviot merged 1 commit into
dteviot:ExperimentalTabModefrom
KitKat31337:fix/twkan-paragraph-markup

Conversation

@KitKat31337

Copy link
Copy Markdown
Contributor

Summary

Convert TWKAN chapter content separated by <br> elements into properly structured <p> elements before generating the EPUB.

Problem

TWKAN provides chapter content as text nodes separated by <br> elements:

<div id="txtcontent0">
    First paragraph<br><br>
    Second paragraph<br><br>
</div>

WebToEpub previously copied that structure directly, producing chapter documents similar to:

<body>
    <h1>Chapter title</h1>
    First paragraph<br />
    <br />
    Second paragraph<br />
    <br />
</body>

Most ebook readers display this correctly, but the chapter text exists directly under <body> rather than inside text-containing elements.

This causes problems for tools that process EPUB content structurally. In particular, the Calibre Ebook Translator plugin looks for translatable text inside elements. Because the TWKAN chapter text is stored as loose text nodes, the plugin detects the chapter headings but skips almost all of the actual book content.

Standards and interoperability

WebToEpub generates this content as EPUB 2 with an XHTML 1.1 doctype.

The XHTML 1.1 structure model defines the content model of <body> as:

(Heading | Block | List)*

Raw character data and inline <br> elements are therefore not valid direct children of <body> under the declared XHTML 1.1 content model. Paragraphs are block elements and provide the appropriate structure for this content.

The corrected output is:

<body>
    <h1>Chapter title</h1>
    <p>First paragraph</p>
    <p>Second paragraph</p>
</body>

Besides improving XHTML conformance, explicit paragraph markup makes the generated EPUB more interoperable with translation tools, accessibility software, converters, editors, and other applications that operate on document elements rather than loose text nodes.

Implementation

Add a TWKAN-specific customRawDomToContentStep() that:

  • Groups chapter text and inline elements into <p> elements.
  • Uses <br> elements as paragraph boundaries and removes them afterward.
  • Preserves inline formatting such as <em>.
  • Leaves existing block-level elements unchanged.
  • Removes formatting-only whitespace outside paragraphs.

The behavior is limited to TwkanParser and does not affect EPUBs generated from other sites.

Testing

Added a TWKAN parser unit test confirming that:

  • Loose chapter text is wrapped in paragraphs.
  • Inline formatting remains inside the correct paragraph.
  • Existing paragraphs are preserved.
  • Separator <br> elements are removed.
  • No meaningful loose text remains in the content container.

The implementation was also tested against a real TWKAN chapter. It produced 87 paragraph elements while preserving all non-whitespace chapter text and leaving no loose body text or <br> separators.

@dteviot
dteviot merged commit cd30a26 into dteviot:ExperimentalTabMode Sep 12, 2026
1 check passed
@dteviot

dteviot commented Sep 12, 2026

Copy link
Copy Markdown
Owner

@KitKat31337 Thank you for your contribution

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.

2 participants