78 lines
3.0 KiB
Python
78 lines
3.0 KiB
Python
# -*- 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'<a href="{prev_url}">攻略を見る ➜</a></td>\n </tr>'
|
|
new_row = f'''\n <tr>
|
|
<td><strong>{name}</strong></td>
|
|
<td>{level}</td>
|
|
<td>{desc}</td>
|
|
<td><a href="{url}">攻略を見る ➜</a></td>
|
|
</tr>'''
|
|
if target in c:
|
|
c = c.replace(target, target + new_row)
|
|
else:
|
|
c = re.sub(rf'(<td><a href="[^"]*{re.escape(prev_url)}"[^>]*>.*?</td>\s*</tr>)', 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'<li><a href="../dungeons/{url}">{sb_name}</a></li>'
|
|
if new_sb not in c:
|
|
c = c.replace('\r\n', '\n')
|
|
c = re.sub(rf'(<li><a href="\.\./dungeons/{re.escape(prev_url)}">.*?</li>)', 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/stone_vigil.html', open('dungeons/stone_vigil.html', 'r', encoding='utf-8', errors='ignore').read())
|
|
add_dungeon_entry(
|
|
'城塞攻略 ストーンヴィジル',
|
|
'Lv41 (新生2.0)',
|
|
'<strong>【イスケビンド飛翔爆撃】</strong> チュドーユドーの振り向きブレス、上空からの絨毯爆撃回避。',
|
|
'stone_vigil.html',
|
|
'ストーンヴィジル (Lv41)',
|
|
'cutter_cry.html'
|
|
)
|