From 74ba669a399399b67824fc5bab760862fd9e2a19 Mon Sep 17 00:00:00 2001 From: Romain de Laage Date: Fri, 4 Dec 2020 20:50:03 +0100 Subject: [PATCH] Improve comments --- gemini.py | 7 +++++-- main.py | 31 +++++++++++++++++++++++-------- renderEngine.py | 41 +++++++++++++++++++++++++++++++++++++++-- 3 files changed, 67 insertions(+), 12 deletions(-) diff --git a/gemini.py b/gemini.py index 1c11270..2fdde86 100644 --- a/gemini.py +++ b/gemini.py @@ -23,6 +23,7 @@ class Request(): context.check_hostname = False context.verify_mode = ssl.CERT_NONE s = context.wrap_socket(s, server_hostname = self.hostname) + # Send request s.sendall((self.url + '\r\n').encode("UTF-8")) @@ -45,6 +46,8 @@ class Request(): # Return the status code return self.status + # Something went wrong during the connecgtion to the server except Exception as err: - print(err) - exit(1) + self.meta = err + self.content = str(err) + return "err" diff --git a/main.py b/main.py index 8a4d453..d8d629c 100644 --- a/main.py +++ b/main.py @@ -5,16 +5,20 @@ import gemini import renderEngine import urllib.parse -# See https://tildegit.org/solderpunk/gemini-demo-1/src/branch/master/gemini-demo.py - class Application(): def __init__(self): + # Some global definitions self.root = tk.Tk() self.nav_bar = NavBar(self) self.content = Content(self) - self.root.mainloop() self.current_URL = "about:home" + # Set window title + self.root.title("tkGemini") + + # App main loop + self.root.mainloop() + def updateContent(self): # Get url asked url=self.nav_bar.getURL() @@ -75,8 +79,10 @@ class Application(): tkinter.messagebox.showerror("URL error", url+" is not a correct Gemini URL ! (HTTP/S is not supported, please use a web browser instead)") def verifyURL(url): + # Is it an absolute path ? if "://" in url: parsed_url = urllib.parse.urlparse(url) + # Is this path supported ? if parsed_url.scheme == "gemini": return True else: @@ -84,35 +90,44 @@ def verifyURL(url): class NavBar(): def __init__(self, parent): + # Some global definitions self.parent = parent - self.URL_var = tk.StringVar() + + # All widgets are in a frame self.root = tk.Frame(self.parent.root) self.root.grid(column=0, row=0) + + # Define URL bar and variable + self.URL_var = tk.StringVar() self.URL_bar = tk.Entry(self.root, textvariable=self.URL_var) self.URL_var.set("about:home") self.URL_bar.grid(column=0, row=0) + + # Define go button with action self.go_button = tk.Button(self.root, text='Go !', command=self.parent.updateContent) self.go_button.grid(column=1, row=0) def getURL(self): + # Get URL from URL bar variable return self.URL_var.get() class Content(): def __init__(self, parent): self.parent = parent + + # Text container with home page self.root = renderEngine.Renderer(self.parent, "#Home page\nWelcome on tkGemini, a browser software for Gemini protocole\n=>gemini://rdelaage.ovh My gemini space") self.root.parse() self.root.content.grid(column=0, row=1) + + # Scrollbar beside to the text container self.scroll_bar = tk.Scrollbar(command=self.root.content.yview, orient='vertical') self.scroll_bar.grid(column=1, row=1) self.root.content['yscrollcommand'] = self.scroll_bar.set + # When content is updated def setContent(self, new_content): self.root.text = new_content self.root.parse() - def linkClick(self, url): - self.parent.nav_bar.URL_var = url - self.parent.updateContent - app = Application() diff --git a/renderEngine.py b/renderEngine.py index 2fe31b2..49f505f 100644 --- a/renderEngine.py +++ b/renderEngine.py @@ -1,11 +1,17 @@ import tkinter as tk import tkinter.font as tkFont +# Renderer class to transform a Gemini text into a visual text formatted class Renderer(): def __init__(self, parent, text): + # Some global variable self.parent = parent self.text = text + self.currX = 0 + self.currY = 1 self.content = tk.Text(self.parent.root) + + # Definition for some styles self.h1Font = tkFont.Font(weight="bold", size="26") self.content.tag_config("h1", font=self.h1Font) self.h2Font = tkFont.Font(weight="bold", size="20") @@ -13,50 +19,73 @@ class Renderer(): 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 def addNormalText(self, text): + # Draw normal text on the current line and go to the next line self.content.insert(str(self.currY)+'.'+str(self.currX), text+"\n") self.currY+=1 def addLink(self, text, url): + # Calculate current index (position) index1 = str(self.currY)+'.'+str(self.currX) + + #prepare tag for this link (format + action when clicking) self.content.tag_config("link-"+index1, foreground="#00f", font=self.underlineFont) self.content.tag_bind("link-"+index1, "", lambda event, url=url, parent=self.parent: parent.linkClick(url)) + + # Add text, add tag from the beginning to the end and go to a new line 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): + # Calculate current index index1 = str(self.currY)+'.'+str(self.currX) + + # Insert text then apply tag and go to a new line 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): + # Calculate current index index1 = str(self.currY)+'.'+str(self.currX) + + # Insert text then apply tag and go to a new line 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): + # Calculate current index index1 = str(self.currY)+'.'+str(self.currX) + + # Inset text then apply tag and go to a new line 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): + # Same as drawing a normal line but with a tab and a bullet point at the beginning self.content.insert(str(self.currY)+'.'+str(self.currX), "\t• "+text+"\n") self.currY+=1 def parse(self): + # Make text editable self.content['state'] = 'normal' + + # Reinitialize position self.currY=1 self.currX=0 + + # Delete all the current text self.content.delete("1.0", tk.END) + + # Cut the text into line lines = self.text.split("\n") + + # Parse each line for line in lines: if(line.startswith("###")): self.addHeading3(line[3:].strip()) @@ -68,12 +97,20 @@ class Renderer(): self.addListItem(line[1:].strip()) elif(line.startswith("=>")): line = line[2:].strip() + + # Link location is before the first whitespace url = line.split()[0] + + # Link text is after the first whitespace text = line[len(url):len(line)].strip() + + # If there is no text, then the link location become the text if text == "": text = url + self.addLink(text, url) else: self.addNormalText(line) + # Now the text should not be edited self.content['state'] = 'disabled'