ba-cafe

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

wiki2convert.py (2261B)


      1 import re
      2 
      3 # 1. ここにWikiのテーブル行を貼り付けてください
      4 wiki_data = """
      5 
      6 """
      7 
      8 def generate_ts_file(raw_text, filename="converted_characters.txt"):
      9     # 列構成の定義 (インデックス, 終了インデックス, レーティング)
     10     # 0列目は名前なので除外、1列目からカウント
     11     col_definitions = [
     12         (1, 2, 'ss'),  # 高級贈り物 特大 (2列)
     13         (3, 4, 'sa'),  # 高級贈り物 大 (2列)
     14         (5, 7, 'na'),  # 通常贈り物 大 (3列)
     15         (8, 15, 'nb')  # 通常贈り物 中 (8列)
     16     ]
     17 
     18     lines = raw_text.strip().split('\n')
     19     formatted_output = []
     20 
     21     for line in lines:
     22         if not line.strip() or line.startswith('|~') or 'h' in line.split('|')[-1]:
     23             continue
     24 
     25         # セルに分割
     26         cells = [c.strip() for c in line.strip('|').split('|')]
     27         
     28         # キャラクター名の抽出
     29         name_match = re.search(r'\[\[([^\]]+)\]\]$', cells[0])
     30         name = name_match.group(1) if name_match else "unknown"
     31         
     32         # IDの生成 (適宜調整してください)
     33         char_id = name.lower().replace('(', '_').replace(')', '')
     34 
     35         # ギフトレーティングの解析
     36         gift_ratings = []
     37         for start, end, rating in col_definitions:
     38             for i in range(start, end + 1):
     39                 if i < len(cells) and cells[i]:
     40                     # アイテム名の抽出
     41                     item_name = re.search(r',([^,)]+)\);', cells[i])
     42                     if item_name:
     43                         gift_ratings.append(f"      '{item_name.group(1)}': '{rating}',")
     44 
     45         # TypeScript形式の文字列組み立て
     46         char_block = [
     47             f"  {{",
     48             f"    id: '{char_id}',",
     49             f"    name: '{name}',",
     50             f"    giftRatings: {{",
     51             *gift_ratings,
     52             f"    }} as Record<string, keyof typeof GIFT_EXP>",
     53             f"  }},"
     54         ]
     55         formatted_output.append("\n".join(char_block))
     56 
     57     # ファイル書き出し
     58     with open(filename, "w", encoding="utf-8") as f:
     59         f.write("\n".join(formatted_output))
     60     
     61     print(f"成功: {filename} に書き出しました。")
     62 
     63 if __name__ == "__main__":
     64     generate_ts_file(wiki_data)