import oracledb from bs4 import BeautifulSoup import re def transform_html(title, raw_html): soup = BeautifulSoup(raw_html, "html.parser") # Extract all images images = [] for img in soup.find_all('img'): src = img.get('src') if src: images.append(src) img.decompose() # Remove from flow hero_image = images[0] if len(images) > 0 else "https://sisvietnam.vn/wp-content/uploads/2021/07/DSC07517-1024x576.jpg" other_images = images[1:] # Extract Contact Info text = soup.get_text() email_match = re.search(r"(?i)Email\s*:\s*([\w\.-]+@[\w\.-]+)", text) phone_match = re.search(r"(?i)(?:SĐT|Điện thoại|ĐT)\s*:\s*([\d\.\s-]+)", text) address_match = re.search(r"(?i)Địa chỉ\s*:\s*([^\n]+)", text) contact_email = email_match.group(1).strip() if email_match else None contact_phone = phone_match.group(1).strip() if phone_match else None contact_address = address_match.group(1).strip() if address_match else None # Parse sections based on wp:list or headings sections_data = [] top_uls = soup.find_all('ul', recursive=False) if not top_uls and soup.find('div', class_='wp-content-container'): top_uls = soup.find('div', class_='wp-content-container').find_all('ul', recursive=False) for ul in top_uls: heading = "Nội dung" strong_tag = ul.find('strong') if strong_tag: heading = strong_tag.text.strip() strong_tag.decompose() content_html = "" for li in ul.find_all('li', recursive=False): inner_uls = li.find_all('ul') for inner in inner_uls: content_html += str(inner) if not content_html: content_html = str(ul) sections_data.append({ 'heading': heading, 'content': content_html }) if not sections_data: sections_data.append({ 'heading': 'Thông tin chi tiết', 'content': str(soup) }) out = "" bg_colors = ["bg-white", "bg-gray-50", "bg-white", "bg-gray-100"] for i, sec in enumerate(sections_data): bg = bg_colors[i % len(bg_colors)] img_html = "" if i < len(other_images): img_src = other_images[i] img_html = f"""