From 513d86154d56ada87e62ed6edd84dfa380e919f0 Mon Sep 17 00:00:00 2001 From: Ron Stone Date: Fri, 18 Oct 2024 15:34:10 +0000 Subject: [PATCH] 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 --- hide-table-columns.py | 69 +++++++++++++++++++++++++++++++++++++++++++ tox.ini | 2 +- 2 files changed, 70 insertions(+), 1 deletion(-) create mode 100644 hide-table-columns.py diff --git a/hide-table-columns.py b/hide-table-columns.py new file mode 100644 index 000000000..6ac7ecc0d --- /dev/null +++ b/hide-table-columns.py @@ -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 ...") + sys.exit(0) + + for html_file in sys.argv[1:]: + remove_column_from_tables(html_file) diff --git a/tox.ini b/tox.ini index dd4651120..30cf1baea 100644 --- a/tox.ini +++ b/tox.ini @@ -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}