import os from bs4 import BeautifulSoup # Define paths TEMPLATES_DIR = os.path.join(os.path.dirname(__file__), '..', 'templates') MODULES_DIR = os.path.join(TEMPLATES_DIR, 'modules') INDEX_FILE = os.path.join(TEMPLATES_DIR, 'index.html') # Ensure modules directory exists os.makedirs(MODULES_DIR, exist_ok=True) # Read and parse the original index.html with open(INDEX_FILE, 'r', encoding='utf-8') as f: soup = BeautifulSoup(f, 'html.parser') # Find all module divs modules = soup.find_all('div', class_='module') # Extract CSS and JavaScript css_style = soup.find('style').prettify() javascript_block = soup.find('script', {'src': None}).prettify() def create_slug(title): """Creates a URL-friendly slug from a module title.""" # Remove emoji and leading/trailing whitespace title = ''.join(c for c in title if c.isalnum() or c.isspace()) # Replace spaces with underscores and convert to lowercase return title.strip().replace(' ', '_').lower() # Process each module for module_div in modules: h2_tag = module_div.find('h2') if not h2_tag: continue # Extract title and create a slug # Example: "🔬 Modul 1: Dokter Tanaman" -> "modul_1_dokter_tanaman" full_title = h2_tag.get_text(strip=True) simple_title = full_title.split(':')[-1].strip() slug = create_slug(simple_title) file_path = os.path.join(MODULES_DIR, f"{slug}.html") # Create a new soup for the module page module_soup = BeautifulSoup(f""" {full_title} - Agrisensa {css_style}
← Kembali ke Dasbor
""", 'html.parser') # Insert the module div into the body module_soup.body.find('div').append(module_div) # Append the JavaScript block script_tag = BeautifulSoup(javascript_block, 'html.parser') module_soup.body.append(script_tag) # Write the new file with open(file_path, 'w', encoding='utf-8') as f: f.write(str(module_soup)) print(f"Created module page: {file_path}") print("Module extraction complete.")