Skip to content

[io] Fix the AsciiDoc export showing escape backslashes and dropping lines - #2238

Open
aad1tyaaaaa wants to merge 2 commits into
mindee:mainfrom
aad1tyaaaaa:fix-asciidoc-escaping
Open

aad1tyaaaaa wants to merge 2 commits into
mindee:mainfrom
aad1tyaaaaa:fix-asciidoc-escaping

Conversation

@aad1tyaaaaa

Copy link
Copy Markdown

What

export_as_asciidoc() escapes special characters with a backslash, but Asciidoctor only treats a backslash as an escape in a few contexts and keeps it as literal text everywhere else. Some escapes also don't stop the formatting, and a few kinds of line change meaning or disappear.

Rendered with Asciidoctor (asciidoctor.js 3.0.4), comparing the text a reader sees with the OCR text:

OCR text Exported now Rendered now
snake_case_name snake\_case\_name snake\_case\_name
C++ C\+\+ C+\+
#tag \#tag \#tag
*important* \*important\* *important\* (still bold in some positions)
H~2~O H\~2\~O subscript
[1] on its own line [1] nothing: read as a block attribute line
1. first, J. Smith unchanged ordered list item
NOTE: check this unchanged admonition block
''' unchanged horizontal rule

Fix

  • escape_text writes the special characters as numeric character references (_ becomes _), which Asciidoctor always renders as the plain character. [ and ] are added to the set.
  • finalize_line also prefixes {empty} to lines that start like an ordered list item (1., a., iv)), an admonition (NOTE:, TIP:, ...) or a ''' break, as it already does for the other block markers. 1.5 million, Note: lower case and 2022 was a year are left alone.

Over 36 sample lines, the rendered text now matches the OCR text in 28 cases instead of 5. The rest is out of scope here: typographic replacements ((C), --, ...), e-mail auto-links and Term:: description lists.

The trade-off is that the .adoc source is less readable by eye (snake_case), but it renders as the OCR text.

Tests

  • Updated the three existing expectations that assumed backslashes.
  • Added test_asciidoc_escape_text (8 cases) and test_asciidoc_finalize_line (10 cases, including the lines that must stay unchanged). The 12 positive cases fail without the fix.
  • tests/common/test_io_exporters.py passes; ruff check, ruff format --check and mypy doctr/ are clean.

AI assistance: I used Claude Code (Claude Opus) to help find this bug, write the fix and tests, and check the rendering with Asciidoctor. I reviewed and ran everything myself.

@codecov

codecov Bot commented Nov 26, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 98.26%. Comparing base (656ea4e) to head (464186b).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2238      +/-   ##
==========================================
- Coverage   98.28%   98.26%   -0.02%     
==========================================
  Files         182      182              
  Lines       13165    13170       +5     
==========================================
+ Hits        12939    12942       +3     
- Misses        226      228       +2     
Flag Coverage Δ
unittests 98.26% <100.00%> (-0.02%) ⬇️

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 self-assigned this Nov 27, 2026
@felixdittrich92 felixdittrich92 added type: bug Something isn't working module: io Related to doctr.io labels Nov 27, 2026
@felixdittrich92 felixdittrich92 added this to the 1.3.0 milestone Nov 27, 2026

@felixdittrich92 felixdittrich92 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks, the character-reference approach checks out when rendered with Asciidoctor (asciidoctor.js 3.0.4):

  • 91 of 103 tricky lines now render as the OCR text, against 28 before, and none renders worse.
  • On real documents (OCR with layout, tables and font style, about 6,800 words) the stray backslashes are gone.
  • It also stops pass:[...] in OCR text from putting raw HTML into the output, and link hrefs no longer contain backslashes.

A few things before merging:

  1. Rebase on main: test_font_style_exports and test_font_style_exports_can_be_plain (from #2237) still expect the backslash escapes (\*due\*) and fail once merged. They now get &#42;due&#42;.
  2. E-mail links: an address with _ now becomes a link to the wrong address. jane_doe@example.com is linked as mailto:doe@example.com, because &#95; cuts Asciidoctor's e-mail autolink in two; before, it was plain text. john+tag@example.com was already split like this. Could you leave e-mail-like words unescaped (Asciidoctor then links the full address), or prefix them with \ to keep them as plain text?
  3. $: could you add $ to _ADOC_SPECIAL_CHARS? $$…$$ is still read as an inline passthrough, so the $$ disappear and the text inside shows the character references literally (&#60;img).
  4. Nit: only a ''' line is a thematic break, so matching it with a regex such as ^'{3,}\s*$, rather than adding ' to _ADOC_LINE_MARKERS, would avoid {empty} before lines like '90s or 'tis.

…lines

Asciidoctor only honours a backslash escape in a few contexts and keeps
it as literal text everywhere else, so snake_case or C++ rendered with
visible backslashes, while *bold* or H~2~O were still formatted. Write
special characters as numeric character references instead, and also
escape [ and ] so a line like [1] is no longer read as a block attribute
line and dropped.

Prefix lines that start like an ordered list item (1., J., iv)), an
admonition (NOTE:) or a thematic break (''') with {empty}, as is already
done for other block markers.
@aad1tyaaaaa

Copy link
Copy Markdown
Author

Thanks for testing this so thoroughly. Pushed an update:

  1. Rebased on main and updated test_font_style_exports and test_font_style_exports_can_be_plain to expect the character references.
  2. E-mail addresses are left unescaped, so Asciidoctor links the whole address (tests for jane_doe@example.com and john+tag@example.com; rendered they link to mailto:jane_doe@example.com and mailto:john+tag@example.com).
  3. $ is escaped, with a test for $$<img$$.
  4. Only a ''' line gets {empty} now (^\s*'{3,}\s*$), so '90s and 'tis are left alone.

@aad1tyaaaaa
aad1tyaaaaa force-pushed the fix-asciidoc-escaping branch from f54721d to 464186b Compare November 27, 2026 12:56

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

module: io Related to doctr.io type: bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants