-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathblock.py
More file actions
116 lines (99 loc) · 3.61 KB
/
Copy pathblock.py
File metadata and controls
116 lines (99 loc) · 3.61 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
from typing import List
class Block:
def __init__(self, content, index=None):
# 父节点
self.parent: Block = None
# 子节点
self.children: List[Block] = []
# 每一个block的每一行
self.content: List[str] = content
# 节点索引
self.index: int = index
# 其他数据
self.other_data: list = []
# 代码块
self.has_code_block: bool = False
self.code_block: list = [0, 0]
self.is_order_list: bool = False
self.is_header_list: bool = False
@staticmethod
def copy_attributes(source: 'Block', target: 'Block', exclude=None):
exclude = exclude or []
for attr, value in vars(source).items():
if attr not in exclude:
setattr(target, attr, value)
@staticmethod
def copy_attributes_skip_flag(source: 'Block', target: 'Block', exclude=None):
exclude = exclude or []
for attr, value in vars(source).items():
if attr not in exclude:
# 跳过布尔值为false的属性
if type(attr) is bool and not value:
continue
else:
setattr(target, attr, value)
def add_child(self, child):
if type(child) is Block:
self.children.append(child)
child.parent = self
else:
raise Exception("child must be a Block")
def add_line_before_content(self, index: int, content_line: str) -> int:
total_insert = 0
if type(content_line) is list:
for line in content_line:
total_insert += 1
self.content.insert(index, line)
index += 1
else:
self.content.insert(index, content_line)
total_insert += 1
return total_insert
def add_line_after_content(self, index: int, content_line: str):
# 在当前行后面插入
self.add_line_before_content(index + 1, content_line)
def get_current_content_line(self, index):
current_line = None
if len(self.content) > index:
current_line = self.content[index]
return current_line
def add_empty_line_around_content(self, index_start, index_end):
total_insert = 0
# 避免插入重复的空行
current_line = self.get_current_content_line(index_start)
if current_line is None or current_line != "":
self.add_line_before_content(index_start, "")
total_insert += 1
# 上一步插入了一行,这里要+1
current_line = self.get_current_content_line(index_end + 2)
if current_line is None or current_line != "":
self.add_line_after_content(index_end + 1, "")
total_insert += 1
else:
self.add_line_before_content(index_end, "")
total_insert += 1
return total_insert
def add_empty_line_around_one_content(self, index):
return self.add_empty_line_around_content(index, index)
def add_content(self, content):
if type(content) is str:
self.content.append(content)
else:
raise Exception("content must be a str")
def init_root_block():
b = Block(None, -2)
b.parent = None
return b
def get_all_block(block, li):
ch = block.children
if ch is not None and len(ch) != 0:
li.extend(ch)
for c in ch:
get_all_block(c, li)
def find_header(block):
if block.is_header_list:
return block
else:
ch = block.children
for c in ch:
return find_header(c)