Skip to content

Commit 4a55062

Browse files
authored
Merge pull request NVIDIA#681 from efegokdemir/fix/676-markdown-table-padding
fix(p9): ignore visible Markdown table alignment
2 parents 383077e + 33c6cbe commit 4a55062

2 files changed

Lines changed: 117 additions & 0 deletions

File tree

‎src/skillspector/nodes/analyzers/whitespace_padding.py‎

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,11 @@
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+
329394
def _detect_block_and_ratio(content: str) -> list[PaddingRun]:
330395
"""Detect a contiguous block > BLOCK_BYTE_BUDGET and the >90%-of-file ratio.
331396

‎tests/nodes/analyzers/test_whitespace_padding.py‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,58 @@ def test_unicode_nbsp_run(self):
210210
assert len(horiz) == 1
211211
assert horiz[0].summary == f"U+00A0 x{HORIZONTAL_RUN_CHARS}"
212212

213+
def test_markdown_table_alignment_spaces_are_not_padding(self):
214+
content = (
215+
"| Field" + " " * 90 + "| Value |\n"
216+
"|" + "-" * 96 + "|-------|\n"
217+
"| Name" + " " * 91 + "| Alice |\n"
218+
)
219+
assert detect_whitespace_padding(content, file_type="markdown") == []
220+
221+
def test_markdown_table_padding_aligned_with_wide_delimiter_cell_is_exempt(self):
222+
spaces = 2_001
223+
content = (
224+
"| Notes | Other |\n"
225+
+ "|"
226+
+ "-" * (spaces + 6)
227+
+ "|---|\n"
228+
+ "| notes"
229+
+ " " * spaces
230+
+ "| other |\n"
231+
)
232+
233+
runs = detect_whitespace_padding(content, file_type="markdown")
234+
235+
assert not any(run.kind == "horizontal" and run.start_line == 3 for run in runs)
236+
237+
def test_markdown_table_misaligned_long_padding_remains_detectable(self):
238+
content = "| A | B |\n|---|---|\n| notes" + " " * 2_000 + "| hidden |\n"
239+
240+
runs = detect_whitespace_padding(content, file_type="markdown")
241+
242+
assert any(run.kind == "horizontal" and run.start_line == 3 for run in runs)
243+
244+
def test_markdown_table_padding_before_cell_text_remains_detectable(self):
245+
content = "| A | B |\n|---|---|\n| note" + " " * HORIZONTAL_RUN_CHARS + "hidden | x |\n"
246+
247+
runs = detect_whitespace_padding(content, file_type="markdown")
248+
249+
assert any(run.kind == "horizontal" and run.start_line == 3 for run in runs)
250+
251+
def test_markdown_table_unusual_padding_remains_detectable(self):
252+
content = (
253+
"| Field" + "\u3000" * HORIZONTAL_RUN_CHARS + "| Value |\n"
254+
"|" + "-" * 96 + "|-------|\n"
255+
"| Name | Alice |\n"
256+
)
257+
runs = detect_whitespace_padding(content, file_type="markdown")
258+
assert any(run.kind == "horizontal" for run in runs)
259+
260+
def test_malformed_pipe_rows_do_not_exempt_padding(self):
261+
content = "| Field" + " " * 90 + "| Value |\n"
262+
runs = detect_whitespace_padding(content, file_type="markdown")
263+
assert any(run.kind == "horizontal" for run in runs)
264+
213265

214266
def _block_only_padding(lines: int, chars_per_line: int) -> str:
215267
"""Build a contiguous whitespace block that does NOT trip vertical/horizontal.

0 commit comments

Comments
 (0)