Files
ff14-guide/tools/convert_all_to_template.py
T

164 lines
6.8 KiB
Python

import os
import re
import sys
sys.stdout.reconfigure(encoding='utf-8')
def extract_content(html):
# 1. titleの抽出
title_match = re.search(r'<title>(.*?)</title>', html, re.DOTALL | re.IGNORECASE)
title = title_match.group(1).strip() if title_match else "FF14 攻略Wiki"
# 2. meta descriptionの抽出
desc_match = re.search(r'<meta\s+name=["\']description["\']\s+content=["\'](.*?)["\']', html, re.DOTALL | re.IGNORECASE)
if not desc_match:
desc_match = re.search(r'<meta\s+content=["\'](.*?)["\']\s+name=["\']description["\']', html, re.DOTALL | re.IGNORECASE)
meta_desc = f' <meta name="description" content="{desc_match.group(1).strip()}">\n' if desc_match else ""
# 3. メインコンテンツの抽出
# パターンA: <main id="wiki-main"> ... </main>
main_match = re.search(r'<main\s+id=["\']wiki-main["\']>(.*?)</main>', html, re.DOTALL | re.IGNORECASE)
if main_match:
return title, meta_desc, main_match.group(1).strip(), "main#wiki-main"
# パターンB: <main class=["\']wiki-main["\']>(.*?)</main>
main_match = re.search(r'<main\s+class=["\']wiki-main["\']>(.*?)</main>', html, re.DOTALL | re.IGNORECASE)
if main_match:
return title, meta_desc, main_match.group(1).strip(), "main.wiki-main"
# パターンC: <main id=["\']wiki-main-container["\']>(.*?)</main>
main_match = re.search(r'<main\s+id=["\']wiki-main-container["\']>(.*?)</main>', html, re.DOTALL | re.IGNORECASE)
if main_match:
return title, meta_desc, main_match.group(1).strip(), "main#wiki-main-container"
# パターンD: <main id=["\']wiki-content["\']>(.*?)</main>
main_match = re.search(r'<main\s+id=["\']wiki-content["\']>(.*?)</main>', html, re.DOTALL | re.IGNORECASE)
if main_match:
content = main_match.group(1).strip()
content = re.sub(r'<div\s+id=["\']wiki-breadcrumb["\']>\s*</div>', '', content)
return title, meta_desc, content.strip(), "main#wiki-content"
# パターンE: <div class=["\']wiki-content["\']>(.*?)</div>\s*</main>
main_match = re.search(r'<div\s+class=["\']wiki-content["\']>(.*?)</div>\s*</main>', html, re.DOTALL | re.IGNORECASE)
if main_match:
return title, meta_desc, main_match.group(1).strip(), "div.wiki-content"
return title, meta_desc, None, "UNKNOWN"
def build_html(filepath, html):
title, meta_desc, content, pattern = extract_content(html)
if not content:
raise ValueError(f"Failed to extract content from {filepath}")
# パンくずリストの整備
has_breadcrumbs = ('wiki-breadcrumbs' in content) or ('wiki-breadcrumb' in content)
is_index = os.path.basename(filepath) == 'index.html'
if not has_breadcrumbs and not is_index:
genre = "ダンジョン攻略" if "dungeons" in filepath else ("レイド攻略" if "raids" in filepath else "討滅戦攻略")
clean_title = re.sub(r'【.*?】', '', title).replace('完全攻略', '').replace('攻略', '').strip()
breadcrumb = f' <div class="wiki-breadcrumbs">\n <a href="../index.html">トップ</a> &gt; <a href="index.html">{genre}</a> &gt; <strong>{clean_title}</strong>\n </div>\n\n'
content = breadcrumb + content
else:
# id="wiki-breadcrumb" を class="wiki-breadcrumbs" に統一
content = re.sub(r'<div\s+id=["\']wiki-breadcrumb["\']>', '<div class="wiki-breadcrumbs">', content)
# 空のパンくずの場合は置換
if re.search(r'<div\s+class=["\']wiki-breadcrumbs["\']>\s*</div>', content):
genre = "ダンジョン攻略" if "dungeons" in filepath else ("レイド攻略" if "raids" in filepath else "討滅戦攻略")
clean_title = re.sub(r'【.*?】', '', title).replace('完全攻略', '').replace('攻略', '').strip()
new_bc = f'<div class="wiki-breadcrumbs">\n <a href="../index.html">トップ</a> &gt; <a href="index.html">{genre}</a> &gt; <strong>{clean_title}</strong>\n </div>'
content = re.sub(r'<div\s+class=["\']wiki-breadcrumbs["\']>\s*</div>', new_bc, content)
template = f'''<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate">
<meta http-equiv="Pragma" content="no-cache">
<meta http-equiv="Expires" content="0">
<title>{title}</title>
{meta_desc} <link rel="stylesheet" href="../common/css/seesaa_neutral_gray.css">
</head>
<body>
<div id="wiki-wrapper">
<!-- 共通ヘッダー&グローバルナビ(iframe共通化) -->
<iframe src="../common/header.html" class="wiki-header-frame" frameborder="0" scrolling="no"></iframe>
<!-- 2カラムコンテンツ -->
<div id="wiki-container">
<!-- 共通左サイドバー(iframe共通化) -->
<div id="wiki-sidebar">
<iframe src="../common/sidebar.html" class="wiki-sidebar-frame" frameborder="0"></iframe>
</div>
<main id="wiki-main">
{content}
</main>
</div>
<!-- フッター -->
<footer id="wiki-footer">
<p>&copy; SQUARE ENIX CO., LTD. All Rights Reserved. | FINAL FANTASY XIV</p>
<p>FF14 攻略Wiki (Seesaa Wiki ニュートラルグレー テンプレート完全準拠)</p>
</footer>
</div>
<script src="../common/js/wiki_common.js"></script>
</body>
</html>
'''
return template
def normalize_text(text):
lines = text.splitlines()
cleaned = []
prev_blank = False
for line in lines:
stripped = line.rstrip()
is_blank = (len(stripped) == 0)
if is_blank and prev_blank:
continue
cleaned.append(stripped)
prev_blank = is_blank
res = '\r\n'.join(cleaned)
if not res.endswith('\r\n'):
res += '\r\n'
return res
def process_file(filepath):
with open(filepath, 'r', encoding='utf-8', errors='ignore') as fp:
html = fp.read()
new_html = build_html(filepath, html)
normalized = normalize_text(new_html)
with open(filepath, 'wb') as fp:
fp.write(b'\xef\xbb\xbf')
fp.write(normalized.encode('utf-8'))
def main():
target_dirs = ['dungeons', 'raids', 'trials']
processed_files = []
for d in target_dirs:
for f in os.listdir(d):
if f.endswith('.html'):
path = os.path.join(d, f).replace('\\', '/')
with open(path, 'r', encoding='utf-8', errors='ignore') as fp:
content = fp.read()
has_iframe_header = 'wiki-header-frame' in content
has_iframe_sidebar = 'wiki-sidebar-frame' in content
if not (has_iframe_header and has_iframe_sidebar):
process_file(path)
processed_files.append(path)
print(f"変換完了ファイル総数: {len(processed_files)}")
if __name__ == '__main__':
main()