Menu

#517 content_offset in directives inside grid/simple tables are off

open-fixed
nobody
None
5
4 days ago
2025-12-27
No

When parsing a directive in a grid table or simple table, the directive's content_offset is off by one. (When nesting such a table in another such a table, it's off by one for every nesting level.)

For example, for the following code block directives, content_offset is always off from the real content line where the code block's content is in:

content_offset is off by 1:

+----------------------+
| .. code-block:: yaml |
|                      |
|    - foo             |
+----------------------+

content_offset is off by 2:

+------------------------+
|+----------------------+|
|| .. code-block:: yaml ||
||                      ||
||    - foo             ||
|+----------------------+|
+------------------------+

content_offset is off by 3:

+--------------------------+
|+------------------------+|
||+----------------------+||
||| .. code-block:: yaml |||
|||                      |||
|||    - foo             |||
||+----------------------+||
|+------------------------+|
+--------------------------+

content_offset is off by 1:

=====  =====
col 1  col 2
=====  =====
1      .. code-block:: yaml

          - foo
=====  =====

content_offset is off by 2:

=====  =====
col 1  col 2
=====  =====
1      =====  =====
       col 1  col 2
       =====  =====
       1      .. code-block:: yaml

                 - foo
       =====  =====
=====  =====

(I've noticed this when registering an own code block directive to find the actual location - row and column - of the code block's contents, to be able to give more precise error messages when linting.)

Discussion

  • Günter Milde

    Günter Milde - 2025-12-31

    Thank you for the report.
    Could you attach your test-directive, so that I can experiment and try a fix?

     
  • Felix Fontein

    Felix Fontein - 2026-01-01

    I've attached a small script which shows the problem. It shows the line indicated by self.content_offset surrounded by some source lines for every code block.

    Like here you can see that content_offset is correct outside a table:

         1 ''
         2 '.. code-block:: yaml'
         3 ''
     ->  4 '   - foo'
         5 ''
         6 'content_offset is off by 1:'
         7 ''
    

    And here it is off by one when inside a grid table:

         9 '| .. code-block:: yaml |'
         10 '|                      |'
         11 '|    - foo             |'
     ->  12 '+----------------------+'
         13 ''
         14 'content_offset is off by 2:'
         15 ''
    
     
  • Günter Milde

    Günter Milde - 6 days ago

    Thanks for the test script. The attached variant was used to explore the problem and find the
    spot that needs to be changed.
    The upcoming commit [r10280] will fix the problem (and also wrong line numbers in some system messages for markup errors inside table cells.

     

    Related

    Commit: [r10280]


    Last edit: Günter Milde 5 days ago
  • Felix Fontein

    Felix Fontein - 5 days ago

    Thanks for looking into this! Unfortunately with your patch, the result of my test script seems unchanged.

    This change fixes the issue for me, but I'm not sure whether it is correct:

    --- a/docutils/docutils/parsers/rst/states.py
    +++ b/docutils/docutils/parsers/rst/states.py
    @@ -1950,7 +1950,7 @@ def build_table_row(self, rowdata, tableline):
                 entry = nodes.entry(**attributes)
                 row += entry
                 if ''.join(cellblock):
    -                self.nested_parse(cellblock, input_offset=tableline+offset,
    +                self.nested_parse(cellblock, input_offset=tableline+offset-1,
                                       node=entry)
             return row
    
     
  • Felix Fontein

    Felix Fontein - 5 days ago

    Oh sorry, I missed the part on "upcoming commit"... I thought the commit you added earlier today (r10279) already fixed the issue...

     
  • Günter Milde

    Günter Milde - 5 days ago
    • status: open --> open-fixed
     
  • Günter Milde

    Günter Milde - 5 days ago

    The fix is out.
    Sorry for the confusion: I intended to publish 2 commits with one git svn dcommit
    but there was a timeout after the first :(

     

    Last edit: Günter Milde 5 days ago
  • Günter Milde

    Günter Milde - 5 days ago

    Line numbers are still wrong in the nested parsing of cell content in "CSV tables".
    Generally, source line numbers are only intended for ease of debugging and should not be relied upon too heavily.

     

    Last edit: Günter Milde 5 days ago

Log in to post a comment.