···
def __init__(self) -> None:
self.tokens: list[cross.Token] = []
self.mentions: list[tuple[str, str]]
···
if isinstance(last_token, cross.TextToken) and not last_token.text.endswith('\n'):
self.tokens.append(cross.TextToken('\n'))
32
-
self.tokens.append(cross.TextToken(' \n'))
35
-
href = attrs_dict.get('href', '')
36
-
self.anchor_stack.append(href)
38
-
elif tag == 'strong' or tag == 'b':
39
-
self.tokens.append(cross.TextToken('**'))
41
-
elif tag == 'em' or tag == 'i':
42
-
self.tokens.append(cross.TextToken('*'))
44
-
elif tag == 'del' or tag == 's':
45
-
self.tokens.append(cross.TextToken('~~'))
49
-
self.tokens.append(cross.TextToken('`'))
54
-
self.tokens.append(cross.TextToken('```\n'))
57
-
elif tag == 'blockquote':
59
-
self.tokens.append(cross.TextToken('\n> '))
62
-
self.list_stack.append('ul')
66
-
self.list_stack.append('ol')
70
-
indent = ' ' * (len(self.list_stack) - 1)
71
-
if self.list_stack and self.list_stack[-1] == 'ul':
72
-
self.tokens.append(cross.TextToken(f'{indent}- '))
73
-
elif self.list_stack and self.list_stack[-1] == 'ol':
74
-
self.tokens.append(cross.TextToken(f'{indent}1. '))
76
-
elif tag == {'h1', 'h2', 'h3', 'h4', 'h5', 'h6'}:
78
-
self.tokens.append(cross.TextToken("\n" + "#" * level + " "))
32
+
self.tokens.append(cross.TextToken(' \n'))
34
+
href = attrs_dict.get('href', '')
35
+
self.anchor_stack.append(href)
37
+
self.tokens.append(cross.TextToken('**'))
39
+
self.tokens.append(cross.TextToken('*'))
41
+
self.tokens.append(cross.TextToken('~~'))
44
+
self.tokens.append(cross.TextToken('`'))
48
+
self.tokens.append(cross.TextToken('```\n'))
52
+
self.tokens.append(cross.TextToken('> '))
54
+
self.list_stack.append(tag)
57
+
indent = ' ' * (len(self.list_stack) - 1)
58
+
if self.list_stack and self.list_stack[-1] == 'ul':
59
+
self.tokens.append(cross.TextToken(f'{indent}- '))
60
+
elif self.list_stack and self.list_stack[-1] == 'ol':
61
+
self.tokens.append(cross.TextToken(f'{indent}1. '))
63
+
if tag in {'h1', 'h2', 'h3', 'h4', 'h5', 'h6'}:
65
+
self.tokens.append(cross.TextToken("\n" + "#" * level + " "))
self.current_tag_stack.append(tag)
···
if tag in self.current_tag_stack:
self.current_tag_stack.remove(tag)
96
-
self.tokens.append(cross.TextToken('\n\n'))
84
+
self.tokens.append(cross.TextToken('\n\n'))
86
+
href = self.anchor_stack.pop()
87
+
anchor_data = ''.join(self.anchor_data)
88
+
self.anchor_data = []
99
-
href = self.anchor_stack.pop()
100
-
anchor_data = ''.join(self.anchor_data)
101
-
self.anchor_data = []
103
-
if anchor_data.startswith('#'):
104
-
as_tag = anchor_data[1:].lower()
105
-
if any(as_tag == block for block in self.tags):
106
-
self.tokens.append(cross.TagToken(anchor_data[1:]))
107
-
elif anchor_data.startswith('@'):
109
-
(pair for pair in self.mentions if anchor_data in pair),
90
+
if anchor_data.startswith('#'):
91
+
as_tag = anchor_data[1:].lower()
92
+
if any(as_tag == block for block in self.tags):
93
+
self.tokens.append(cross.TagToken(anchor_data[1:]))
94
+
elif anchor_data.startswith('@'):
96
+
(pair for pair in self.mentions if anchor_data in pair),
114
-
self.tokens.append(cross.MentionToken(match[1], ''))
116
-
self.tokens.append(cross.LinkToken(href, anchor_data))
118
-
elif tag == 'strong' or tag == 'b':
119
-
self.tokens.append(cross.TextToken('**'))
121
-
elif tag == 'em' or tag == 'i':
122
-
self.tokens.append(cross.TextToken('*'))
124
-
elif tag == 'del' or tag == 's':
125
-
self.tokens.append(cross.TextToken('~~'))
127
-
elif tag == 'code':
128
-
if not self.in_pre and self.in_code:
129
-
self.tokens.append(cross.TextToken('`'))
130
-
self.in_code = False
133
-
self.tokens.append(cross.TextToken('\n```\n'))
134
-
self.in_pre = False
136
-
elif tag == 'blockquote':
137
-
self.tokens.append(cross.TextToken('\n'))
139
-
elif tag == 'ul' or tag == 'ol':
140
-
if self.list_stack:
141
-
self.list_stack.pop()
142
-
self.tokens.append(cross.TextToken('\n'))
145
-
self.tokens.append(cross.TextToken('\n'))
147
-
elif tag in ['h1', 'h2', 'h3', 'h4', 'h5', 'h6']:
148
-
self.tokens.append(cross.TextToken('\n'))
101
+
self.tokens.append(cross.MentionToken(match[1], ''))
103
+
self.tokens.append(cross.LinkToken(href, anchor_data))
104
+
case 'strong', 'b':
105
+
self.tokens.append(cross.TextToken('**'))
107
+
self.tokens.append(cross.TextToken('*'))
109
+
self.tokens.append(cross.TextToken('~~'))
111
+
if not self.in_pre and self.in_code:
112
+
self.tokens.append(cross.TextToken('`'))
113
+
self.in_code = False
115
+
self.tokens.append(cross.TextToken('\n```\n'))
116
+
self.in_pre = False
118
+
self.tokens.append(cross.TextToken('\n'))
120
+
if self.list_stack:
121
+
self.list_stack.pop()
122
+
self.tokens.append(cross.TextToken('\n'))
124
+
self.tokens.append(cross.TextToken('\n'))
126
+
if tag in ['h1', 'h2', 'h3', 'h4', 'h5', 'h6']:
127
+
self.tokens.append(cross.TextToken('\n'))
def get_tokens(self) -> list[cross.Token]: