|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Script to move footer contentinfo to main so Zoomin does render it. |
| 4 | +
|
| 5 | +Copyright (c) 2021 Nordic Semiconductor ASA |
| 6 | +SPDX-License-Identifier: LicenseRef-Nordic-5-Clause |
| 7 | +""" |
| 8 | + |
| 9 | +from bs4 import BeautifulSoup |
| 10 | +import sys |
| 11 | +import argparse |
| 12 | + |
| 13 | + |
| 14 | +def move_contentinfo_to_main(html_file, silent=False): |
| 15 | + """ |
| 16 | + Move contentinfo div from footer to bottom of main div. |
| 17 | +
|
| 18 | + Args: |
| 19 | + html_file: Path to the HTML file to process |
| 20 | + silent: If True, suppress warning messages |
| 21 | +
|
| 22 | + Returns: |
| 23 | + Modified HTML content as string |
| 24 | + """ |
| 25 | + # Read the HTML file |
| 26 | + with open(html_file, 'r', encoding='utf-8') as f: |
| 27 | + html_content = f.read() |
| 28 | + |
| 29 | + # Parse the HTML |
| 30 | + soup = BeautifulSoup(html_content, 'html.parser') |
| 31 | + |
| 32 | + # Find the footer tag |
| 33 | + footer = soup.find('footer') |
| 34 | + if not footer: |
| 35 | + if not silent: |
| 36 | + print(f"Warning: No footer tag found in {html_file}") |
| 37 | + return html_content |
| 38 | + |
| 39 | + # Find the div with role="contentinfo" within the footer |
| 40 | + contentinfo_div = footer.find('div', attrs={'role': 'contentinfo'}) |
| 41 | + if not contentinfo_div: |
| 42 | + if not silent: |
| 43 | + print(f"Warning: No div with role='contentinfo' found in footer of {html_file}") |
| 44 | + return html_content |
| 45 | + |
| 46 | + # Find the div with role="main" |
| 47 | + main_div = soup.find('div', attrs={'role': 'main'}) |
| 48 | + if not main_div: |
| 49 | + if not silent: |
| 50 | + print(f"Warning: No div with role='main' found in {html_file}") |
| 51 | + return html_content |
| 52 | + |
| 53 | + # Extract the contentinfo div from its current location |
| 54 | + contentinfo_div.extract() |
| 55 | + |
| 56 | + # Append it to the bottom of the main div |
| 57 | + main_div.append(contentinfo_div) |
| 58 | + |
| 59 | + # Return the modified HTML as string |
| 60 | + return str(soup) |
| 61 | + |
| 62 | + |
| 63 | +def main(): |
| 64 | + parser = argparse.ArgumentParser( |
| 65 | + description='Move contentinfo div from footer to bottom of main div in HTML files', |
| 66 | + allow_abbrev=False |
| 67 | + ) |
| 68 | + parser.add_argument( |
| 69 | + 'input_file', |
| 70 | + help='Input HTML file to process' |
| 71 | + ) |
| 72 | + parser.add_argument( |
| 73 | + '-o', '--output', |
| 74 | + help='Output file (default: overwrite input file)', |
| 75 | + default=None |
| 76 | + ) |
| 77 | + parser.add_argument( |
| 78 | + '--dry-run', |
| 79 | + action='store_true', |
| 80 | + help='Print the modified HTML without saving' |
| 81 | + ) |
| 82 | + parser.add_argument( |
| 83 | + '-s', '--silent', |
| 84 | + action='store_true', |
| 85 | + help='Suppress all output messages' |
| 86 | + ) |
| 87 | + |
| 88 | + args = parser.parse_args() |
| 89 | + |
| 90 | + try: |
| 91 | + # Process the HTML file |
| 92 | + modified_html = move_contentinfo_to_main(args.input_file, silent=args.silent) |
| 93 | + |
| 94 | + if args.dry_run: |
| 95 | + if not args.silent: |
| 96 | + print("Modified HTML (dry run - not saved):") |
| 97 | + print(modified_html) |
| 98 | + else: |
| 99 | + # Determine output file |
| 100 | + output_file = args.output if args.output else args.input_file |
| 101 | + |
| 102 | + # Write the modified HTML |
| 103 | + with open(output_file, 'w', encoding='utf-8') as f: |
| 104 | + f.write(modified_html) |
| 105 | + |
| 106 | + if not args.silent: |
| 107 | + print(f"Successfully processed {args.input_file}") |
| 108 | + if args.output: |
| 109 | + print(f"Output saved to {args.output}") |
| 110 | + else: |
| 111 | + print(f"File updated in place") |
| 112 | + |
| 113 | + except FileNotFoundError: |
| 114 | + if not args.silent: |
| 115 | + print(f"Error: File '{args.input_file}' not found") |
| 116 | + sys.exit(1) |
| 117 | + except Exception as e: |
| 118 | + if not args.silent: |
| 119 | + print(f"Error processing file: {e}") |
| 120 | + sys.exit(1) |
| 121 | + |
| 122 | + |
| 123 | +if __name__ == '__main__': |
| 124 | + main() |
0 commit comments