added table support and cleaned up a few other things
This commit is contained in:
parent
6fd3e868ac
commit
043cc2051c
112
uml.py
112
uml.py
|
@ -1,6 +1,7 @@
|
||||||
from fileinput import input as finput
|
from fileinput import input as finput
|
||||||
|
import re
|
||||||
|
|
||||||
class UntitledError: pass
|
class UntitledError(BaseException): pass
|
||||||
class InternalError(UntitledError): pass
|
class InternalError(UntitledError): pass
|
||||||
class ParsingError(UntitledError): pass
|
class ParsingError(UntitledError): pass
|
||||||
class MiscError(UntitledError): pass
|
class MiscError(UntitledError): pass
|
||||||
|
@ -11,8 +12,42 @@ class Element:
|
||||||
else: self.data = data
|
else: self.data = data
|
||||||
self.element = element
|
self.element = element
|
||||||
def append(self, string, space=False):
|
def append(self, string, space=False):
|
||||||
if space: self.data += " "
|
if type(data) == str:
|
||||||
self.data += string
|
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__()
|
||||||
|
|
||||||
def printchain(chain):
|
def printchain(chain):
|
||||||
for i in chain:
|
for i in chain:
|
||||||
|
@ -20,35 +55,50 @@ def printchain(chain):
|
||||||
else: print(i.element)
|
else: print(i.element)
|
||||||
|
|
||||||
chain = []
|
chain = []
|
||||||
for i in finput():
|
flags = {
|
||||||
i = i.rstrip()
|
"TABLE_PREV": False,
|
||||||
|
}
|
||||||
|
def main():
|
||||||
|
for i in finput():
|
||||||
|
i = i.rstrip()
|
||||||
|
|
||||||
# ignore blank lines
|
# ignore blank lines
|
||||||
if len(i) == 0: continue
|
if len(i) == 0: continue
|
||||||
|
|
||||||
# lines with text
|
# lines with text
|
||||||
if i[0].isalpha(): chain.append(Element("p", i))
|
if i[0].isalpha(): chain.append(Element("p", i))
|
||||||
|
|
||||||
# we have a processing command
|
# we have a processing command
|
||||||
if not i[0].isalpha():
|
if not i[0].isalpha():
|
||||||
command = ""
|
command = ""
|
||||||
params = ""
|
params = ""
|
||||||
counter = 0
|
counter = 0
|
||||||
for j in i:
|
for j in i:
|
||||||
if not j.isalpha() \
|
if not j.isalpha() \
|
||||||
and not j == ' ':
|
and not j == ' ':
|
||||||
command += j
|
command += j
|
||||||
else:
|
|
||||||
if j == ' ':
|
|
||||||
params = i[counter + 1:]
|
|
||||||
else:
|
else:
|
||||||
params = i[counter:]
|
if j == ' ':
|
||||||
break
|
params = i[counter + 1:]
|
||||||
counter += 1
|
else:
|
||||||
if command[0] == "#": chain.append(Element("h" + str(len(command)), params))
|
params = i[counter:]
|
||||||
elif command == "%": chain.append(Element("c", params))
|
break
|
||||||
elif command == "*": chain.append(Element("l", params))
|
counter += 1
|
||||||
elif command == "-": chain.append(Element("l", params))
|
if command[0] == "#": chain.append(Element("h" + str(len(command)), params))
|
||||||
elif command == ">": chain.append(Element("q", params))
|
elif command == "%": chain.append(Element("c", params))
|
||||||
elif command == "=": chain.append(Element("r", params))
|
elif command == "*": chain.append(Element("l", params))
|
||||||
printchain(chain)
|
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()
|
||||||
|
|
Loading…
Reference in New Issue