# -*- 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 = '攻略を見る ➜\n ' target_crlf = '攻略を見る ➜\r\n ' new_row = f'''\n {name} {level} {desc} 攻略を見る ➜ ''' c = c.replace('\r\n', '\n') if target in c: c = c.replace(target, target + new_row) else: c = re.sub(r'(]*>.*?\s*)', 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'
  • {sb_name}
  • ' if new_sb not in c: c = c.replace('\r\n', '\n') target = '
  • ブレイフロクスの野営地 (Lv32)
  • ' if target in c: c = c.replace(target, f'{target}\n {new_sb}') else: c = re.sub(r'(
  • .*?
  • )', 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)', '【死の宣告・即死回避の登竜門】 光るパネルでの解除、ハチのファイナルスピア阻止、天秤の彫像。', 'sunken_temple.html', 'カルン埋没寺院 (Lv35)' )