# -*- coding: utf-8 -*-
import os
import re
def clean_and_save(path, content):
content = content.replace('\r\n', '\n').replace('\r', '\n')
lines = [line.rstrip() for line in content.split('\n')]
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
while result_lines and result_lines[-1] == '':
result_lines.pop()
text = '\r\n'.join(result_lines) + '\r\n'
text = text.lstrip('\ufeff')
with open(path, 'wb') as f:
f.write(b'\xef\xbb\xbf' + text.encode('utf-8'))
def add_dungeon_entry(name, level, desc, url, sb_name, prev_url):
# 1. dungeons/index.html
path_idx = 'dungeons/index.html'
with open(path_idx, 'rb') as f:
c = f.read().decode('utf-8', errors='ignore')
if url not in c:
c = c.replace('\r\n', '\n')
target = f'攻略を見る ➜\n '
new_row = f'''\n
| {name} |
{level} |
{desc} |
攻略を見る ➜ |
'''
if target in c:
c = c.replace(target, target + new_row)
else:
c = re.sub(rf'(]*>.*? | \s*)', r'\1' + new_row, c, flags=re.DOTALL)
clean_and_save(path_idx, c)
print(f"Added {name} to {path_idx}")
# 2. common/sidebar.html
path_sb = 'common/sidebar.html'
with open(path_sb, 'rb') as f:
c = f.read().decode('utf-8', errors='ignore')
new_sb = f'{sb_name}'
if new_sb not in c:
c = c.replace('\r\n', '\n')
target = f''
c = re.sub(rf'(.*?)', r'\1\n ' + new_sb, c)
clean_and_save(path_sb, c)
print(f"Added {sb_name} to {path_sb}")
# 3. common/js/wiki_common.js
path_js = 'common/js/wiki_common.js'
with open(path_js, 'rb') as f:
c = f.read().decode('utf-8', errors='ignore')
new_js = f"{{ text: '{sb_name}', path: 'dungeons/{url}' }}"
if url not in c:
c = c.replace('\r\n', '\n')
c = re.sub(rf"(\{{ text: '[^']*', path: 'dungeons/{re.escape(prev_url)}' \}})", r"\1,\n " + new_js, c)
clean_and_save(path_js, c)
print(f"Added {sb_name} to {path_js}")
if __name__ == '__main__':
# カッターズクライの正規化とリンク追加
clean_and_save('dungeons/cutter_cry.html', open('dungeons/cutter_cry.html', 'r', encoding='utf-8', errors='ignore').read())
add_dungeon_entry(
'流砂迷宮 カッターズクライ',
'Lv38 (新生2.0)',
'【キマイラ雷電・氷結の原点】 山羊(青)は離れる、羊(紫)は足元安地。流砂ワープ進行。',
'cutter_cry.html',
'カッターズクライ (Lv38)',
'sunken_temple.html'
)