Files
ff14-guide/tools/update_links.py
T

61 lines
2.4 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_zorool_ja():
# 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 'href="zorool_ja.html"' not in c:
c = c.replace('\r\n', '\n')
c = re.sub(r'(ゾラージャ討滅戦</strong>.*?<td>)準備中(</td>)', r'\1<a href="zorool_ja.html">攻略を見る ➜</a>\2', c, flags=re.DOTALL)
clean_and_save(path_idx, c)
print("Updated zorool_ja in trials/index.html")
# 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 = '<li><a href="../trials/zorool_ja.html">ゾラージャ討滅戦 (Lv99)</a></li>'
if new_sb not in c:
c = c.replace('\r\n', '\n')
c = re.sub(r'(<li><a href="\.\./trials/valigarmanda\.html">.*?</li>)', r'\1\n ' + new_sb, c)
clean_and_save(path_sb, c)
print("Added zorool_ja to common/sidebar.html")
# 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 = "{ text: 'ゾラージャ討滅戦 (Lv99)', path: 'trials/zorool_ja.html' }"
if 'zorool_ja.html' not in c:
c = c.replace('\r\n', '\n')
c = re.sub(r"(\{ text: 'ヴァリガルマンダ討滅戦 \(Lv93\)', path: 'trials/valigarmanda\.html' \})", r"\1,\n " + new_js, c)
clean_and_save(path_js, c)
print("Added zorool_ja to common/js/wiki_common.js")
if __name__ == '__main__':
clean_and_save('trials/zorool_ja.html', open('trials/zorool_ja.html', 'r', encoding='utf-8', errors='ignore').read())
update_zorool_ja()