Fix indentation
Fix indentation logic in cell reconstruction Change-Id: I208c10538a7eb8144d521a75e554fc61e0111201 Signed-off-by: Ron Stone <ronald.stone@windriver.com>
This commit is contained in:
@@ -32,32 +32,80 @@ def extract_table_blocks(lines):
|
|||||||
return blocks
|
return blocks
|
||||||
|
|
||||||
def split_table_row(row_lines):
|
def split_table_row(row_lines):
|
||||||
"""Splits a table row (beginning with '*') into a list of cells."""
|
"""Splits a table row (beginning with '*') into a list of cells, preserving original line spacing."""
|
||||||
cells = []
|
import re
|
||||||
current_cell = []
|
cells = []
|
||||||
for line in row_lines:
|
current_cell = []
|
||||||
if re.match(r'^\s*\*\s+-', line): # First cell in row
|
for line in row_lines:
|
||||||
parts = re.split(r'\s*\*\s+-\s*', line, maxsplit=1)
|
if re.match(r'^\s*\*\s+-', line): # First cell in row
|
||||||
current_cell = [parts[1]]
|
# Keep only the cell content (text after "* - ")
|
||||||
elif re.match(r'^\s*-\s+', line): # New cell
|
parts = re.split(r'^\s*\*\s+-\s*', line, maxsplit=1)
|
||||||
cells.append(current_cell)
|
current_cell = [parts[1]]
|
||||||
current_cell = [line.strip()[2:]]
|
elif re.match(r'^\s*-\s+', line): # New cell
|
||||||
else:
|
# Finish previous cell and start a new one (text after "- ")
|
||||||
current_cell.append(line.strip())
|
cells.append(current_cell)
|
||||||
cells.append(current_cell)
|
parts = re.split(r'^\s*-\s+', line, maxsplit=1)
|
||||||
return cells
|
current_cell = [parts[1]]
|
||||||
|
else:
|
||||||
|
# Continuation line: keep exactly as-is (including leading spaces)
|
||||||
|
current_cell.append(line)
|
||||||
|
cells.append(current_cell)
|
||||||
|
return cells
|
||||||
|
|
||||||
def join_cells(cells, base_indent):
|
def join_cells(cells, base_indent):
|
||||||
"""Reconstructs a list-table row from cell lists."""
|
"""Reconstructs a list-table row from cell lists.
|
||||||
line = f"{base_indent}* - " + cells[0][0]
|
|
||||||
lines = [line]
|
Continuation lines are aligned to the cell's content column (the column where the
|
||||||
for line in cells[0][1:]:
|
first line's text starts), and any additional original indentation beyond that is preserved.
|
||||||
lines.append(base_indent + " " + line)
|
This also preserves the deeper indentation used by directive option lines.
|
||||||
for cell in cells[1:]:
|
"""
|
||||||
lines.append(base_indent + " - " + cell[0])
|
# In a list-table, content starts 4 characters after the list marker for both
|
||||||
for l in cell[1:]:
|
# the first cell ("* - ") and other cells (" - ").
|
||||||
lines.append(base_indent + " " + l)
|
content_col_len = len(base_indent) + 4 # baseline spaces before content in any cell
|
||||||
return lines
|
|
||||||
|
def count_leading_spaces(s: str) -> int:
|
||||||
|
n = 0
|
||||||
|
for ch in s:
|
||||||
|
if ch == ' ':
|
||||||
|
n += 1
|
||||||
|
elif ch == '\t':
|
||||||
|
# tabs aren't expected in RST, but treat as 4 spaces if present
|
||||||
|
n += 4
|
||||||
|
else:
|
||||||
|
break
|
||||||
|
return n
|
||||||
|
|
||||||
|
out = []
|
||||||
|
|
||||||
|
# First cell
|
||||||
|
first_line_text = cells[0][0].rstrip('\n')
|
||||||
|
out.append(f"{base_indent}* - {first_line_text.rstrip()}")
|
||||||
|
|
||||||
|
for line in cells[0][1:]:
|
||||||
|
if line.strip() == "":
|
||||||
|
out.append("") # preserve blank lines
|
||||||
|
continue
|
||||||
|
s = line.rstrip('\n')
|
||||||
|
lead = count_leading_spaces(s)
|
||||||
|
extra = max(0, lead - content_col_len) # extra indent beyond the baseline content column
|
||||||
|
out.append((" " * content_col_len) + (" " * extra) + s.lstrip(' '))
|
||||||
|
|
||||||
|
# Remaining cells
|
||||||
|
for cell in cells[1:]:
|
||||||
|
first_line_text = cell[0].rstrip('\n')
|
||||||
|
out.append(f"{base_indent} - {first_line_text.rstrip()}")
|
||||||
|
for line in cell[1:]:
|
||||||
|
if line.strip() == "":
|
||||||
|
out.append("")
|
||||||
|
continue
|
||||||
|
s = line.rstrip('\n')
|
||||||
|
lead = count_leading_spaces(s)
|
||||||
|
extra = max(0, lead - content_col_len)
|
||||||
|
out.append((" " * content_col_len) + (" " * extra) + s.lstrip(' '))
|
||||||
|
|
||||||
|
return out
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def process_table(table_lines, cols_to_remove_str, remove_empty_row=False):
|
def process_table(table_lines, cols_to_remove_str, remove_empty_row=False):
|
||||||
# Parse comma-separated column names
|
# Parse comma-separated column names
|
||||||
|
Reference in New Issue
Block a user