79 lines
3.1 KiB
Python
79 lines
3.1 KiB
Python
# -*- coding: utf-8 -*-
|
|
import os
|
|
import re
|
|
|
|
def normalize_text(text):
|
|
# CRLF統一
|
|
text = text.replace('\r\n', '\n').replace('\r', '\n')
|
|
lines = [line.rstrip() for line in text.split('\n')]
|
|
# 連続空行の防止(最大1行)
|
|
result_lines = []
|
|
prev_empty = False
|
|
for line in lines:
|
|
if line == '':
|
|
if not prev_empty:
|
|
result_lines.append(line)
|
|
prev_empty = True
|
|
else:
|
|
result_lines.append(line)
|
|
prev_empty = False
|
|
return '\r\n'.join(result_lines) + '\r\n'
|
|
|
|
def save_utf8_bom(path, content):
|
|
content = normalize_text(content)
|
|
# 既存の先頭BOM文字を除去してから書き込む
|
|
content = content.lstrip('\ufeff')
|
|
with open(path, 'w', encoding='utf-8-sig', newline='\r\n') as f:
|
|
f.write(content)
|
|
|
|
def update_dungeon_index(dungeon_id, dungeon_name, url):
|
|
path = 'dungeons/index.html'
|
|
if not os.path.exists(path):
|
|
return
|
|
with open(path, 'r', encoding='utf-8', errors='ignore') as f:
|
|
c = f.read()
|
|
# 該当行の「準備中」をリンクへ置換
|
|
pattern = re.compile(rf'(<td><strong>[^<]*{re.escape(dungeon_name)}[^<]*</strong></td>\s*<td>[^<]*</td>\s*<td>[^<]*</td>\s*<td>)準備中(</td>)', re.DOTALL)
|
|
if pattern.search(c):
|
|
c = pattern.sub(rf'\g<1><a href="{url}">攻略を見る ➜</a>\2', c)
|
|
save_utf8_bom(path, c)
|
|
print(f"Updated {path} for {dungeon_name}")
|
|
|
|
def update_sidebar_dungeon(name, url):
|
|
# 1. sidebar.html
|
|
path_sb = 'common/sidebar.html'
|
|
if os.path.exists(path_sb):
|
|
with open(path_sb, 'r', encoding='utf-8', errors='ignore') as f:
|
|
c = f.read()
|
|
target = '<li><a href="../dungeons/alexandria.html">記憶幻想 アレクサンドリア (Lv100)</a></li>'
|
|
new_entry = f'<li><a href="../dungeons/{url}">{name}</a></li>'
|
|
if new_entry not in c and target in c:
|
|
c = c.replace(target, f'{target}\n {new_entry}')
|
|
save_utf8_bom(path_sb, c)
|
|
print(f"Updated {path_sb} with {name}")
|
|
|
|
# 2. wiki_common.js
|
|
path_js = 'common/js/wiki_common.js'
|
|
if os.path.exists(path_js):
|
|
with open(path_js, 'r', encoding='utf-8', errors='ignore') as f:
|
|
c = f.read()
|
|
target_js = "{ text: '記憶幻想 アレクサンドリア (Lv100)', path: 'dungeons/alexandria.html' }"
|
|
new_entry_js = f"{{ text: '{name}', path: 'dungeons/{url}' }}"
|
|
if new_entry_js not in c and target_js in c:
|
|
c = c.replace(target_js, f"{target_js},\n {new_entry_js}")
|
|
save_utf8_bom(path_js, c)
|
|
print(f"Updated {path_js} with {name}")
|
|
|
|
def normalize_file(filepath):
|
|
if not os.path.exists(filepath):
|
|
return
|
|
with open(filepath, 'r', encoding='utf-8', errors='ignore') as f:
|
|
c = f.read()
|
|
save_utf8_bom(filepath, c)
|
|
print(f"Normalized {filepath}")
|
|
|
|
if __name__ == '__main__':
|
|
normalize_file('dungeons/brayflox.html')
|
|
update_dungeon_index('brayflox', 'ブレイフロクス', 'brayflox.html')
|
|
update_sidebar_dungeon('ブレイフロクスの野営地 (Lv32)', 'brayflox.html')
|