Files
ff14-guide/tools/update_links.py
T

87 lines
3.6 KiB
Python

# -*- coding: utf-8 -*-
import os
import re
def save_clean_file(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 = '\n'.join(result_lines) + '\n'
text = text.lstrip('\ufeff')
# Windows上で autocrlf=true と調和するように \r\n で書き込むか \n で書き込むかをテスト
with open(path, 'w', encoding='utf-8-sig', newline='\n') as f:
f.write(text)
def add_dungeon_entry(name, level, desc, url, sb_name):
# 1. dungeons/index.html
path_idx = 'dungeons/index.html'
with open(path_idx, 'r', encoding='utf-8', errors='ignore') as f:
c = f.read()
if url not in c:
target = '<td><a href="brayflox.html">攻略を見る ➜</a></td>\n </tr>'
target_crlf = '<td><a href="brayflox.html">攻略を見る ➜</a></td>\r\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>'''
c = c.replace('\r\n', '\n')
if target in c:
c = c.replace(target, target + new_row)
else:
c = re.sub(r'(<td><a href="[^"]*brayflox\.html"[^>]*>.*?</td>\s*</tr>)', r'\1' + new_row, c, flags=re.DOTALL)
save_clean_file(path_idx, c)
print(f"Added {name} to {path_idx}")
# 2. common/sidebar.html
path_sb = 'common/sidebar.html'
with open(path_sb, 'r', encoding='utf-8', errors='ignore') as f:
c = f.read()
new_sb = f'<li><a href="../dungeons/{url}">{sb_name}</a></li>'
if new_sb not in c:
c = c.replace('\r\n', '\n')
target = '<li><a href="../dungeons/brayflox.html">ブレイフロクスの野営地 (Lv32)</a></li>'
if target in c:
c = c.replace(target, f'{target}\n {new_sb}')
else:
c = re.sub(r'(<li><a href="\.\./dungeons/brayflox\.html">.*?</li>)', r'\1\n ' + new_sb, c)
save_clean_file(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, 'r', encoding='utf-8', errors='ignore') as f:
c = f.read()
new_js = f"{{ text: '{sb_name}', path: 'dungeons/{url}' }}"
if url not in c:
c = c.replace('\r\n', '\n')
target = "{ text: 'ブレイフロクスの野営地 (Lv32)', path: 'dungeons/brayflox.html' }"
if target in c:
c = c.replace(target, f"{target},\n {new_js}")
else:
c = re.sub(r"(\{ text: 'ブレイフロクス[^}]*brayflox\.html' \})", r"\1,\n " + new_js, c)
save_clean_file(path_js, c)
print(f"Added {sb_name} to {path_js}")
if __name__ == '__main__':
save_clean_file('dungeons/sunken_temple.html', open('dungeons/sunken_temple.html', 'r', encoding='utf-8', errors='ignore').read())
add_dungeon_entry(
'遺跡探索 カルン埋没寺院',
'Lv35 (新生2.0)',
'<strong>【死の宣告・即死回避の登竜門】</strong> 光るパネルでの解除、ハチのファイナルスピア阻止、天秤の彫像。',
'sunken_temple.html',
'カルン埋没寺院 (Lv35)'
)