2021-06-29 13:07:22 -05:00
|
|
|
from fileinput import input as finput
|
2021-06-29 16:48:03 -05:00
|
|
|
import re
|
2021-06-29 13:07:22 -05:00
|
|
|
|
2021-06-29 16:48:03 -05:00
|
|
|
class UntitledError(BaseException): pass
|
2021-06-29 13:07:22 -05:00
|
|
|
class InternalError(UntitledError): pass
|
|
|
|
class ParsingError(UntitledError): pass
|
|
|
|
class MiscError(UntitledError): pass
|
|
|
|
|
|
|
|
class Element:
|
|
|
|
def __init__(self, element, data=None):
|
|
|
|
if not data: self.data = ""
|
|
|
|
else: self.data = data
|
|
|
|
self.element = element
|
|
|
|
def append(self, string, space=False):
|
2021-06-29 16:48:03 -05:00
|
|
|
if type(data) == str:
|
|
|
|
if space: self.data += " "
|
|
|
|
self.data += string
|
|
|
|
else: raise InternalError("Appending to object!")
|
|
|
|
def set(self, data):
|
|
|
|
self.data = data
|
|
|
|
def __str__(self):
|
|
|
|
return str((str(self.element), str(self.data)))
|
|
|
|
def __repl__(self): return self.__str__()
|
|
|
|
class Table:
|
|
|
|
def __init__(self, data=None):
|
|
|
|
if data == None: self.data = []
|
|
|
|
else:
|
|
|
|
self.data = []
|
|
|
|
self.addrow(data)
|
|
|
|
def addrow(self, data):
|
|
|
|
if type(data) == str:
|
|
|
|
buf = [i.rstrip().lstrip() for i in data.split("|")]
|
|
|
|
if len(self.data) > 0:
|
|
|
|
if len(self.data[-1]) == len(buf):
|
|
|
|
self.data.append(buf)
|
|
|
|
else:
|
|
|
|
raise ParsingError("tables must have uniform dimensions throughout")
|
|
|
|
else:
|
|
|
|
self.data.append(buf)
|
|
|
|
elif type(data) == list:
|
|
|
|
if len(self.data) > 0:
|
|
|
|
if len(self.data[-1]) == len(data):
|
|
|
|
self.data.append(data)
|
|
|
|
else:
|
|
|
|
raise ParsingError("tables must have uniform dimensions throughout")
|
|
|
|
else:
|
|
|
|
self.data.append(data)
|
|
|
|
def __str__(self):
|
|
|
|
return str(self.data)
|
|
|
|
def __repl__(self): return self.__str__()
|
2021-06-29 13:07:22 -05:00
|
|
|
|
|
|
|
def printchain(chain):
|
|
|
|
for i in chain:
|
|
|
|
if not i.data == None: print(i.data, i.element)
|
|
|
|
else: print(i.element)
|
|
|
|
|
|
|
|
chain = []
|
2021-06-29 16:48:03 -05:00
|
|
|
flags = {
|
|
|
|
"TABLE_PREV": False,
|
|
|
|
}
|
|
|
|
def main():
|
|
|
|
for i in finput():
|
|
|
|
i = i.rstrip()
|
|
|
|
|
|
|
|
# ignore blank lines
|
|
|
|
if len(i) == 0: continue
|
|
|
|
|
|
|
|
# lines with text
|
|
|
|
if i[0].isalpha(): chain.append(Element("p", i))
|
|
|
|
|
|
|
|
# we have a processing command
|
|
|
|
if not i[0].isalpha():
|
|
|
|
command = ""
|
|
|
|
params = ""
|
|
|
|
counter = 0
|
|
|
|
for j in i:
|
|
|
|
if not j.isalpha() \
|
|
|
|
and not j == ' ':
|
|
|
|
command += j
|
2021-06-29 13:07:22 -05:00
|
|
|
else:
|
2021-06-29 16:48:03 -05:00
|
|
|
if j == ' ':
|
|
|
|
params = i[counter + 1:]
|
|
|
|
else:
|
|
|
|
params = i[counter:]
|
|
|
|
break
|
|
|
|
counter += 1
|
|
|
|
if command[0] == "#": chain.append(Element("h" + str(len(command)), params))
|
|
|
|
elif command == "%": chain.append(Element("c", params))
|
|
|
|
elif command == "*": chain.append(Element("l", params))
|
|
|
|
elif command == "-": chain.append(Element("l", params))
|
|
|
|
elif command == ">": chain.append(Element("q", params))
|
|
|
|
elif command == "=": chain.append(Element("r", params))
|
|
|
|
elif command == "|":
|
|
|
|
if flags["TABLE_PREV"]:
|
|
|
|
chain[-1].data.addrow(params)
|
|
|
|
continue
|
|
|
|
else:
|
|
|
|
chain.append(Element("t", Table(params)))
|
|
|
|
flags["TABLE_PREV"] = True
|
|
|
|
continue
|
|
|
|
flags["TABLE_PREV"] = False
|
|
|
|
printchain(chain)
|
|
|
|
|
|
|
|
if __name__ == "__main__": main()
|