102 lines
3.7 KiB
Python
102 lines
3.7 KiB
Python
from bs4 import BeautifulSoup
|
|
import re
|
|
|
|
# Read template
|
|
with open("html_snippets/content_main_blocks/temp", "r", encoding="utf-8") as f:
|
|
html = f.read()
|
|
|
|
# Strip styles and scripts (we can just remove script tags by regex)
|
|
html = re.sub(r"<script.*?</script>", "", html, flags=re.DOTALL)
|
|
html = re.sub(r" style=\"[^\"]*\"", "", html)
|
|
|
|
soup = BeautifulSoup(html, 'html.parser')
|
|
|
|
# Replace doctor text
|
|
# Find "Đào Thị Thanh An"
|
|
for el in soup.find_all(string=re.compile("Đào Thị Thanh An")):
|
|
parent = el.parent
|
|
parent.clear()
|
|
span = soup.new_tag("span")
|
|
span['th:text'] = '${doctor.name}'
|
|
span.string = "Tên Bác sĩ"
|
|
parent.append(span)
|
|
|
|
# Find "TS BS."
|
|
for el in soup.find_all(string=re.compile("TS BS.")):
|
|
parent = el.parent
|
|
parent.clear()
|
|
span = soup.new_tag("span")
|
|
span['th:text'] = '${doctor.title}'
|
|
span.string = "Học hàm/Học vị"
|
|
parent.append(span)
|
|
|
|
# Find "Khoa Khám bệnh"
|
|
for el in soup.find_all(string=re.compile("Khoa Khám bệnh")):
|
|
parent = el.parent
|
|
parent.clear()
|
|
span = soup.new_tag("span")
|
|
span['th:text'] = '${specialtyName}'
|
|
span.string = "Chuyên khoa"
|
|
parent.append(span)
|
|
|
|
# Find Avatar img
|
|
img = soup.find('img', alt="Avatar Bác sĩ")
|
|
if img:
|
|
img['th:src'] = '${doctor.avatarUrl != null and !doctor.avatarUrl.isEmpty() ? doctor.avatarUrl : \'/images/default-avatar.png\'}'
|
|
img['style'] = "width: 100%; height: 100%; object-fit: contain;"
|
|
|
|
# Find Đặt lịch khám href
|
|
for a in soup.find_all('a', href="/dat-lich-kham"):
|
|
del a['href']
|
|
a['th:href'] = "${doctor.bookingUrl != null and !doctor.bookingUrl.isEmpty() ? doctor.bookingUrl : '#'}"
|
|
|
|
# Find accordions
|
|
accordions = soup.find_all('div', class_='jam-accordion')
|
|
for acc in accordions:
|
|
title_div = acc.find('div', class_='title-3')
|
|
if not title_div:
|
|
continue
|
|
title = title_div.get_text(strip=True)
|
|
content_div = acc.find('div', class_='jam-accordion-content')
|
|
if not content_div:
|
|
continue
|
|
|
|
# Clear original hardcoded list content
|
|
content_div.clear()
|
|
# Create replacement div
|
|
new_div = soup.new_tag("div")
|
|
|
|
if "Quá trình đào tạo" in title:
|
|
acc['th:if'] = "${doctor.education != null and !doctor.education.isEmpty()}"
|
|
new_div['class'] = "prose doctor-info-prose transition-all duration-200 !text-[14px] xl:m-3 m-2"
|
|
new_div['th:utext'] = "${doctor.education}"
|
|
content_div.append(new_div)
|
|
elif "Quá trình công tác" in title:
|
|
acc['th:if'] = "${doctor.workExperience != null and !doctor.workExperience.isEmpty()}"
|
|
new_div['class'] = "prose doctor-info-prose transition-all duration-200 !text-[14px] space-y-2 xl:m-3 m-2"
|
|
new_div['th:utext'] = "${doctor.workExperience}"
|
|
content_div.append(new_div)
|
|
elif "Hiệp hội chuyên môn" in title:
|
|
title_div.string = "Hiệp hội chuyên môn & Thành tựu"
|
|
acc['th:if'] = "${doctor.achievements != null and !doctor.achievements.isEmpty()}"
|
|
new_div['class'] = "prose doctor-info-prose transition-all duration-200 !text-[14px] xl:m-3 m-2"
|
|
new_div['th:utext'] = "${doctor.achievements}"
|
|
content_div.append(new_div)
|
|
|
|
|
|
# Get inner HTML of <main>
|
|
main_tag = soup.find('main')
|
|
main_content = "".join(str(item) for item in main_tag.contents)
|
|
|
|
# Read original target template
|
|
with open("src/main/resources/templates/doctor-detail.html", "r", encoding="utf-8") as tf:
|
|
template = tf.read()
|
|
|
|
# Replace content inside <main>
|
|
new_template = re.sub(r"(<main[^>]*>).*?(</main>)", r"\1" + main_content.replace("\\", "\\\\") + r"\2", template, flags=re.DOTALL)
|
|
|
|
with open("src/main/resources/templates/doctor-detail.html", "w", encoding="utf-8") as tf:
|
|
tf.write(new_template)
|
|
|
|
print("Fixed layout using BeautifulSoup!")
|