-
Notifications
You must be signed in to change notification settings - Fork 172
Expand file tree
/
Copy pathupdate_lang_caution.py
More file actions
98 lines (79 loc) · 3.36 KB
/
Copy pathupdate_lang_caution.py
File metadata and controls
98 lines (79 loc) · 3.36 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
# 言語機能ページに注意事項を記載・更新する
# リポジトリのトップディレクトリで以下を実行する
# python3 .github/workflows/script/update_lang_caution.py
import glob
import os
import sys
import re
LANG_CAUTION = '''このページはC++{}に採用された言語機能の変更を解説しています。
のちのC++規格でさらに変更される場合があるため[関連項目](#relative-page)を参照してください。'''
FUTURE_LANG_CAUTION = '''このページはC++{}に採用される見込みの言語機能の変更を解説しています。
のちのC++規格でさらに変更される場合があるため[関連項目](#relative-page)を参照してください。'''
FUTURE_LANG_VERSION = 26
START_CAUTION = "<!-- start lang caution -->"
LAST_CAUTION = "<!-- last lang caution -->"
def update_lang_caution(text: str, filename: str) -> str:
version = 0
new_lines = []
is_version_line = False
start_lang_caution = False
end_lang_caution = False
found_relative_page = False
for line in text.splitlines():
m = re.search(r'\* cpp(.*?)\[meta cpp\]', line)
if m:
version = int(m[1])
new_lines.append(line)
is_version_line = True
continue
if start_lang_caution:
if line == LAST_CAUTION:
start_lang_caution = False
end_lang_caution = True
continue
if len(line) <= 0:
new_lines.append(line)
continue
if line == "## 関連項目":
new_lines.append("## <a id=\"relative-page\" href=\"#relative-page\">関連項目</a>")
found_relative_page = True
continue
elif line == "## <a id=\"relative-page\" href=\"#relative-page\">関連項目</a>":
found_relative_page = True
if not end_lang_caution:
is_lang_caution = False
if line == START_CAUTION:
start_lang_caution = True
is_lang_caution = True
elif is_version_line:
is_version_line = False
if not start_lang_caution:
is_lang_caution = True
if is_lang_caution:
caution_text = LANG_CAUTION if version < FUTURE_LANG_VERSION else FUTURE_LANG_CAUTION
new_lines.append(START_CAUTION)
new_lines.append("")
new_lines.append(caution_text.format(version))
new_lines.append("")
new_lines.append(LAST_CAUTION)
if not start_lang_caution:
new_lines.append("")
if not start_lang_caution:
new_lines.append(line)
continue
new_lines.append(line)
new_text = "\n".join(new_lines)
if not found_relative_page:
new_text = new_text.replace("[関連項目](#relative_page)", "関連項目")
return new_text
if __name__ == '__main__':
for p in sorted(list(glob.glob("lang/**/*.md", recursive=True))):
if p.count("/") <= 1:
continue
with open(p) as f:
text = f.read()
new_text = update_lang_caution(text, p)
if text != new_text and text != (new_text + "\n") and (text + "\n") != new_text:
print("hit {}".format(p))
with open(p, 'w') as f:
f.write(new_text)