-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtrim_md_table.py
More file actions
62 lines (52 loc) · 2.19 KB
/
trim_md_table.py
File metadata and controls
62 lines (52 loc) · 2.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import re
def trim_md_table(markdown: str) -> str:
"""
Simplify Markdown by removing excessive whitespace from table cells
and normalizing table separator rows.
Args:
markdown: Input Markdown string with potential excessive whitespace in tables
Returns:
Cleaned Markdown with normalized table formatting
"""
lines = markdown.split("\n")
result = []
# Pattern to detect actual table rows (must have at least 2 pipes)
table_row_pattern = re.compile(r"^\s*\|.*\|")
# Pattern to detect separator cells (dashes with optional colons)
separator_pattern = re.compile(r"^:?-+:?$")
def normalize_separator_cell(cell: str) -> str:
"""Normalize a separator cell to minimal form (3 dashes)."""
cell = cell.strip()
if not separator_pattern.match(cell):
return cell
# Check alignment
starts_with_colon = cell.startswith(":")
ends_with_colon = cell.endswith(":")
if starts_with_colon and ends_with_colon:
return ":---:" # Center aligned
elif starts_with_colon:
return ":---" # Left aligned (explicit)
elif ends_with_colon:
return "---:" # Right aligned
else:
return "---" # Default (left aligned)
for line in lines:
# Check if this is actually a table row, not just any line with |
if table_row_pattern.match(line) and line.count("|") >= 2:
# Process as table row
cells = line.split("|")
# Strip whitespace from each cell
cells = [cell.strip() for cell in cells]
# Filter out empty first/last elements from leading/trailing |
cleaned_cells = [
cell for i, cell in enumerate(cells) if i > 0 and i < len(cells) - 1
]
# Normalize separator cells
cleaned_cells = [normalize_separator_cell(cell) for cell in cleaned_cells]
# Reconstruct with normalized spacing
cleaned_line = "| " + " | ".join(cleaned_cells) + " |"
result.append(cleaned_line)
else:
# Not a table row, preserve as-is
result.append(line)
return "\n".join(result)