Skip to content

[reading order] Improve reading order and add exporters#2116

Open
felixdittrich92 wants to merge 13 commits into
mindee:mainfrom
felixdittrich92:reading-order-and-exporters
Open

[reading order] Improve reading order and add exporters#2116
felixdittrich92 wants to merge 13 commits into
mindee:mainfrom
felixdittrich92:reading-order-and-exporters

Conversation

@felixdittrich92

@felixdittrich92 felixdittrich92 commented Jul 23, 2026

Copy link
Copy Markdown
Collaborator

This PR:

  • Move export functionality into it's own module
  • Add cor docs
  • Add tests for exporters
  • Update reading order DAG and fix mem leak in overlap_ratios
  • Update table cropping by adding padding

Closes: #1586 #2085

@felixdittrich92 felixdittrich92 self-assigned this Jul 23, 2026
@codecov

codecov Bot commented Jul 23, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 87.56856% with 68 lines in your changes missing coverage. Please review.
✅ Project coverage is 96.08%. Comparing base (703d9e1) to head (e3a9bdc).

Files with missing lines Patch % Lines
doctr/io/exporters.py 88.91% 42 Missing ⚠️
doctr/models/builder.py 83.06% 21 Missing ⚠️
doctr/models/predictor/pytorch.py 0.00% 5 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2116      +/-   ##
==========================================
- Coverage   96.61%   96.08%   -0.53%     
==========================================
  Files         168      169       +1     
  Lines        9094     9533     +439     
==========================================
+ Hits         8786     9160     +374     
- Misses        308      373      +65     
Flag Coverage Δ
unittests 96.08% <87.56%> (-0.53%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@felixdittrich92 felixdittrich92 added topic: documentation Improvements or additions to documentation module: models Related to doctr.models module: io Related to doctr.io type: new feature New feature ext: docs Related to docs folder topic: reading order Related to the task of reading order labels Jul 24, 2026
@felixdittrich92 felixdittrich92 added this to the 1.1.0 milestone Jul 24, 2026
@felixdittrich92 felixdittrich92 changed the title [WIP] [reading order] Improve reading order and add exporters [reading order] Improve reading order and add exporters Jul 24, 2026
@felixdittrich92
felixdittrich92 marked this pull request as ready for review July 24, 2026 13:36
@felixdittrich92
felixdittrich92 requested a review from Copilot July 25, 2026 12:56

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR improves DocTR’s reading-order handling (including memory optimizations), introduces a dedicated doctr.io.exporters module with new Markdown/AsciiDoc/HTML exporters, and updates the builder/predictor paths and docs/tests to support these new export surfaces.

Changes:

  • Refactors export logic into doctr/io/exporters.py, adds Markdown/AsciiDoc/HTML exporters, and wires export mixins into core element classes.
  • Updates reading-order computation (DAG traversal + overlap ratio memory behavior) and adds extensive regression tests for reading-order and exporters.
  • Improves table-region cropping by adding padding and adjusts table word ordering / geometry handling in the builder.

Reviewed changes

Copilot reviewed 12 out of 12 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
tests/common/test_models_reading_order.py Adds regression tests and helpers for reading-order edge cases (columns, key/value rows).
tests/common/test_models_builder.py Extends builder repr expectations and adds tests for keep_reading_order.
tests/common/test_io_exporters.py New test suite covering page/document export to Markdown/AsciiDoc/HTML/XML and reading-order behavior.
doctr/models/reading_order/base.py Optimizes overlap computations and refines topological reading-order traversal (incl. multi-column heuristic).
doctr/models/predictor/pytorch.py Adds padding to table-region crops to improve downstream table recognition.
doctr/models/layout/lw_detr/pytorch.py Updates layout model class list (removes checkbox classes).
doctr/models/builder.py Adds keep_reading_order, improves rotated-page handling in line resolution, and refines table cell word ordering.
doctr/io/exporters.py New module implementing exporters, reading-order caching, and export mixins.
doctr/io/elements.py Moves export/render methods to mixins (delegating to doctr.io.exporters).
doctr/io/init.py Re-exports new exporters in the doctr.io package namespace.
docs/source/using_doctr/using_models.rst Documents new export APIs and keep_reading_order.
docs/source/modules/io.rst Adds API docs for new export methods and exporter classes.
Comments suppressed due to low confidence (1)

doctr/io/exporters.py:930

  • Same issue as export_as_markdown: document-level calls can reasonably pass include_furniture, which currently breaks on KIE pages. Accept/ignore it here as well to avoid TypeError when exporting mixed document types with shared helpers.
    def export_as_asciidoc(self, direction: str = "auto", escape: bool = True) -> str:
        """Export the KIE page as AsciiDoc, with the predictions of each class sorted in reading order.

        Args:
            direction: reading direction, one of 'auto', 'ltr', 'rtl', 'ttb-rtl' or 'ttb-ltr'
            escape: whether the characters and line markers carrying a structural meaning in AsciiDoc should
                be escaped

        Returns:
            an AsciiDoc string with one section per detection class
        """
        return AsciiDocExporter().export_kie_page(cast("KIEPage", self), direction=direction, escape=escape)


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread doctr/io/exporters.py
Comment on lines +906 to +917
def export_as_markdown(self, direction: str = "auto", escape: bool = True) -> str:
"""Export the KIE page as Markdown, with the predictions of each class sorted in reading order.

Args:
direction: reading direction, one of 'auto', 'ltr', 'rtl', 'ttb-rtl' or 'ttb-ltr'
escape: whether the characters carrying a structural meaning in Markdown should be escaped

Returns:
a Markdown string with one section per detection class
"""
return MarkdownExporter().export_kie_page(cast("KIEPage", self), direction=direction, escape=escape)

Comment thread doctr/io/exporters.py
Comment on lines +931 to +934
def export_as_html(self, direction: str = "auto") -> str:
"""Export the KIE page as semantic HTML, with the predictions of each class sorted in reading order"""
return HTMLExporter().export_kie_page(cast("KIEPage", self), direction=direction)

Comment on lines +631 to +636
To export the output in reading order as Markdown, AsciiDoc, HTML or XML, you can use the `export_as_markdown`,
`export_as_asciidoc`, `export_as_html` and `export_as_xml` methods (also available on a single :class:`Page`).
Reading order is always applied by these exporters: the content is linearized column by column, the reading direction is inferred from the
recognized text (e.g. right-to-left for Arabic or Hebrew documents), and - when the predictor is run with
layout detection (``detect_layout=True``) - the layout regions are used to render headings, list items and
recognized tables, and to place the page furniture (headers, footers, footnotes):
Comment on lines +149 to +151
The reading-order-aware export of a :class:`Document` / :class:`Page` to Markdown, AsciiDoc is available
through the ``export_as_markdown`` / ``export_as_asciidoc`` / ``export_as_html`` / ``export_as_xml`` / ``export_as`` / ``export`` / ``render`` methods documented above, which
delegate to the exporters of :mod:`doctr.io.exporters`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ext: docs Related to docs folder module: io Related to doctr.io module: models Related to doctr.models topic: documentation Improvements or additions to documentation topic: reading order Related to the task of reading order type: new feature New feature

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[improvement] .render() isn't that robust - wrong ordered results

2 participants