-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathcategorize_data_engineering.py
More file actions
129 lines (111 loc) · 4.7 KB
/
categorize_data_engineering.py
File metadata and controls
129 lines (111 loc) · 4.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import os
# --- 設定 ---
POSTS_DIRECTORY = 'source/_posts'
# このタグが含まれていたら対象となる
TARGET_TAG = 'マネジメント'
# 上書きする新しいカテゴリ
NEW_CATEGORY = 'Management'
# --- 設定ここまで ---
def replace_category_for_tag(file_path):
"""
ファイルのfront matterをチェックし、ターゲットタグが含まれていれば
カテゴリを指定したものに置換し、元のタグを削除する。
他の行のフォーマットは保持する。
"""
try:
with open(file_path, 'r', encoding='utf-8') as f:
lines = f.readlines()
# --- ステップ1: まず対象ファイルか判定する ---
in_front_matter = False
in_tags_list = False
has_target_tag = False
for line in lines:
if line.strip() == '---':
if in_front_matter: break
else:
in_front_matter = True
continue
if in_front_matter:
stripped_line = line.strip()
if not line.startswith((' ', '\t')):
in_tags_list = stripped_line.startswith(('tag:', 'tags:'))
if in_tags_list and stripped_line.startswith('-'):
tag_value = stripped_line[1:].strip().strip('\'"')
if tag_value == TARGET_TAG:
has_target_tag = True
break
if not has_target_tag:
return False, "対象外"
# --- ステップ2: 対象ファイルだった場合、内容を書き換える ---
new_lines = []
in_front_matter = False
in_tags_list = False
in_category_list = False
was_modified = False
for line in lines:
if line.strip() == '---':
if in_front_matter:
in_front_matter = False
in_tags_list = False
in_category_list = False
else:
in_front_matter = True
new_lines.append(line)
continue
if in_front_matter:
stripped_line = line.strip()
if not line.startswith((' ', '\t')):
if stripped_line.startswith(('tag:', 'tags:')):
in_tags_list = True
in_category_list = False
elif stripped_line.startswith(('category:', 'categories:')):
new_lines.append(f'category:\n - {NEW_CATEGORY}\n')
in_category_list = True
in_tags_list = False
was_modified = True
continue
else:
in_tags_list = False
in_category_list = False
new_lines.append(line)
continue
if in_category_list and stripped_line.startswith('-'):
was_modified = True
continue
if in_tags_list and stripped_line.startswith('-'):
tag_value = stripped_line[1:].strip().strip('\'"')
if tag_value == TARGET_TAG:
was_modified = True
continue
new_lines.append(line)
else:
new_lines.append(line)
if was_modified:
with open(file_path, 'w', encoding='utf-8') as f:
f.writelines(new_lines)
return True, "カテゴリとタグを変換しました"
else:
return False, "対象タグが見つかりませんでした"
except Exception as e:
return False, f"エラー: {e}"
def main():
"""メイン処理"""
if not os.path.isdir(POSTS_DIRECTORY):
print(f"エラー: '{POSTS_DIRECTORY}' が見つかりません。")
return
print(f"「{TARGET_TAG}」タグを持つ記事のカテゴリを「{NEW_CATEGORY}」に置換します...")
updated_files_count = 0
for root, _, files in os.walk(POSTS_DIRECTORY):
for file in files:
if file.endswith('.md'):
file_path = os.path.join(root, file)
was_updated, message = replace_category_for_tag(file_path)
if was_updated:
updated_files_count += 1
print(f"✅ {message}: {file_path}")
if updated_files_count > 0:
print(f"\n完了しました。合計 {updated_files_count} 個のファイルを修正しました。")
else:
print(f"\n完了しました。修正対象のファイルは見つかりませんでした。")
if __name__ == '__main__':
main()