2020-12-04 09:51:55 +01:00
|
|
|
import tkinter as tk
|
2020-12-04 10:02:39 +01:00
|
|
|
import tkinter.font as tkFont
|
2020-12-04 09:51:55 +01:00
|
|
|
|
|
|
|
class Renderer():
|
2020-12-04 16:50:47 +01:00
|
|
|
def __init__(self, parent, text):
|
2020-12-04 09:51:55 +01:00
|
|
|
self.parent = parent
|
2020-12-04 16:50:47 +01:00
|
|
|
self.text = text
|
2020-12-04 19:07:58 +01:00
|
|
|
self.content = tk.Text(self.parent.root)
|
2020-12-04 16:50:47 +01:00
|
|
|
self.h1Font = tkFont.Font(weight="bold", size="26")
|
|
|
|
self.content.tag_config("h1", font=self.h1Font)
|
|
|
|
self.h2Font = tkFont.Font(weight="bold", size="20")
|
|
|
|
self.content.tag_config("h2", font=self.h2Font)
|
|
|
|
self.h3Font = tkFont.Font(weight="bold", size="16")
|
|
|
|
self.content.tag_config("h3", font=self.h3Font)
|
|
|
|
self.underlineFont = tkFont.Font(underline=1)
|
2020-12-04 09:51:55 +01:00
|
|
|
self.currX = 0
|
|
|
|
self.currY = 1
|
|
|
|
|
|
|
|
def addNormalText(self, text):
|
|
|
|
self.content.insert(str(self.currY)+'.'+str(self.currX), text+"\n")
|
|
|
|
self.currY+=1
|
|
|
|
|
2020-12-04 16:50:47 +01:00
|
|
|
def addLink(self, text, url):
|
2020-12-04 09:51:55 +01:00
|
|
|
index1 = str(self.currY)+'.'+str(self.currX)
|
2020-12-04 10:02:39 +01:00
|
|
|
self.content.tag_config("link-"+index1, foreground="#00f", font=self.underlineFont)
|
2020-12-04 19:07:58 +01:00
|
|
|
self.content.tag_bind("link-"+index1, "<Button-1>", lambda event, url=url, parent=self.parent:
|
|
|
|
parent.linkClick(url))
|
2020-12-04 09:51:55 +01:00
|
|
|
self.content.insert(index1, text+"\n")
|
|
|
|
self.content.tag_add("link-"+index1, index1, str(self.currY)+'.'+str(self.currX+len(text)))
|
|
|
|
self.currY+=1
|
|
|
|
|
2020-12-04 16:50:47 +01:00
|
|
|
def addHeading1(self, text):
|
|
|
|
index1 = str(self.currY)+'.'+str(self.currX)
|
|
|
|
self.content.insert(index1, text+"\n")
|
|
|
|
self.content.tag_add("h1", index1, str(self.currY)+'.'+str(self.currX+len(text)))
|
|
|
|
self.currY+=1
|
|
|
|
|
|
|
|
def addHeading2(self, text):
|
|
|
|
index1 = str(self.currY)+'.'+str(self.currX)
|
|
|
|
self.content.insert(index1, text+"\n")
|
|
|
|
self.content.tag_add("h2", index1, str(self.currY)+'.'+str(self.currX+len(text)))
|
|
|
|
self.currY+=1
|
|
|
|
|
|
|
|
def addHeading3(self, text):
|
|
|
|
index1 = str(self.currY)+'.'+str(self.currX)
|
|
|
|
self.content.insert(index1, text+"\n")
|
|
|
|
self.content.tag_add("h3", index1, str(self.currY)+'.'+str(self.currX+len(text)))
|
|
|
|
self.currY+=1
|
|
|
|
|
|
|
|
def addListItem(self, text):
|
|
|
|
self.content.insert(str(self.currY)+'.'+str(self.currX), "\t• "+text+"\n")
|
|
|
|
self.currY+=1
|
|
|
|
|
|
|
|
def parse(self):
|
2020-12-04 17:18:09 +01:00
|
|
|
self.content['state'] = 'normal'
|
|
|
|
self.currY=1
|
|
|
|
self.currX=0
|
|
|
|
self.content.delete("1.0", tk.END)
|
2020-12-04 16:50:47 +01:00
|
|
|
lines = self.text.split("\n")
|
|
|
|
for line in lines:
|
|
|
|
if(line.startswith("###")):
|
|
|
|
self.addHeading3(line[3:].strip())
|
|
|
|
elif(line.startswith("##")):
|
|
|
|
self.addHeading2(line[2:].strip())
|
|
|
|
elif(line.startswith("#")):
|
|
|
|
self.addHeading1(line[1:].strip())
|
|
|
|
elif(line.startswith("*")):
|
|
|
|
self.addListItem(line[1:].strip())
|
|
|
|
elif(line.startswith("=>")):
|
|
|
|
line = line[2:].strip()
|
|
|
|
url = line.split()[0]
|
|
|
|
text = line[len(url):len(line)].strip()
|
|
|
|
if text == "":
|
|
|
|
text = url
|
|
|
|
self.addLink(text, url)
|
|
|
|
else:
|
|
|
|
self.addNormalText(line)
|
|
|
|
|
|
|
|
self.content['state'] = 'disabled'
|