This repository has been archived by the owner on Sep 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
313 lines (251 loc) · 6.88 KB
/
main.py
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 os
import sys
import ply.lex as lex
states = [
('paragraph', 'exclusive'),
('bold', 'exclusive'),
('italic', 'exclusive'),
('underlined', 'exclusive'),
('strikethrough', 'exclusive'),
('struct', 'exclusive'),
('header', 'exclusive')
]
tokens = ['BOLD', 'ITL', 'UNDLINE', 'STRIKE',
'STRUCTO', 'STRUCTC',
'IMAGE', 'HDR', 'TEXT', 'LINEDOWN']
# Global Variables
headerSize = 0
# formatação: negrito, itálico, sublinhado;
# • vários níveis de títulos;
# • listas de tópicos (items) não-numerados, numerados ou tipo entradas de um dicionário ;
# • inclusão de imagens;
# • inclusão e formatação de tabelas;
# • todos os outros que achar necessário ou a sua imaginação vislumbrar.
# Header
def t_HDR(t):
r'\@+'
t.lexer.push_state('header')
global headerSize
headerSize = 0
for character in t.value:
if character == '@':
headerSize += 1
t.lexer.output += "<h" + str(headerSize) + ">"
def t_header_TEXT(t):
r'.+'
t.lexer.pop_state()
t.lexer.output += t.value.strip()
t.lexer.output += "</h" + str(headerSize) + ">\n"
def t_paragraph_HDR(t):
r'\@+'
t.lexer.pop_state()
t.lexer.output += '</p>\n'
t.lexer.push_state('header')
global headerSize
headerSize = 0
for character in t.value:
if character == '@':
headerSize += 1
t.lexer.output += "<h" + str(headerSize) + ">"
# BOLD
def t_BOLD(t):
r'\$\$'
t.lexer.output += "\n<p>\n"
a = t.lexer.lexstatestack
t.lexer.push_state('paragraph')
if 'bold' in a:
return t
else:
t.lexer.push_state('bold')
t.lexer.output += "<strong>"
def t_paragraph_italic_underlined_strikethrough_struct_BOLD(t):
r'\$\$'
a = t.lexer.lexstatestack
if 'bold' in a:
return t
else:
t.lexer.push_state('bold')
t.lexer.output += "<strong>"
def t_bold_BOLD(t):
r'\$\$'
t.lexer.pop_state()
t.lexer.output += "</strong>"
# Italic
def t_ITL(t):
r'//'
a = t.lexer.lexstatestack
t.lexer.push_state('paragraph')
t.lexer.output += "<p>\n"
if 'italic' in a:
return t
else:
t.lexer.push_state('italic')
t.lexer.output += "<em>"
def t_paragraph_bold_underlined_strikethrough_struct_ITL(t):
r'//'
a = t.lexer.lexstatestack
if 'italic' in a:
return t
else:
t.lexer.push_state('italic')
t.lexer.output += "<em>"
def t_italic_ITL(t):
r'//'
t.lexer.pop_state()
t.lexer.output += "</em>"
# UNDERLINED
def t_UNDLINE(t):
r'__'
a = t.lexer.lexstatestack
t.lexer.push_state('paragraph')
t.lexer.output += "\n<p>\n"
if 'underlined' in a:
return t
else:
t.lexer.push_state('underlined')
t.lexer.output += "<u>"
def t_paragraph_bold_italic_strikethrough_struct_UNDLINE(t):
r'__'
a = t.lexer.lexstatestack
if 'underlined' in a:
return t
else:
t.lexer.push_state('underlined')
t.lexer.output += "<u>"
def t_underlined_UNDLINE(t):
r'__'
t.lexer.pop_state()
t.lexer.output += "</u>"
# STRIKETHROUGH
def t_STRIKE(t):
r'--'
a = t.lexer.lexstatestack
t.lexer.push_state('paragraph')
t.lexer.output += '\n<p>\n'
if 'strikethrough' in a:
return t
else:
t.lexer.push_state('strikethrough')
t.lexer.output += "<del>"
def t_paragraph_bold_italic_underlined_STRIKE(t):
r'--'
a = t.lexer.lexstatestack
if 'strikethrough' in a:
return t
else:
t.lexer.push_state('strikethrough')
t.lexer.output += "<del>"
def t_strikethrough_STRIKE(t):
r'--'
t.lexer.pop_state()
t.lexer.output += "</del>"
# STRUCTS
def t_struct_STRUCTC(t):
r'\]\ *'
t.lexer.pop_state()
match t.lexer.structtype:
case "num":
t.lexer.output += '</ol>\n'
case "dot":
t.lexer.output += '</ul>\n'
case "dictionary":
t.lexer.output += '</dl>\n'
case "table":
t.lexer.structtype = "table"
t.lexer.output += '</table>\n'
t.lexer.rownum = True
case _:
pass
return t
def t_INITIAL_paragraph_STRUCTO(t):
r'\[\ *\{\ *\w+\}'
if 'paragraph' in t.lexer.lexstatestack:
t.lexer.pop_state()
t.lexer.output += '\n</p>\n'
t.lexer.push_state('struct')
for character in r'[{} ':
t.value = t.value.replace(character, "")
match t.value:
case "num":
t.lexer.structtype = 'num'
t.lexer.output += '\n<ol>'
case "dot":
t.lexer.structtype = 'dot'
t.lexer.output += '\n<ul>'
case "dictionary":
t.lexer.structtype = "dictionary"
t.lexer.output += '\n<dl>'
case "table":
t.lexer.structtype = "table"
t.lexer.output += '\n<table border=1px>'
case _:
pass
return t
def t_struct_TEXT(t):
r'\ *.+\ *'
match t.lexer.structtype:
case "dictionary":
list = t.value.split(":")
t.lexer.output += '\t<dt>' + list[0].strip() + '</dt>'
t.lexer.output += '<dd>' + list[1].strip() + '</dd>'
case "table":
t.lexer.output += "\t<tr>\n"
row_data = t.value.strip().split('|')
for cell in row_data:
if t.lexer.fstrow:
t.lexer.output += "\t\t<th>" + cell.strip() + "</th>\n"
else:
t.lexer.output += "\t\t<td>" + cell.strip() + "</td>\n"
t.lexer.output += "\t</tr>"
t.lexer.fstrow = False
case _:
t.lexer.output += '\t<li>' + t.value.strip() + '</li>'
# IMAGE
def t_INITIAL_paragraph_IMAGE(t):
r'img\{\ *[^\{\}]+\}\ *'
lista = t.value.strip().split('{')[1].split('}')
t.lexer.output += r'<img src="' + lista[0] + '\">'
# TEXT
def t_paragraph_LINEDOWN(t):
r'\n\n'
t.lexer.output += '\n</p>'
t.lexer.pop_state()
def t_ANY_LINEDOWN(t):
r'\n\n'
return t
def t_TEXT(t):
r'(.|\n)'
t.lexer.output += '\n<p>\n'
t.lexer.output += t.value
t.lexer.push_state('paragraph')
return t
def t_paragraph_bold_italic_underlined_strikethrough_struct_TEXT(t):
r'(.|\n)'
t.lexer.output += t.value
return t
t_ignore = '\n'
t_header_paragraph_bold_italic_underlined_strikethrough_struct_ignore = ''
# ERROR
def t_ANY_error(t):
print('Invalid Character! ' + t.value)
if len(sys.argv) < 3:
sys.exit(1)
# Open input file
f = open(sys.argv[1], "r")
# Read input file
text = f.read()
# Close input file
f.close()
lexer = lex.lex()
lexer.input(text)
# Define our variable
lexer.output = "<html>\n<body>\n"
lexer.structtype = ""
lexer.fstrow = True
for tok in lexer:
pass
# Open output file
outputFile = open(sys.argv[2], "w")
lexer.output += "</body>\n</html>"
outputFile.write(lexer.output)
outputFile.close()