import os import re import sys sys.stdout.reconfigure(encoding='utf-8') def extract_content(html): # 1. titleの抽出 title_match = re.search(r'(.*?)', 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'\n' if desc_match else "" # 3. メインコンテンツの抽出 # パターンA:
...
main_match = re.search(r'(.*?)', html, re.DOTALL | re.IGNORECASE) if main_match: return title, meta_desc, main_match.group(1).strip(), "main#wiki-main" # パターンB:
(.*?)
main_match = re.search(r'(.*?)', html, re.DOTALL | re.IGNORECASE) if main_match: return title, meta_desc, main_match.group(1).strip(), "main.wiki-main" # パターンC:
(.*?)
main_match = re.search(r'(.*?)', html, re.DOTALL | re.IGNORECASE) if main_match: return title, meta_desc, main_match.group(1).strip(), "main#wiki-main-container" # パターンD:
(.*?)
main_match = re.search(r'(.*?)', html, re.DOTALL | re.IGNORECASE) if main_match: content = main_match.group(1).strip() content = re.sub(r'\s*', '', content) return title, meta_desc, content.strip(), "main#wiki-content" # パターンE:
(.*?)
\s* main_match = re.search(r'(.*?)\s*', 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'
\n トップ > {genre} > {clean_title}\n
\n\n' content = breadcrumb + content else: # id="wiki-breadcrumb" を class="wiki-breadcrumbs" に統一 content = re.sub(r'', '
', content) # 空のパンくずの場合は置換 if re.search(r'\s*
', content): genre = "ダンジョン攻略" if "dungeons" in filepath else ("レイド攻略" if "raids" in filepath else "討滅戦攻略") clean_title = re.sub(r'【.*?】', '', title).replace('完全攻略', '').replace('攻略', '').strip() new_bc = f'
\n トップ > {genre} > {clean_title}\n
' content = re.sub(r'\s*', new_bc, content) template = f''' {title} {meta_desc}
{content}

© SQUARE ENIX CO., LTD. All Rights Reserved. | FINAL FANTASY XIV

FF14 攻略Wiki (Seesaa Wiki ニュートラルグレー テンプレート完全準拠)

''' 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()