Skip to content

Fix directive body_offset inflated by one when body ends in blank lines - #1179

Merged
chrisjsewell merged 7 commits into
executablebooks:masterfrom
dchaudhari7177:fix/body-offset-trailing-blank
Nov 9, 2026
Merged

chrisjsewell merged 7 commits into
executablebooks:masterfrom
dchaudhari7177:fix/body-offset-trailing-blank

Conversation

@dchaudhari7177

Copy link
Copy Markdown
Contributor

Fixes #1177.

body_offset (the number of lines from a directive's opening line to its first body line) was inflated by one whenever a directive had an option block and its body ended in blank line(s).

_parse_directive_options returns the body as a string built with "\n".join(...), and parse_directive_text re-split it and derived the offset as len(content.splitlines()) - len(body_lines). The join→split round-trip drops exactly one trailing blank line, so len(body_lines) was one short and the offset one too high — every body element was then reported one source line too low.

Fix: _parse_directive_options now reports the number of source lines the option block actually occupies (computed before the body is re-joined), and parse_directive_text uses that directly instead of the lossy subtraction. The count also correctly handles a --- closer that carries trailing text (which becomes the first body line on the closing line itself).

Verification

Added test_body_offset_not_inflated_by_trailing_blank_lines covering colon- and YAML-style option blocks, with and without trailing blanks — it fails on main and passes here. The full test_parse_directives.py suite (including the snapshot fixtures and the trailing-closing-text case) stays green.

dchaudhari7177 and others added 7 commits July 27, 2026 12:09
…es end to end

Parametrise the body_offset unit test (one trailing blank, a blank
separator line, text after the closing `---`, an unclosed `---` block,
and first-line content with and without options), and add an end-to-end
test that body paragraphs and their warnings land on their source lines.
…n paths

Adds trailing-blank cases for validate_options=False (the plain-YAML path
myst-nb uses) and for docutils' test directive, whose option results
return through their own branches. Also rewords the option_line_count
docstring (it counts the lines before the body, which is one fewer than
the block when text trails a closing `---`) and wraps the new comments.
@chrisjsewell
chrisjsewell merged commit 56849d1 into executablebooks:master Nov 9, 2026
24 checks passed
@chrisjsewell

Copy link
Copy Markdown
Member

Merged in 56849d1, thanks @dchaudhari7177, the fix was right: counting the option-block lines before the body is re-joined is the correct place to do it. Before merging I pushed two merges of master and two test commits:

  • your unit test parametrised, with extra cases for a single trailing blank line, a blank separator line, text after the closing ---, an unclosed --- block, and first-line body text with and without options;
  • an end-to-end test in tests/test_docutils.py checking that body paragraphs and their warnings land on their source lines;
  • the same trailing-blank case for validate_options=False (the plain-YAML path myst-nb uses) and for docutils' test directive.

With directives.py reverted, 12 of the 17 new cases fail.

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.

body_offset is inflated by one when a directive has an option block **and** its body ends in blank line(s)

2 participants