90 lines
3.6 KiB
Python
90 lines
3.6 KiB
Python
import re
|
|
import io
|
|
|
|
with open('/home/x79/sisvietnamvn_01/sisvietnamvn_Trang chính thức hiện tại/Đào tạo tại UMC.html', 'r', encoding='utf-8') as f:
|
|
html = f.read()
|
|
|
|
# Extract body
|
|
body_match = re.search(r'<body[^>]*>(.*?)</body>', html, re.IGNORECASE | re.DOTALL)
|
|
content = body_match.group(1) if body_match else html
|
|
|
|
# Remove header/footer
|
|
content = re.sub(r'<header.*?</header>', '', content, flags=re.IGNORECASE | re.DOTALL)
|
|
content = re.sub(r'<footer.*?</footer>', '', content, flags=re.IGNORECASE | re.DOTALL)
|
|
# Also remove script tags at the bottom to avoid conflicts
|
|
content = re.sub(r'<script.*?</script>', '', content, flags=re.IGNORECASE | re.DOTALL)
|
|
|
|
# Find swiper-wrappers and replace their content.
|
|
# Since it's all in one line, we can use regex to find `<div class="swiper-wrapper"> ... </div>` inside the swiper.
|
|
# Wait, this is dangerous because of nested divs.
|
|
# Instead, since we know there are 5 swipers in the file:
|
|
# 1. Gallery
|
|
# 2. Chứng chỉ
|
|
# 3. Giấy chứng nhận
|
|
# 4. Quá trình thực hành (xac-nhan-thuc-hanh)
|
|
# 5. Hình ảnh thực hành (thuc-hanh / training-practical)
|
|
|
|
# A safer approach is to split the content by `<div class="swiper-wrapper">`
|
|
parts = content.split('<div class="swiper-wrapper">')
|
|
|
|
if len(parts) >= 6:
|
|
new_content = parts[0]
|
|
|
|
slugs = ['gallery', 'chung-chi', 'giay-chung-nhan', 'xac-nhan-thuc-hanh', 'hinh-anh-thuc-hanh']
|
|
|
|
for i in range(1, len(parts)):
|
|
# Find the closing </div> of the swiper-wrapper.
|
|
# This is the first </div> that balances the wrapper, or since we know it's a list of <div class="swiper-slide">,
|
|
# we can just find the end of the last swiper-slide.
|
|
part = parts[i]
|
|
|
|
# We know each wrapper is closed by </div>.
|
|
# But there are nested divs.
|
|
# A simple hack: look for the end of the last slide which is followed by </div>.
|
|
# Actually, let's just find the first `</div>` that belongs to the wrapper.
|
|
# Since the slider items end, the wrapper ends with `</div>`.
|
|
# Let's count `<div` and `</div` to find the matching closing tag.
|
|
div_count = 1
|
|
pos = 0
|
|
while div_count > 0 and pos < len(part):
|
|
next_open = part.find('<div', pos)
|
|
next_close = part.find('</div', pos)
|
|
|
|
if next_close == -1:
|
|
break
|
|
|
|
if next_open != -1 and next_open < next_close:
|
|
div_count += 1
|
|
pos = next_open + 4
|
|
else:
|
|
div_count -= 1
|
|
pos = next_close + 6
|
|
|
|
slug = slugs[i-1] if (i-1) < len(slugs) else "unknown"
|
|
replacement = f"\n<th:block th:utext=\"${{@hookManager.applyFilters('swiper_slider_items', '{slug}')}}\"></th:block>\n"
|
|
|
|
new_content += '<div class="swiper-wrapper">' + replacement + part[pos:]
|
|
|
|
else:
|
|
new_content = content
|
|
print("Warning: Did not find exactly 5 swipers. Found", len(parts) - 1)
|
|
|
|
template = f"""<!DOCTYPE html>
|
|
<html lang="en" xmlns:th="http://www.thymeleaf.org" xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
|
|
layout:decorate="~{{fragments/layout(bodyClass='umass-platform-homepage path-frontpage page-node-type-homepage homepage transparent-header')}}">
|
|
<head>
|
|
<title>Đào tạo tại UMC</title>
|
|
</head>
|
|
<body>
|
|
<div layout:fragment="content">
|
|
{new_content}
|
|
</div>
|
|
</body>
|
|
</html>
|
|
"""
|
|
|
|
with open('/home/x79/sisvietnamvn_01/sisvietnamvn_main/src/main/resources/templates/dao-tao.html', 'w', encoding='utf-8') as f:
|
|
f.write(template)
|
|
|
|
print("Created dao-tao.html successfully!")
|