# -*- 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') target_str = 'タイタン討滅戦\n Lv34 (4人)\n 【外周落下死の登竜門】 ランドスライド、大地の重み、心核破壊。\n 準備中' replacement_str = f'タイタン討滅戦\n Lv34 (4人)\n 【外周落下死の登竜門】 ランドスライド、大地の重み、心核破壊。\n 攻略を見る ➜' if target_str in c: c = c.replace(target_str, replacement_str) else: c = re.sub(rf'({re.escape(name)}.*?)準備中()', rf'\g<1>攻略を見る ➜\2', c, flags=re.DOTALL) 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/titan.html', open('trials/titan.html', 'r', encoding='utf-8', errors='ignore').read()) update_trial_entry('タイタン討滅戦', 'titan.html', 'タイタン討滅戦 (Lv34)', 'ifrit.html')