The goal is near (make parse working)

This commit is contained in:
Romain de Laage 2020-12-04 16:50:47 +01:00
parent 029889966f
commit 223ceb8702
Signed by: rdelaage
GPG Key ID: 534845FADDF0C329
1 changed files with 71 additions and 11 deletions

View File

@ -2,33 +2,93 @@ import tkinter as tk
import tkinter.font as tkFont
class Renderer():
def __init__(self, parent):
def __init__(self, parent, text):
self.parent = parent
self.underlineFont = tkFont.Font(underline=1)
self.text = text
self.content = tk.Text(self.parent)
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)
self.currX = 0
self.currY = 1
self.addNormalText("test")
self.addLink("mylink")
self.addNormalText("test2")
self.addLink("mylink2")
self.content['state'] = 'disabled'
self.content.pack()
def addNormalText(self, text):
self.content.insert(str(self.currY)+'.'+str(self.currX), text+"\n")
self.currY+=1
def addLink(self, text):
def addLink(self, text, url):
index1 = str(self.currY)+'.'+str(self.currX)
self.content.tag_config("link-"+index1, foreground="#00f", font=self.underlineFont)
self.content.tag_bind("link-"+index1, "<Button-1>", lambda event, url=text:
self.content.tag_bind("link-"+index1, "<Button-1>", lambda event, url=url:
print(url))
self.content.insert(index1, text+"\n")
self.content.tag_add("link-"+index1, index1, str(self.currY)+'.'+str(self.currX+len(text)))
self.currY+=1
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):
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.pack()
self.content['state'] = 'disabled'
text = """# Mon site
* item 1
*item2
=>gemini://rdelaage.ovh test
=> gemini://lol
##Test2
### TEST3
mon texte"""
app = tk.Tk()
app.title("Test")
render = Renderer(app)
render = Renderer(app, text)
render.parse()
app.mainloop()