import os
templates_to_update = [
'src/main/resources/templates/dao-tao-detail.html',
'src/main/resources/templates/posts/full-width.html',
'src/main/resources/templates/posts/livestream.html',
'src/main/resources/templates/posts/sidebar.html',
'src/main/resources/templates/posts/standard.html'
]
switch_script = """
"""
for filepath in templates_to_update:
if not os.path.exists(filepath):
continue
with open(filepath, 'r', encoding='utf-8') as f:
content = f.read()
# First, remove the script from wherever it is (at the bottom)
if "document.addEventListener('click', function(e) {" in content:
# Find the script block
start_idx = content.find("document.addEventListener('click', function(e) {")
script_start = content.rfind("", start_idx) + 9
# Remove it
content = content[:script_start].rstrip() + "\n" + content[script_end:].lstrip()
# Now find where layout:fragment="content" ends
# We can just look for the last closing inside the document before