# -*- 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'' not in c:
c = c.replace('\r\n', '\n')
pattern = re.compile(rf'(
[^<]*{re.escape(name)}[^<]* | \s*[^<]* | \s*[^<]* | \s*)準備中( | )', re.DOTALL)
if pattern.search(c):
c = pattern.sub(rf'\g<1>攻略を見る ➜\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'{sb_name}'
if new_sb not in c:
c = c.replace('\r\n', '\n')
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: '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/ultima_weapon.html', open('trials/ultima_weapon.html', 'r', encoding='utf-8', errors='ignore').read())
update_trial_entry('アルテマウェポン破壊作戦', 'ultima_weapon.html', 'アルテマウェポン破壊作戦 (Lv50)', 'garuda.html')