Reimplement table column and row removal to be output format agnostic. Change-Id: I4822d53d37fd4604bf45c4bc4a315c8fc904376a Signed-off-by: Ron Stone <ronald.stone@windriver.com>
		
			
				
	
	
		
			69 lines
		
	
	
		
			1.9 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
			
		
		
	
	
			69 lines
		
	
	
		
			1.9 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
import sys
 | 
						|
import re
 | 
						|
 | 
						|
def is_grid_table_line(line):
 | 
						|
    return re.match(r'^\+[-=+]+$', line.strip()) is not None
 | 
						|
 | 
						|
def is_grid_row_empty(row):
 | 
						|
    # A grid row is considered empty if the cells contain only whitespace or are just column dividers
 | 
						|
    content = re.sub(r'[+|]', '', row)
 | 
						|
    return content.strip() == ''
 | 
						|
 | 
						|
 | 
						|
def remove_empty_grid_rows(lines):
 | 
						|
    result = []
 | 
						|
    i = 0
 | 
						|
    while i < len(lines):
 | 
						|
        line = lines[i]
 | 
						|
 | 
						|
        if is_grid_table_line(line):
 | 
						|
            result.append(line)
 | 
						|
 | 
						|
            # Check if there's a data line and a closing border after this
 | 
						|
            if i + 2 < len(lines):
 | 
						|
                data_line = lines[i + 1]
 | 
						|
                next_border = lines[i + 2]
 | 
						|
 | 
						|
                if is_grid_table_line(next_border):
 | 
						|
                    if is_grid_row_empty(data_line):
 | 
						|
                        # Skip this row (data + border)
 | 
						|
                        i += 3
 | 
						|
                        continue
 | 
						|
                    else:
 | 
						|
                        # Keep data row and closing border
 | 
						|
                        result.append(data_line)
 | 
						|
                        result.append(next_border)
 | 
						|
                        i += 3
 | 
						|
                        continue
 | 
						|
                else:
 | 
						|
                    # Not a properly structured row — just advance
 | 
						|
                    i += 1
 | 
						|
            else:
 | 
						|
                # Not enough lines ahead — just advance
 | 
						|
                i += 1
 | 
						|
        else:
 | 
						|
            result.append(line)
 | 
						|
            i += 1
 | 
						|
 | 
						|
    return result
 | 
						|
 | 
						|
 | 
						|
def remove_empty_table_rows(filepath):
 | 
						|
    with open(filepath, 'r', encoding='utf-8') as f:
 | 
						|
        lines = f.readlines()
 | 
						|
 | 
						|
    lines = remove_empty_grid_rows(lines)
 | 
						|
 | 
						|
    with open(filepath, 'w', encoding='utf-8') as f:
 | 
						|
        f.writelines(lines)
 | 
						|
 | 
						|
if __name__ == '__main__':
 | 
						|
    if len(sys.argv) != 2:
 | 
						|
        print("Usage: python remove_empty-grid_rows.py path/to/file.rst")
 | 
						|
        sys.exit(1)
 | 
						|
 | 
						|
    filepath = sys.argv[1]
 | 
						|
    remove_empty_table_rows(filepath)
 | 
						|
    print(f"Processed: {filepath}")
 | 
						|
 |