|
|
@@ -150,8 +150,8 @@ def generate_post_detail_html(post_data: Dict, post_idx: int) -> str:
|
|
|
# 生成缩略图HTML
|
|
|
thumbnail_html = ""
|
|
|
if images and len(images) > 0:
|
|
|
- # 使用第一张图片作为缩略图
|
|
|
- thumbnail_html = f'<img src="{images[0]}" class="post-card-thumbnail" alt="缩略图">'
|
|
|
+ # 使用第一张图片作为缩略图,添加懒加载
|
|
|
+ thumbnail_html = f'<img src="{images[0]}" class="post-card-thumbnail" alt="缩略图" loading="lazy">'
|
|
|
else:
|
|
|
thumbnail_html = '<div class="post-card-thumbnail-placeholder">📄</div>'
|
|
|
|
|
|
@@ -3275,11 +3275,11 @@ def generate_combined_html(posts_data: List[Dict], category_mapping: Dict = None
|
|
|
try {{
|
|
|
const postData = JSON.parse(postDataStr);
|
|
|
|
|
|
- // 生成图片HTML
|
|
|
+ // 生成图片HTML(添加懒加载)
|
|
|
let imagesHtml = '';
|
|
|
if (postData.images && postData.images.length > 0) {{
|
|
|
imagesHtml = postData.images.map(img =>
|
|
|
- `<img src="${{img}}" class="post-detail-image" alt="图片">`
|
|
|
+ `<img src="${{img}}" class="post-detail-image" alt="图片" loading="lazy">`
|
|
|
).join('');
|
|
|
}} else {{
|
|
|
imagesHtml = '<div style="text-align: center; color: #9ca3af; padding: 40px;">暂无图片</div>';
|
|
|
@@ -3658,6 +3658,42 @@ def generate_combined_html(posts_data: List[Dict], category_mapping: Dict = None
|
|
|
return html
|
|
|
|
|
|
|
|
|
+def minify_html(html: str) -> str:
|
|
|
+ """压缩HTML,去除多余空格和换行"""
|
|
|
+ import re
|
|
|
+
|
|
|
+ # 保护script和style标签内容
|
|
|
+ scripts = []
|
|
|
+ styles = []
|
|
|
+
|
|
|
+ def save_script(match):
|
|
|
+ scripts.append(match.group(0))
|
|
|
+ return f"___SCRIPT_{len(scripts)-1}___"
|
|
|
+
|
|
|
+ def save_style(match):
|
|
|
+ styles.append(match.group(0))
|
|
|
+ return f"___STYLE_{len(styles)-1}___"
|
|
|
+
|
|
|
+ # 保存script和style
|
|
|
+ html = re.sub(r'<script[^>]*>.*?</script>', save_script, html, flags=re.DOTALL)
|
|
|
+ html = re.sub(r'<style[^>]*>.*?</style>', save_style, html, flags=re.DOTALL)
|
|
|
+
|
|
|
+ # 去除HTML注释
|
|
|
+ html = re.sub(r'<!--.*?-->', '', html, flags=re.DOTALL)
|
|
|
+
|
|
|
+ # 去除多余空格和换行
|
|
|
+ html = re.sub(r'\s+', ' ', html)
|
|
|
+ html = re.sub(r'>\s+<', '><', html)
|
|
|
+
|
|
|
+ # 恢复script和style
|
|
|
+ for i, script in enumerate(scripts):
|
|
|
+ html = html.replace(f"___SCRIPT_{i}___", script)
|
|
|
+ for i, style in enumerate(styles):
|
|
|
+ html = html.replace(f"___STYLE_{i}___", style)
|
|
|
+
|
|
|
+ return html.strip()
|
|
|
+
|
|
|
+
|
|
|
def main():
|
|
|
"""主函数"""
|
|
|
script_dir = Path(__file__).parent
|
|
|
@@ -3692,12 +3728,41 @@ def main():
|
|
|
print(f"\n生成合并的 HTML...")
|
|
|
html_content = generate_combined_html(posts_data, category_mapping, source_mapping)
|
|
|
|
|
|
- print(f"保存到: {output_file}")
|
|
|
+ # 保存原始版本
|
|
|
+ print(f"保存原始HTML到: {output_file}")
|
|
|
with open(output_file, "w", encoding="utf-8") as f:
|
|
|
f.write(html_content)
|
|
|
|
|
|
- print(f"\n完成! 可视化文件已保存")
|
|
|
- print(f"请在浏览器中打开: {output_file}")
|
|
|
+ original_size = len(html_content) / 1024 / 1024
|
|
|
+ print(f"原始HTML大小: {original_size:.1f} MB")
|
|
|
+
|
|
|
+ # 压缩HTML
|
|
|
+ print(f"\n压缩HTML...")
|
|
|
+ minified_html = minify_html(html_content)
|
|
|
+
|
|
|
+ minified_file = data_dir / "当前帖子_how解构结果_可视化.min.html"
|
|
|
+ print(f"保存压缩HTML到: {minified_file}")
|
|
|
+ with open(minified_file, "w", encoding="utf-8") as f:
|
|
|
+ f.write(minified_html)
|
|
|
+
|
|
|
+ minified_size = len(minified_html) / 1024 / 1024
|
|
|
+ print(f"压缩HTML大小: {minified_size:.1f} MB (减少 {(1 - minified_size/original_size)*100:.1f}%)")
|
|
|
+
|
|
|
+ # Gzip压缩
|
|
|
+ import gzip
|
|
|
+ print(f"\n生成Gzip压缩版本...")
|
|
|
+ gzip_file = data_dir / "当前帖子_how解构结果_可视化.html.gz"
|
|
|
+ with gzip.open(gzip_file, "wb") as f:
|
|
|
+ f.write(minified_html.encode('utf-8'))
|
|
|
+
|
|
|
+ gzip_size = gzip_file.stat().st_size / 1024 / 1024
|
|
|
+ print(f"Gzip压缩大小: {gzip_size:.1f} MB (比原始减少 {(1 - gzip_size/original_size)*100:.1f}%)")
|
|
|
+
|
|
|
+ print(f"\n完成! 生成了3个版本:")
|
|
|
+ print(f"1. 原始版本: {output_file} ({original_size:.1f} MB)")
|
|
|
+ print(f"2. 压缩版本: {minified_file} ({minified_size:.1f} MB)")
|
|
|
+ print(f"3. Gzip版本: {gzip_file} ({gzip_size:.1f} MB)")
|
|
|
+ print(f"\n建议分享: {gzip_file.name} (浏览器可直接打开)")
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|