Files
ff14-guide/tools/update_links.py
T

63 lines
2.5 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 update_trial_entry(name, url, sb_name, prev_url):
# 1. trials/index.html
path_idx = 'trials/index.html'
with open(path_idx, 'rb') as f:
c = f.read().decode('utf-8', errors='ignore')
if f'<a href="{url}">' not in c:
c = c.replace('\r\n', '\n')
pattern = re.compile(rf'(<td><strong>[^<]*{re.escape(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)
clean_and_save(path_idx, c)
print(f"Updated {name} in {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="../trials/{url}">{sb_name}</a></li>'
if new_sb not in c:
c = c.replace('\r\n', '\n')
c = re.sub(rf'(<li><a href="\.\./trials/{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: 'trials/{url}' }}"
if url not in c:
c = c.replace('\r\n', '\n')
c = re.sub(rf"(\{{ text: '[^']*', path: 'trials/{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('trials/garuda.html', open('trials/garuda.html', 'r', encoding='utf-8', errors='ignore').read())
update_trial_entry('ガルーダ討滅戦', 'garuda.html', 'ガルーダ討滅戦 (Lv44)', 'titan.html')