#!/usr/bin/env python3 """Combine all numbered page files into a single index.html.""" import os import re def build_index(title, DIR): # Collect numbered pages, sorted numerically page_nums = sorted( int(m.group(1)) for f in os.listdir(DIR) if (m := re.match(r'^(\d+)\.html$', f)) ) def read_page(n): with open(os.path.join(DIR, f'{n}.html'), encoding='utf-8') as f: content = f.read() content = re.sub(r'[ \t]*]+>\n?', '', content) return content.strip() sections = [(n, read_page(n)) for n in page_nums] sidebar_items = '\n '.join( f'
  • ' for n in page_nums ) sections_html = '\n\n '.join( f'
    \n{content}\n
    ' for n, content in sections ) pages_js = '[' + ','.join(str(n) for n in page_nums) + ']' html = f'''\ {title} ''' html += f'''
    {sections_html}
    ''' out = os.path.join(DIR, 'index.html') with open(out, 'w', encoding='utf-8') as f: f.write(html) print(f'Generated {out} with {len(page_nums)} pages ({os.path.getsize(out) // 1024} KB)') build_index("Fabula Ultima - Core Rulebook", "./books/core") build_index("Fabula Ultima - Natural Fantasy Atlas", "./books/natural-fantasy-atlas")