pathScripts/tablemd

25 lines
730 B
Python
Executable file

#!/usr/bin/env python3
import sys
import re
def to_markdown_table(text):
lines = text.strip().split('\n')
# Using regex to split while preserving multi-word columns
pattern = r'\s{2,}' # Two or more spaces
rows = [re.split(pattern, line.strip()) for line in lines]
# Create the markdown header row
header = ' | '.join(rows[0])
# Create separator row with correct number of columns
separator = ' | '.join(['---'] * len(rows[0]))
# Create data rows
data_rows = [' | '.join(row) for row in rows[1:]]
# Combine all parts
return f"| {header} |\n| {separator} |\n" + \
'\n'.join(f"| {row} |" for row in data_rows)
print(to_markdown_table(sys.stdin.read()))