-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
195 lines (149 loc) · 5.97 KB
/
Copy pathapp.py
File metadata and controls
195 lines (149 loc) · 5.97 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
from enum import Enum, auto
import re
from typing import List, Set
class FontStyle(Enum):
BOLD = auto()
ITALIC = auto()
UNDERLINE = auto()
class TextConverter:
def convert_character(self, c: str):
pass
def convert_font_change(self, font: FontStyle):
pass
def convert_paragraph(self):
pass
class ASCIIConverter(TextConverter):
def __init__(self):
self._result = []
def convert_character(self, c: str):
self._result.append(c)
def convert_font_change(self, font: FontStyle):
# ASCII doesn't support font changes
pass
def convert_paragraph(self):
self._result.append('\n')
def GetASCIIText(self) -> str:
return ''.join(self._result)
class TeXConverter(TextConverter):
def __init__(self):
self._result = []
self._active_styles: Set[FontStyle] = set()
def _escape_tex_special_char(self, c: str) -> str:
special_chars = {
'&': r'\&',
'%': r'\%',
'$': r'\$',
'#': r'\#',
'_': r'\_',
'{': r'\{',
'}': r'\}'
}
return special_chars.get(c, c)
def _wrap_in_tex_commands(self, text: str) -> str:
styles = self._active_styles
if {FontStyle.BOLD, FontStyle.ITALIC} <= styles:
return r'\textbf{\textit{' + text + '}}'
if FontStyle.BOLD in styles:
return r'\textbf{' + text + '}'
if FontStyle.ITALIC in styles:
return r'\textit{' + text + '}'
if FontStyle.UNDERLINE in styles:
return r'\underline{' + text + '}'
return text
def convert_character(self, c: str):
converted_char = self._escape_tex_special_char(c)
if self._active_styles:
converted_char = self._wrap_in_tex_commands(converted_char)
self._result.append(converted_char)
def convert_font_change(self, font: FontStyle):
if font in self._active_styles:
self._active_styles.remove(font)
else:
self._active_styles.add(font)
def convert_paragraph(self):
self._result.append('\n\n')
def GetTeXText(self) -> str:
return ''.join(self._result)
class TextWidgetConverter(TextConverter):
def __init__(self):
self._elements: List[str] = []
self._active_styles: Set[FontStyle] = set()
def convert_character(self, c: str):
# Create a TextWidget for each character with current font styles
styles_str = 'normal' if not self._active_styles else '+'.join(
style.name.lower() for style in self._active_styles
)
self._elements.append(f"TextWidget{{text='{c}', font='{styles_str}'}}")
def convert_font_change(self, font: FontStyle):
# Toggle the font style for subsequent characters
if font in self._active_styles:
self._active_styles.remove(font)
else:
self._active_styles.add(font)
def convert_paragraph(self):
# Add paragraph separator
self._elements.append("Paragraph")
def GetTextWidget(self) -> str:
return '\n'.join(self._elements)
class RTFReader:
def __init__(self, builder: TextConverter):
self._builder = builder
# Regex pattern to match different token types
self._token_pattern = re.compile(r'\{(char|font|par)(?::([^}]+))?\}')
def parse_rtf(self, rtf_input: str):
# Find all tokens in the input
tokens = self._token_pattern.finditer(rtf_input)
last_index = 0
for match in tokens:
token_type = match.group(1)
token_value = match.group(2)
if token_type == 'char' and token_value and len(token_value) == 1:
self._builder.convert_character(token_value)
elif token_type == 'font':
if token_value == 'bold':
self._builder.convert_font_change(FontStyle.BOLD)
elif token_value == 'italic':
self._builder.convert_font_change(FontStyle.ITALIC)
elif token_value == 'underline':
self._builder.convert_font_change(FontStyle.UNDERLINE)
elif token_type == 'par':
self._builder.convert_paragraph()
last_index = match.end()
# Handle any parsing errors or unexpected input
if last_index < len(rtf_input):
print(f"Warning: Unparsed input remains: {rtf_input[last_index:]}")
def main():
# Enhanced sample input with more complex font toggling
sample_input = (
"{char:H}{char:e}{char:l}{char:l}{char:o} "
"{font:bold}{char:W}{char:o}{char:r}{char:l}{char:d}{font:bold} "
"{font:italic}{char:!}{font:italic}{par}"
"{char:A}{font:bold}{font:italic}{char:B}{char:C}{font:bold}{font:italic}"
)
# Print the sample input for visualization
print(f"\033[1;34mSample Input:\033[0m")
print(sample_input)
print(f"\n\033[1;34mProcessing...\033[0m\n")
# Demonstrate ASCII Conversion
print(f"\033[1;32mASCII Conversion:\033[0m")
ascii_converter = ASCIIConverter()
ascii_reader = RTFReader(ascii_converter)
ascii_reader.parse_rtf(sample_input)
print(ascii_converter.GetASCIIText())
print(f"\n\033[1;34m-----------------------------\033[0m\n")
# Demonstrate TeX Conversion
print(f"\033[1;32mTeX Conversion:\033[0m")
tex_converter = TeXConverter()
tex_reader = RTFReader(tex_converter)
tex_reader.parse_rtf(sample_input)
print(tex_converter.GetTeXText())
print(f"\n\033[1;34m-----------------------------\033[0m\n")
# Demonstrate Text Widget Conversion
print(f"\033[1;32mText Widget Conversion:\033[0m")
widget_converter = TextWidgetConverter()
widget_reader = RTFReader(widget_converter)
widget_reader.parse_rtf(sample_input)
print(widget_converter.GetTextWidget())
print(f"\n\033[1;34m-----------------------------\033[0m\n")
if __name__ == "__main__":
main()