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", "Tên Bác sĩ") html = html.replace("TS BS.", "Học hàm/Học vị") html = html.replace("Khoa Khám bệnh", "Chuyên khoa") # Find Avatar img and replace avatar_start = html.find('', avatar_start) img_tag = html[avatar_start:avatar_end+1] if 'A17-243' in img_tag: new_img = 'Avatar Bác sĩ' html = html[:avatar_start] + new_img + html[avatar_end+1:] break avatar_start = html.find('{title_text}') if title_idx == -1: return html_str start_idx = html_str.rfind('
', 0, title_idx) content_idx = html_str.find('
', title_idx) content_start = content_idx + len('
') # We find the matching closing div for jam-accordion active. # But since we know it ends with
(content, shadow box, accordion) end_idx = html_str.find('', content_start) if end_idx == -1: return html_str prefix = html_str[:start_idx] acc_open = f'
' middle = html_str[start_idx + len('
') : content_start] new_content = f'
' 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('
Hiệp hội chuyên môn
', '
Hiệp hội chuyên môn & Thành tựu
') html = process_accordion(html, "Hiệp hội chuyên môn & 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 '', 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('') if end_main != -1: end_main += 7 new_main_html = html[start_main:end_main] # Extract template parts t_start = template.find('') 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!")