Files
sisvietnamvn_01/sisvietnamvn_main/apply_layout_fast.py
T

86 lines
4.1 KiB
Python

with open("html_snippets/content_main_blocks/temp", "r", encoding="utf-8") as f:
html = f.read()
# Replace simple strings first (very fast)
html = html.replace("Đào Thị Thanh An", "<span th:text=\"${doctor.name}\">Tên Bác sĩ</span>")
html = html.replace("TS BS.", "<span th:text=\"${doctor.title}\">Học hàm/Học vị</span>")
html = html.replace("Khoa Khám bệnh", "<span th:text=\"${specialtyName}\">Chuyên khoa</span>")
# Find Avatar img and replace
avatar_start = html.find('<img')
while avatar_start != -1:
avatar_end = html.find('>', avatar_start)
img_tag = html[avatar_start:avatar_end+1]
if 'A17-243' in img_tag:
new_img = '<img th:src="${doctor.avatarUrl != null and !doctor.avatarUrl.isEmpty() ? doctor.avatarUrl : \'/images/default-avatar.png\'}" style="width: 100%; height: 100%; object-fit: contain;" alt="Avatar Bác sĩ" />'
html = html[:avatar_start] + new_img + html[avatar_end+1:]
break
avatar_start = html.find('<img', avatar_start + 1)
# Add href to Đặt lịch khám
html = html.replace('href="/dat-lich-kham"', 'th:href="${doctor.bookingUrl != null and !doctor.bookingUrl.isEmpty() ? doctor.bookingUrl : \'#\'}"')
def process_accordion(html_str, title_text, th_if, th_utext, extra_classes):
title_idx = html_str.find(f'<div class="title-3 text-white">{title_text}</div>')
if title_idx == -1: return html_str
start_idx = html_str.rfind('<div class="jam-accordion active">', 0, title_idx)
content_idx = html_str.find('<div class="jam-accordion-content">', title_idx)
content_start = content_idx + len('<div class="jam-accordion-content">')
# We find the matching closing div for jam-accordion active.
# But since we know it ends with </div></div></div> (content, shadow box, accordion)
end_idx = html_str.find('</div></div></div>', content_start)
if end_idx == -1: return html_str
prefix = html_str[:start_idx]
acc_open = f'<div class="jam-accordion active" th:if="{th_if}">'
middle = html_str[start_idx + len('<div class="jam-accordion active">') : content_start]
new_content = f'<div class="prose doctor-info-prose transition-all duration-200 !text-[14px] {extra_classes}" th:utext="{th_utext}"></div>'
suffix = html_str[end_idx:]
return prefix + acc_open + middle + new_content + suffix
html = process_accordion(html, "Quá trình đào tạo", "${doctor.education != null and !doctor.education.isEmpty()}", "${doctor.education}", "xl:m-3 m-2")
html = process_accordion(html, "Quá trình công tác", "${doctor.workExperience != null and !doctor.workExperience.isEmpty()}", "${doctor.workExperience}", "space-y-2 xl:m-3 m-2")
html = html.replace('<div class="title-3 text-white">Hiệp hội chuyên môn</div>', '<div class="title-3 text-white">Hiệp hội chuyên môn &amp; Thành tựu</div>')
html = process_accordion(html, "Hiệp hội chuyên môn &amp; Thành tựu", "${doctor.achievements != null and !doctor.achievements.isEmpty()}", "${doctor.achievements}", "xl:m-3 m-2")
# Strip script and style carefully without ReGex to avoid hangs
# Strip styles
while ' style="' in html:
s_start = html.find(' style="')
s_end = html.find('"', s_start + 8)
html = html[:s_start] + html[s_end+1:]
# Strip scripts
while '<script' in html:
sc_start = html.find('<script')
sc_end = html.find('</script>', sc_start)
if sc_end != -1:
html = html[:sc_start] + html[sc_end+9:]
else:
break
# Read target template
with open("src/main/resources/templates/doctor-detail.html", "r", encoding="utf-8") as tf:
template = tf.read()
# Extract new main
start_main = html.find('<main')
end_main = html.find('</main>')
if end_main != -1: end_main += 7
new_main_html = html[start_main:end_main]
# Extract template parts
t_start = template.find('<main')
t_end = template.find('</main>')
if t_end != -1: t_end += 7
new_template = template[:t_start] + new_main_html + template[t_end:]
with open("src/main/resources/templates/doctor-detail.html", "w", encoding="utf-8") as tf:
tf.write(new_template)
print("Successfully replaced layout completely without Regex!")