-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathformat_note.py
More file actions
249 lines (208 loc) · 10.8 KB
/
format_note.py
File metadata and controls
249 lines (208 loc) · 10.8 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
import re
def is_table_line(line):
"""
判断是否为表格行
表格特征:包含 | 符号,或者包含大量的 - 符号且同时包含 | 符号(分隔线)
"""
stripped_line = line.strip()
# 空行不是表格行
if not stripped_line:
return False
# 包含 | 符号的行是表格行
if '|' in stripped_line:
return True
# 修改:只有同时包含 | 和大量 - 符号的行才认为是表格分隔线
# 这样可以避免将普通的分隔线误判为表格
dash_count = stripped_line.count('-')
pipe_count = stripped_line.count('|')
# 如果同时包含 | 符号和大量 - 符号,且主要是 - 和 | 字符,认为是表格分隔线
if '|' in stripped_line and dash_count >= 3:
total_chars = len(stripped_line.replace(' ', '')) # 去掉空格后的字符数
if (dash_count + pipe_count) >= total_chars * 0.7:
return True
return False
def format_note(text):
"""
格式化输出文本,调节大小标题逻辑,并插入空行调节排版
"""
# 将文本按行分割
lines = text.split('\n')
formatted_lines = []
# 第一步:收集所有标题信息,用于判断是否需要添加【笔记主题】
headers = []
for i, line in enumerate(lines):
stripped_line = line.strip()
if stripped_line.startswith('#'):
header_match = re.match(r'^(#+)', stripped_line)
if header_match:
header_level = len(header_match.group(1))
headers.append((i, header_level, stripped_line))
# 判断是否需要添加【笔记主题】
first_header_level = None
level_1_count = 0
first_level_1_index = -1
for i, level, content in headers:
if first_header_level is None:
first_header_level = level
if level == 1:
level_1_count += 1
if first_level_1_index == -1:
first_level_1_index = i
need_add_theme = (first_header_level == 1 and level_1_count == 1)
# 第二步:重新处理文本
i = 0
while i < len(lines):
line = lines[i]
stripped_line = line.strip()
# 处理标题行
if stripped_line.startswith('#'):
# 在标题前添加空行(如果不是第一行且前一行不是空行)
if formatted_lines and formatted_lines[-1].strip() != '':
formatted_lines.append('')
# 减少标题级别(#数量减一,但至少保留一个#)
header_match = re.match(r'^(#+)', stripped_line)
if header_match:
header_level = len(header_match.group(1))
new_level = max(1, header_level - 1) # 至少保留1级标题
new_header = '#' * new_level
new_line = stripped_line.replace(header_match.group(1), new_header, 1)
# 如果是第一个一级标题且需要添加主题标记
if need_add_theme and i == first_level_1_index and new_level == 1:
new_line = "# 【笔记主题】" + new_line[1:].strip()
formatted_lines.append(new_line)
else:
formatted_lines.append(stripped_line)
# 在标题后添加空行
formatted_lines.append('')
# 处理有序列表(以数字+.+空格开头)
elif re.match(r'^\s*\d+\.\s+', stripped_line):
# 判断前一行是否也是列表项
prev_is_ordered_list = False
prev_is_unordered_list = False
if formatted_lines:
prev_line = formatted_lines[-1].strip()
if re.match(r'^\s*\d+\.\s+', prev_line):
prev_is_ordered_list = True
elif re.match(r'^\s*[\*\-]\s+', prev_line):
prev_is_unordered_list = True
# 默认情况下,连续的有序列表项不需要空行
need_space = False
if prev_is_ordered_list:
# 提取前一行和当前行的数字
prev_match = re.match(r'^\s*(\d+)\.', formatted_lines[-1].strip())
curr_match = re.match(r'^\s*(\d+)\.', stripped_line)
if prev_match and curr_match:
prev_num = int(prev_match.group(1))
curr_num = int(curr_match.group(1))
# 如果不是连续的数字,或者前一行包含冒号(主题标题),则需要空行
if curr_num != prev_num + 1 or ':' in formatted_lines[-1]:
need_space = True
elif prev_is_unordered_list:
# 前一行是无序列表,需要根据缩进级别判断是否需要空行
prev_line = formatted_lines[-1]
prev_indent = len(prev_line) - len(prev_line.lstrip())
curr_indent = len(line) - len(line.lstrip())
# 如果前一行缩进较深(嵌套列表),转换为有序列表时不需要空行
# 如果前一行缩进较浅,且当前有序列表项包含冒号(主题标题),则需要空行
if prev_indent >= 6: # 较深缩进
need_space = False
elif ':' in stripped_line: # 主题标题
need_space = True
else:
need_space = False
else:
# 如果前一行不是列表,则需要空行
need_space = True
# 根据需要添加空行
if need_space and formatted_lines:
prev_line = formatted_lines[-1].strip()
if prev_line.startswith('#'):
formatted_lines.append('')
elif prev_line != '' and not prev_line.startswith('>') and not re.match(r'^\s*\d+\.\s+', prev_line):
# 如果前一行是普通正文(不是列表项),需要空行
formatted_lines.append('')
formatted_lines.append(line)
# 处理无序列表(以*或-+空格开头)
elif re.match(r'^\s*[\*\-]\s+', stripped_line):
# 判断前一行是否也是列表项(包括有序和无序)
prev_is_ordered_list = False
prev_is_unordered_list = False
if formatted_lines:
prev_line = formatted_lines[-1].strip()
if re.match(r'^\s*\d+\.\s+', prev_line):
prev_is_ordered_list = True
elif re.match(r'^\s*[\*\-]\s+', prev_line):
prev_is_unordered_list = True
# 从有序列表转换到无序列表时的处理
# 无序列表之间紧密排版,不插入空行
# 如果前一行不是列表项,需要空行分隔
need_space = False
if prev_is_ordered_list:
# 检查缩进级别:如果当前无序列表缩进较深,说明是有序列表的子项,不需要空行
# 如果当前无序列表没有缩进或缩进较浅,说明是新的主题,需要空行
prev_line = formatted_lines[-1]
prev_indent = len(prev_line) - len(prev_line.lstrip())
curr_indent = len(line) - len(line.lstrip())
if curr_indent > prev_indent:
need_space = False # 子项,不需要空行
else:
need_space = True # 新主题,需要空行
elif not prev_is_unordered_list and formatted_lines:
prev_line = formatted_lines[-1].strip()
if prev_line.startswith('#'):
need_space = True
elif prev_line != '' and not prev_line.startswith('>'):
# 如果前一行是普通正文,也需要空行
need_space = True
# 根据需要添加空行
if need_space and formatted_lines:
formatted_lines.append('')
formatted_lines.append(line)
# 处理引用行(以>开头)
elif stripped_line.startswith('>'):
# 引用前添加空行(如果不是第一行且前一行不是空行且不是引用)
if formatted_lines and formatted_lines[-1].strip() != '' and not formatted_lines[-1].strip().startswith('>'):
formatted_lines.append('')
formatted_lines.append(line)
# 处理分隔线(---或***)
elif stripped_line in ['---', '***', '----']:
# 分隔线前添加空行(如果不是第一行且前一行不是空行)
if formatted_lines and formatted_lines[-1].strip() != '':
formatted_lines.append('')
formatted_lines.append(line)
# 分隔线后添加空行
if i < len(lines) - 1:
formatted_lines.append('')
# 处理表格行
elif is_table_line(line):
# 表格行前不添加空行,保持表格的紧凑性
# 但如果前一行不是表格行且不是空行,需要添加空行分隔
if formatted_lines:
prev_line = formatted_lines[-1].strip()
if prev_line != '' and not is_table_line(formatted_lines[-1]) and not prev_line.startswith('#'):
formatted_lines.append('')
formatted_lines.append(line)
# 处理普通正文
elif stripped_line != '':
# 如果前一行是标题,已经添加过空行了
# 如果前一行是正文且不是空行,添加空行
# 但如果前一行是表格行,则不添加空行,保持表格的完整性
if (formatted_lines and
formatted_lines[-1].strip() != '' and
not formatted_lines[-1].strip().startswith('#') and
not formatted_lines[-1].strip().startswith('>') and
not re.match(r'^\s*[\d\*\-\+]\s+', formatted_lines[-1].strip()) and
not is_table_line(formatted_lines[-1])): # 新增:前一行不是表格行
formatted_lines.append('')
formatted_lines.append(line)
# 处理空行
else:
# 保留原有的空行,但避免连续空行过多
if not formatted_lines or formatted_lines[-1].strip() != '':
formatted_lines.append(line)
i += 1
# 重新组合文本
text = '\n'.join(formatted_lines)
# 清理多余的连续空行(超过2个空行的情况)
text = re.sub(r'\n{3,}', '\n\n', text)
return text