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