-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocess_docs.py
More file actions
313 lines (284 loc) · 10.4 KB
/
Copy pathprocess_docs.py
File metadata and controls
313 lines (284 loc) · 10.4 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
import re
CJK_RE = re.compile(r'[\u4e00-\u9fff]')
ASCII_RE = re.compile(r'[a-zA-Z]')
CJK_PUNCT_RE = re.compile(r'[。,、;:!?()【】《》""''…]')
def cjk_count(text):
return len(CJK_RE.findall(text))
def ascii_letter_count(text):
return len(ASCII_RE.findall(text))
def english_word_count(text):
return len(re.findall(r'[a-zA-Z]+', text))
def has_cjk_punct(text):
return bool(CJK_PUNCT_RE.search(text))
def is_chinese_sentence(text):
cjk = cjk_count(text)
if cjk == 0:
return False
en_words = english_word_count(text)
if has_cjk_punct(text):
return True
if cjk > en_words:
return True
return False
def split_slash(line):
idx = line.find(' / ')
if idx == -1:
return None
before = line[:idx]
after = line[idx+3:]
before_cjk = cjk_count(before)
after_cjk = cjk_count(after)
if after_cjk > 0 and before_cjk == 0:
return (before, after)
if before_cjk > 0 and after_cjk == 0:
return (after, before)
if before_cjk > 0 and after_cjk > 0:
if english_word_count(before) > english_word_count(after):
return (before, after)
else:
return (after, before)
return None
def clean_en_line(line):
s = split_slash(line)
if s is not None:
return s[0]
m = re.match(r'^(.+?[.!?])\s+[\u4e00-\u9fff]', line)
if m:
return m.group(1)
m = re.search(r'[\u4e00-\u9fff][。!?]\s+([A-Za-z].*)$', line)
if m:
return m.group(1)
return line
def clean_zh_line(line):
s = split_slash(line)
if s is not None:
return s[1]
m = re.match(r'^([^\u4e00-\u9fff]*?[.!?])\s+([\u4e00-\u9fff])', line)
if m:
marker = re.match(r'^([\s>]*[-*]?\s*)', line)
prefix = marker.group(1) if marker else ''
return prefix + line[m.start(2):]
m = re.search(r'[。!?]\s*[A-Za-z]', line)
if m:
return line[:m.start()+1]
return line
def process_comment_en(indent, text):
"""Process a comment's text for en-US."""
if cjk_count(text) == 0:
return text
s = split_slash(text)
if s is not None:
return s[0]
if is_chinese_sentence(text):
return None
cleaned = clean_en_line(text)
if cjk_count(cleaned) > 0:
cleaned = re.sub(r'[\u4e00-\u9fff\u3000-\u303f\uff00-\uffef,。、;:""''()【】《》!?…]+', '', cleaned)
cleaned = re.sub(r' +', ' ', cleaned).strip()
return cleaned if cleaned and ascii_letter_count(cleaned) > 0 else None
def process_comment_zh(text):
if cjk_count(text) == 0:
return None
s = split_slash(text)
if s is not None:
return s[1]
return text
def process_line_en(line, in_code_block):
stripped = line.strip()
if not stripped or stripped == '---':
return line
if stripped.startswith('```'):
return line
if in_code_block:
# Find comment marker # (inline or full-line comment)
hash_idx = line.find('#')
if hash_idx != -1 and not re.match(r'^\s*#', line):
# Inline comment: code # comment
code_part = line[:hash_idx]
comment_text = line[hash_idx+1:].strip()
s = split_slash(comment_text)
if s is not None:
cleaned = s[0]
elif cjk_count(comment_text) == 0:
cleaned = comment_text
elif is_chinese_sentence(comment_text):
return code_part.rstrip()
else:
cleaned = clean_en_line(comment_text)
if cjk_count(cleaned) > 0:
cleaned = re.sub(r'[\u4e00-\u9fff\u3000-\u303f\uff00-\uffef,。、;:""''()【】《》!?…]+', '', cleaned)
cleaned = re.sub(r' +', ' ', cleaned).strip()
if cleaned and ascii_letter_count(cleaned) > 0:
return f'{code_part}# {cleaned}'
return code_part.rstrip()
# Full-line comment (# or //)
cm = re.match(r'^(\s*)(#|//)\s*(.*)$', line)
if cm:
indent, cmarker, text = cm.group(1), cm.group(2), cm.group(3)
if cjk_count(text) == 0:
return line
s = split_slash(text)
if s is not None:
return f'{indent}{cmarker} {s[0]}'
if is_chinese_sentence(text):
return None
cleaned = clean_en_line(text)
if cjk_count(cleaned) > 0:
cleaned = re.sub(r'[\u4e00-\u9fff\u3000-\u303f\uff00-\uffef,。、;:""''()【】《》!?…]+', '', cleaned)
cleaned = re.sub(r' +', ' ', cleaned).strip()
if cleaned and ascii_letter_count(cleaned) > 0:
return f'{indent}{cmarker} {cleaned}'
return None
# No comment - try slash split on whole line (e.g. help text)
s = split_slash(line)
if s is not None:
return s[0]
# Handle translation key-value: "English" = "中文" -> "English" = "English"
kv = re.match(r'^(\s*)"([^"]*)"\s*=\s*"([^"]*)"(\s*)$', line)
if kv and cjk_count(kv.group(3)) > 0 and cjk_count(kv.group(2)) == 0:
indent, key, val, tail = kv.group(1), kv.group(2), kv.group(3), kv.group(4)
return f'{indent}"{key}" = "{key}"{tail}'
return line
# Outside code block
# Heading: extract level and text, split only the text part
if stripped.startswith('#'):
hm = re.match(r'^(#{1,6})\s+(.*)$', line)
if hm:
level, text = hm.group(1), hm.group(2)
s = split_slash(text)
if s is not None:
return f'{level} {s[0]}'
if cjk_count(text) == 0:
return line
if is_chinese_sentence(text):
return None
cleaned = clean_en_line(text)
return f'{level} {cleaned}' if cjk_count(cleaned) == 0 else None
return line
# Reference link list item with Chinese text -> remove for en-US
link_match = re.match(r'^\s*[-*]\s*\[([^\]]*)\]', line)
if link_match and cjk_count(link_match.group(1)) > 0:
return None
if cjk_count(line) == 0:
return line
# Try to extract English first (handles ' / ' separators)
cleaned = clean_en_line(line)
if cjk_count(cleaned) == 0:
return cleaned
# Still has CJK after cleaning - if primarily Chinese, remove
if is_chinese_sentence(line):
return None
return cleaned
def process_line_zh(line, in_code_block):
stripped = line.strip()
if not stripped or stripped == '---':
return line
if stripped.startswith('```'):
return line
if in_code_block:
# Find comment marker # (inline or full-line comment)
hash_idx = line.find('#')
if hash_idx != -1 and not re.match(r'^\s*#', line):
code_part = line[:hash_idx]
comment_text = line[hash_idx+1:].strip()
s = split_slash(comment_text)
if s is not None:
cleaned = s[1]
elif cjk_count(comment_text) == 0:
return code_part.rstrip() # pure English comment - remove
else:
cleaned = clean_zh_line(comment_text)
if cleaned and cjk_count(cleaned) > 0:
return f'{code_part}# {cleaned}'
return code_part.rstrip()
# Full-line comment (# or //)
cm = re.match(r'^(\s*)(#|//)\s*(.*)$', line)
if cm:
indent, cmarker, text = cm.group(1), cm.group(2), cm.group(3)
if cjk_count(text) == 0:
return None
s = split_slash(text)
if s is not None:
return f'{indent}{cmarker} {s[1]}'
cleaned = clean_zh_line(text)
if cjk_count(cleaned) > 0:
return f'{indent}{cmarker} {cleaned}'
return line
# No comment - try slash split on whole line
s = split_slash(line)
if s is not None:
return s[1]
return line
# Outside code block
if stripped.startswith('#'):
hm = re.match(r'^(#{1,6})\s+(.*)$', line)
if hm:
level, text = hm.group(1), hm.group(2)
s = split_slash(text)
if s is not None:
return f'{level} {s[1]}'
# No slash separator - keep heading as-is (may be filename/code)
return line
return line
if cjk_count(line) == 0:
if re.match(r'^\s*[-*]\s*\[', line) or stripped.startswith('http'):
return line
return None
# Has Chinese - clean any leading/trailing English
if ascii_letter_count(line) > 0:
cleaned = clean_zh_line(line)
if cjk_count(cleaned) > 0:
return cleaned
return line
def process_file(filepath, lang):
with open(filepath, 'r', encoding='utf-8') as f:
lines = f.readlines()
processed = []
in_code_block = False
for line in lines:
line = line.rstrip('\n')
stripped = line.strip()
if stripped.startswith('```'):
in_code_block = not in_code_block
processed.append(line)
continue
if lang == 'en':
result = process_line_en(line, in_code_block)
else:
result = process_line_zh(line, in_code_block)
if result is not None:
processed.append(result)
final = []
prev_blank = False
for l in processed:
s = l.strip()
if s == '>':
continue
if not s:
if prev_blank:
continue
prev_blank = True
else:
prev_blank = False
final.append(l)
while final and not final[-1].strip():
final.pop()
with open(filepath, 'w', encoding='utf-8') as f:
f.write('\n'.join(final) + '\n')
return len(final)
import sys
import shutil
base = r'e:\Github_desktop\bomiot_example\greaterwms\media'
backup = r'e:\Github_desktop\bomiot_example\_backup_media'
names = [
'django', 'fastapi_app', 'flask_app', 'bomiotconf', 'terminal',
'interaction', 'server', 'scheduler', 'observer', 'data',
'structure', 'permission', 'mysql', 'postgresql', 'inscription'
]
for name in names:
shutil.copy2(f'{backup}\\{name}.en-US.md', f'{base}\\{name}.en-US.md')
shutil.copy2(f'{backup}\\{name}.zh-CN.md', f'{base}\\{name}.zh-CN.md')
en = process_file(f'{base}\\{name}.en-US.md', 'en')
zh = process_file(f'{base}\\{name}.zh-CN.md', 'zh')
print(f'{name}: EN={en} ZH={zh}')
print("All done")