Created
July 17, 2026 20:56
-
-
Save chapmanjacobd/a238357d3f0f7b43449bd5078a570122 to your computer and use it in GitHub Desktop.
HowToWinAtGobletGrotto combine supplementary pages
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import os | |
| import re | |
| import glob | |
| base_dir = '/home/xk/Downloads/WIN/HowToWinAtGobletGrotto/BOOK' | |
| par_dir = os.path.join(base_dir, 'par') | |
| out_file = os.path.join(base_dir, 'supplementary.html') | |
| with open(os.path.join(base_dir, 'paragraphs.html'), 'r', encoding='ISO-8859-1') as f: | |
| paragraphs_html = f.read() | |
| # Replace links in paragraphs.html | |
| paragraphs_html = re.sub(r'href="par/([^"]+)\.html"', r'href="#\1"', paragraphs_html) | |
| # Extract body contents of paragraphs.html | |
| m = re.search(r'(.*<body[^>]*>)(.*?)(</body>.*)', paragraphs_html, re.IGNORECASE | re.DOTALL) | |
| if not m: | |
| print("Could not parse paragraphs.html") | |
| exit(1) | |
| head = m.group(1) | |
| toc_body = m.group(2) | |
| tail = m.group(3) | |
| # Get all files in par/ | |
| files = sorted([f for f in os.listdir(par_dir) if f.endswith('.html')]) | |
| combined_body = toc_body + "\n<hr>\n<h1>SUPPLEMENTARY PAGES</h1>\n" | |
| for f in files: | |
| file_id = f[:-5] # remove .html | |
| filepath = os.path.join(par_dir, f) | |
| with open(filepath, 'r', encoding='ISO-8859-1') as html_file: | |
| content = html_file.read() | |
| # Extract body | |
| bm = re.search(r'<body([^>]*)>(.*?)</body>', content, re.IGNORECASE | re.DOTALL) | |
| if bm: | |
| body_attrs = bm.group(1) | |
| inner_html = bm.group(2) | |
| # Extract background if any | |
| bg_match = re.search(r'background="([^"]+)"', body_attrs, re.IGNORECASE) | |
| bg_style = "" | |
| if bg_match: | |
| bg_url = bg_match.group(1).replace('../', '') | |
| bg_style = f"background-image: url('{bg_url}'); " | |
| # Strip absolute positioning | |
| inner_html = re.sub(r'position:\s*absolute;?', 'position: relative;', inner_html, flags=re.IGNORECASE) | |
| inner_html = re.sub(r'left:\s*-?\d+px;?', '', inner_html, flags=re.IGNORECASE) | |
| inner_html = re.sub(r'top:\s*-?\d+px;?', '', inner_html, flags=re.IGNORECASE) | |
| # Fix image and link paths | |
| inner_html = inner_html.replace('../ImportantResources/', 'ImportantResources/') | |
| # Replace links to other par files | |
| def repl_link(m): | |
| target = m.group(1) | |
| if target in files: | |
| return f'href="#{target[:-5]}"' | |
| return m.group(0) # unchanged if not a par file | |
| inner_html = re.sub(r'href="([^"]+\.html)"', repl_link, inner_html) | |
| # Append to combined body | |
| combined_body += f"<div id='{file_id}' style='{bg_style} padding: 20px; margin-bottom: 20px; min-height: 200px;'>\n" | |
| combined_body += f"<h2><a name='{file_id}'>{file_id}</a></h2>\n" | |
| combined_body += inner_html | |
| combined_body += "\n</div>\n<hr>\n" | |
| final_html = head + combined_body + tail | |
| with open(out_file, 'w', encoding='ISO-8859-1') as f: | |
| f.write(final_html) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment