7474# Markdown fenced-code delimiter (``` or ~~~ with optional leading indentation).
7575_FENCE_RE = re .compile (r"^\s*(```|~~~)" )
7676
77+ # A GFM table delimiter cell contains at least one dash, optionally surrounded
78+ # by alignment colons. Only a proven table may exempt ordinary ASCII cell
79+ # padding from P9.
80+ _TABLE_DELIMITER_CELL_RE = re .compile (r"^\s*:?-+:?\s*$" )
81+
7782# Line-boundary characters/sequences that count as line separators when splitting
7883# content into logical lines. Beyond ASCII LF, this includes CR / CRLF and the
7984# Unicode line/paragraph separators U+2028 / U+2029 / U+0085 (NEL) — all of which
@@ -294,6 +299,7 @@ def _detect_horizontal(
294299 runs : list [PaddingRun ] = []
295300 # Only the markdown path needs fence flags; skip building the list otherwise.
296301 fence_flags = _fence_line_flags (lines ) if file_type == "markdown" else None
302+ table_boundaries = _markdown_table_boundaries (lines ) if file_type == "markdown" else {}
297303 for idx , line in enumerate (lines ):
298304 if fence_flags is not None and fence_flags [idx ]:
299305 continue
@@ -309,6 +315,11 @@ def _detect_horizontal(
309315 k += 1
310316 run_len = k - start
311317 if run_len >= HORIZONTAL_RUN_CHARS :
318+ # Exempt ASCII cell padding only when its closing pipe occupies
319+ # the matching delimiter-row boundary. Misaligned table padding
320+ # can still push content off-screen and remains a finding.
321+ if k in table_boundaries .get (idx , set ()) and line [start :k ] == " " * run_len :
322+ continue
312323 start_offset = line_offset + start
313324 followed_by_content = k < line_len
314325 summary = summarize_run (line [start :k ])
@@ -326,6 +337,60 @@ def _detect_horizontal(
326337 return runs
327338
328339
340+ def _markdown_table_cells (line : str ) -> list [str ] | None :
341+ """Return cells for an outer-pipe Markdown row, or ``None`` otherwise."""
342+ stripped = line .strip ()
343+ if not (stripped .startswith ("|" ) and stripped .endswith ("|" )):
344+ return None
345+ return stripped [1 :- 1 ].split ("|" )
346+
347+
348+ def _markdown_table_boundaries (lines : list [str ]) -> dict [int , set [int ]]:
349+ """Return row pipe positions aligned with the proven delimiter row.
350+
351+ Requiring a header followed by a delimiter row keeps arbitrary pipe-heavy
352+ prose on the fail-closed detection path. A row pipe is exemptible only
353+ when it has the same ordinal and character column as its delimiter-row pipe.
354+ """
355+ aligned_boundaries : dict [int , set [int ]] = {}
356+ index = 0
357+ while index + 1 < len (lines ):
358+ header = _markdown_table_cells (lines [index ])
359+ delimiter = _markdown_table_cells (lines [index + 1 ])
360+ if (
361+ header
362+ and delimiter
363+ and len (header ) == len (delimiter )
364+ and all (_TABLE_DELIMITER_CELL_RE .fullmatch (cell ) for cell in delimiter )
365+ ):
366+ delimiter_pipes = [
367+ i for i , character in enumerate (lines [index + 1 ]) if character == "|"
368+ ]
369+ header_pipes = [i for i , character in enumerate (lines [index ]) if character == "|" ]
370+ if len (header_pipes ) == len (delimiter_pipes ):
371+ aligned_boundaries [index ] = {
372+ row_pipe
373+ for row_pipe , delimiter_pipe in zip (header_pipes , delimiter_pipes , strict = True )
374+ if row_pipe == delimiter_pipe
375+ }
376+ index += 2
377+ while index < len (lines ):
378+ row = _markdown_table_cells (lines [index ])
379+ if row is None or len (row ) != len (header ):
380+ break
381+ row_pipes = [i for i , character in enumerate (lines [index ]) if character == "|" ]
382+ if len (row_pipes ) == len (delimiter_pipes ):
383+ aligned_boundaries [index ] = {
384+ row_pipe
385+ for row_pipe , delimiter_pipe in zip (row_pipes , delimiter_pipes , strict = True )
386+ if row_pipe == delimiter_pipe
387+ }
388+ index += 1
389+ continue
390+ index += 1
391+ return aligned_boundaries
392+
393+
329394def _detect_block_and_ratio (content : str ) -> list [PaddingRun ]:
330395 """Detect a contiguous block > BLOCK_BYTE_BUDGET and the >90%-of-file ratio.
331396
0 commit comments