add paragraph and table rendering support

This commit is contained in:
randomuser 2021-06-30 00:14:07 -05:00
parent 043cc2051c
commit b13d3ab3e9
1 changed files with 61 additions and 1 deletions

62
uml.py
View File

@ -21,6 +21,7 @@ class Element:
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 = []
@ -54,6 +55,63 @@ def printchain(chain):
if not i.data == None: print(i.data, i.element)
else: print(i.element)
def renderer(chain):
for i in chain:
rendered = False
if i.element == "h1":
rendered = True
print("+" + (len(i.data) + 2) * "-" + "+")
print("| " + i.data + " |")
print("+" + (len(i.data) + 2) * "-" + "+")
elif i.element == "h2":
rendered = True
print(" " + i.data + " ")
print("=" * (len(i.data) + 2))
elif i.element == "h3":
rendered = True
print(" " + i.data + " ")
print("-" * (len(i.data) + 2))
elif "h" in i.element:
raise ParsingError("you cannot have more than three levels of headers")
elif i.element == "p":
rendered = True
isplit = i.data.split(' ')
buf = ""
for i in isplit:
if (len(i) + len(buf) + 1) <= 63:
if not len(buf) == 0: buf += " "
buf += i
else:
print(buf)
buf = ""
buf += i
print(buf)
elif i.element == "t":
buf = [0] * len(i.data.data[0])
for j in i.data.data:
c = 0
for k in j:
if len(k) > buf[c]: buf[c] = len(k)
c += 1
width = 0
for j in buf:
width += j
width += 2 * len(i.data.data[0]) + 1
print("+" + "-" * width + "+")
# don't ask
for j in range(len(i.data.data)):
c = 0
for k in range(len(i.data.data[j])):
if len(i.data.data[j][k]) < buf[c]:
i.data.data[j][k] = i.data.data[j][k].ljust(buf[c], " ")
cbuf = ""
for j in i.data.data:
cbuf = "| " + ' | '.join(j) + " |"
print(cbuf)
print("+" + "-" * width + "+")
if rendered: print()
chain = []
flags = {
"TABLE_PREV": False,
@ -99,6 +157,8 @@ def main():
flags["TABLE_PREV"] = True
continue
flags["TABLE_PREV"] = False
printchain(chain)
continue
# printchain(chain)
renderer(chain)
if __name__ == "__main__": main()