Remove table columns (r9,r8,r7,r6)

Allow conditional removal of table columns when used in different contexts.

Change-Id: Ie6708a551ad5c0b6ab5125d241d94de8c15026e5
Signed-off-by: Ron Stone <ronald.stone@windriver.com>
This commit is contained in:
Ron Stone 2024-10-18 15:34:10 +00:00
parent 257bd3e728
commit 513d86154d
2 changed files with 70 additions and 1 deletions

69
hide-table-columns.py Normal file
View File

@ -0,0 +1,69 @@
import sys
from bs4 import BeautifulSoup
def remove_column_from_tables(file_path):
with open(file_path, 'r', encoding='utf-8') as file:
soup = BeautifulSoup(file, 'lxml')
# Find column to delete
column_tag = soup.find('meta', attrs={'name': 'remove-column-from-html-table'})
if column_tag:
column_names = column_tag.get('content').split(",")
else:
print(f"No column to remove specified in '{file_path}' header")
sys.exit(1)
# Remove empty rows?
row_tag = soup.find('meta', attrs={'name': 'remove-column-emptied-row'})
if row_tag:
empty_rows = 1
else:
empty_rows = 0
for column_name in column_names:
with open(file_path, 'r', encoding='utf-8') as file:
soup = BeautifulSoup(file, 'lxml')
print(f"Removing column '{column_name}' from '{file_path}'")
tables = soup.find_all('table')
for table in tables:
headers = table.find_all('th')
column_index = None
for index, header in enumerate(headers):
if header.get_text(strip=True) == column_name:
column_index = index
break
if column_index is not None:
# for header in headers:
# header.extract()
rows = table.find_all('tr')
for row in rows:
columns = row.find_all(['td', 'th'])
if column_index < len(columns):
columns[column_index].extract()
# Clean up rows that have become empty
if int(empty_rows) == 1:
for row in rows:
if not row.find_all(['td', 'th']):
row.decompose()
with open(file_path, 'w', encoding='utf-8') as file:
file.write(str(soup))
if int(empty_rows) == 1:
print("... removed rows made empty by column removal")
if __name__ == '__main__':
if len(sys.argv) < 2:
print("Usage: python remove_column.py <html_file> <html_file> ...")
sys.exit(0)
for html_file in sys.argv[1:]:
remove_column_from_tables(html_file)

View File

@ -34,9 +34,9 @@ commands =
bash hw-updates.sh
# bash hide-empty-rows.sh doc/build/html
bash -c 'python hide-empty-rows.py $(grep -rl --include="*.html" "post-build-hide-empty-table-rows" doc/build/html/*)'
bash -c 'python hide-table-columns.py $(grep -rl --include="*.html" remove-column-from-html-table doc/build/html/*)'
bash htmlChecks.sh doc/build/html
[testenv:docs]
deps =
# -c{env:TOX_CONSTRAINTS_FILE:doc/upper-constraints.txt}