Improve comments
This commit is contained in:
parent
c7ad9e260e
commit
74ba669a39
|
@ -23,6 +23,7 @@ class Request():
|
||||||
context.check_hostname = False
|
context.check_hostname = False
|
||||||
context.verify_mode = ssl.CERT_NONE
|
context.verify_mode = ssl.CERT_NONE
|
||||||
s = context.wrap_socket(s, server_hostname = self.hostname)
|
s = context.wrap_socket(s, server_hostname = self.hostname)
|
||||||
|
|
||||||
# Send request
|
# Send request
|
||||||
s.sendall((self.url + '\r\n').encode("UTF-8"))
|
s.sendall((self.url + '\r\n').encode("UTF-8"))
|
||||||
|
|
||||||
|
@ -45,6 +46,8 @@ class Request():
|
||||||
# Return the status code
|
# Return the status code
|
||||||
return self.status
|
return self.status
|
||||||
|
|
||||||
|
# Something went wrong during the connecgtion to the server
|
||||||
except Exception as err:
|
except Exception as err:
|
||||||
print(err)
|
self.meta = err
|
||||||
exit(1)
|
self.content = str(err)
|
||||||
|
return "err"
|
||||||
|
|
31
main.py
31
main.py
|
@ -5,16 +5,20 @@ import gemini
|
||||||
import renderEngine
|
import renderEngine
|
||||||
import urllib.parse
|
import urllib.parse
|
||||||
|
|
||||||
# See https://tildegit.org/solderpunk/gemini-demo-1/src/branch/master/gemini-demo.py
|
|
||||||
|
|
||||||
class Application():
|
class Application():
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
|
# Some global definitions
|
||||||
self.root = tk.Tk()
|
self.root = tk.Tk()
|
||||||
self.nav_bar = NavBar(self)
|
self.nav_bar = NavBar(self)
|
||||||
self.content = Content(self)
|
self.content = Content(self)
|
||||||
self.root.mainloop()
|
|
||||||
self.current_URL = "about:home"
|
self.current_URL = "about:home"
|
||||||
|
|
||||||
|
# Set window title
|
||||||
|
self.root.title("tkGemini")
|
||||||
|
|
||||||
|
# App main loop
|
||||||
|
self.root.mainloop()
|
||||||
|
|
||||||
def updateContent(self):
|
def updateContent(self):
|
||||||
# Get url asked
|
# Get url asked
|
||||||
url=self.nav_bar.getURL()
|
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)")
|
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):
|
def verifyURL(url):
|
||||||
|
# Is it an absolute path ?
|
||||||
if "://" in url:
|
if "://" in url:
|
||||||
parsed_url = urllib.parse.urlparse(url)
|
parsed_url = urllib.parse.urlparse(url)
|
||||||
|
# Is this path supported ?
|
||||||
if parsed_url.scheme == "gemini":
|
if parsed_url.scheme == "gemini":
|
||||||
return True
|
return True
|
||||||
else:
|
else:
|
||||||
|
@ -84,35 +90,44 @@ def verifyURL(url):
|
||||||
|
|
||||||
class NavBar():
|
class NavBar():
|
||||||
def __init__(self, parent):
|
def __init__(self, parent):
|
||||||
|
# Some global definitions
|
||||||
self.parent = parent
|
self.parent = parent
|
||||||
self.URL_var = tk.StringVar()
|
|
||||||
|
# All widgets are in a frame
|
||||||
self.root = tk.Frame(self.parent.root)
|
self.root = tk.Frame(self.parent.root)
|
||||||
self.root.grid(column=0, row=0)
|
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_bar = tk.Entry(self.root, textvariable=self.URL_var)
|
||||||
self.URL_var.set("about:home")
|
self.URL_var.set("about:home")
|
||||||
self.URL_bar.grid(column=0, row=0)
|
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 = tk.Button(self.root, text='Go !', command=self.parent.updateContent)
|
||||||
self.go_button.grid(column=1, row=0)
|
self.go_button.grid(column=1, row=0)
|
||||||
|
|
||||||
def getURL(self):
|
def getURL(self):
|
||||||
|
# Get URL from URL bar variable
|
||||||
return self.URL_var.get()
|
return self.URL_var.get()
|
||||||
|
|
||||||
class Content():
|
class Content():
|
||||||
def __init__(self, parent):
|
def __init__(self, parent):
|
||||||
self.parent = 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 = 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.parse()
|
||||||
self.root.content.grid(column=0, row=1)
|
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 = tk.Scrollbar(command=self.root.content.yview, orient='vertical')
|
||||||
self.scroll_bar.grid(column=1, row=1)
|
self.scroll_bar.grid(column=1, row=1)
|
||||||
self.root.content['yscrollcommand'] = self.scroll_bar.set
|
self.root.content['yscrollcommand'] = self.scroll_bar.set
|
||||||
|
|
||||||
|
# When content is updated
|
||||||
def setContent(self, new_content):
|
def setContent(self, new_content):
|
||||||
self.root.text = new_content
|
self.root.text = new_content
|
||||||
self.root.parse()
|
self.root.parse()
|
||||||
|
|
||||||
def linkClick(self, url):
|
|
||||||
self.parent.nav_bar.URL_var = url
|
|
||||||
self.parent.updateContent
|
|
||||||
|
|
||||||
app = Application()
|
app = Application()
|
||||||
|
|
|
@ -1,11 +1,17 @@
|
||||||
import tkinter as tk
|
import tkinter as tk
|
||||||
import tkinter.font as tkFont
|
import tkinter.font as tkFont
|
||||||
|
|
||||||
|
# Renderer class to transform a Gemini text into a visual text formatted
|
||||||
class Renderer():
|
class Renderer():
|
||||||
def __init__(self, parent, text):
|
def __init__(self, parent, text):
|
||||||
|
# Some global variable
|
||||||
self.parent = parent
|
self.parent = parent
|
||||||
self.text = text
|
self.text = text
|
||||||
|
self.currX = 0
|
||||||
|
self.currY = 1
|
||||||
self.content = tk.Text(self.parent.root)
|
self.content = tk.Text(self.parent.root)
|
||||||
|
|
||||||
|
# Definition for some styles
|
||||||
self.h1Font = tkFont.Font(weight="bold", size="26")
|
self.h1Font = tkFont.Font(weight="bold", size="26")
|
||||||
self.content.tag_config("h1", font=self.h1Font)
|
self.content.tag_config("h1", font=self.h1Font)
|
||||||
self.h2Font = tkFont.Font(weight="bold", size="20")
|
self.h2Font = tkFont.Font(weight="bold", size="20")
|
||||||
|
@ -13,50 +19,73 @@ class Renderer():
|
||||||
self.h3Font = tkFont.Font(weight="bold", size="16")
|
self.h3Font = tkFont.Font(weight="bold", size="16")
|
||||||
self.content.tag_config("h3", font=self.h3Font)
|
self.content.tag_config("h3", font=self.h3Font)
|
||||||
self.underlineFont = tkFont.Font(underline=1)
|
self.underlineFont = tkFont.Font(underline=1)
|
||||||
self.currX = 0
|
|
||||||
self.currY = 1
|
|
||||||
|
|
||||||
def addNormalText(self, text):
|
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.content.insert(str(self.currY)+'.'+str(self.currX), text+"\n")
|
||||||
self.currY+=1
|
self.currY+=1
|
||||||
|
|
||||||
def addLink(self, text, url):
|
def addLink(self, text, url):
|
||||||
|
# Calculate current index (position)
|
||||||
index1 = str(self.currY)+'.'+str(self.currX)
|
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_config("link-"+index1, foreground="#00f", font=self.underlineFont)
|
||||||
self.content.tag_bind("link-"+index1, "<Button-1>", lambda event, url=url, parent=self.parent:
|
self.content.tag_bind("link-"+index1, "<Button-1>", lambda event, url=url, parent=self.parent:
|
||||||
parent.linkClick(url))
|
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.insert(index1, text+"\n")
|
||||||
self.content.tag_add("link-"+index1, index1, str(self.currY)+'.'+str(self.currX+len(text)))
|
self.content.tag_add("link-"+index1, index1, str(self.currY)+'.'+str(self.currX+len(text)))
|
||||||
self.currY+=1
|
self.currY+=1
|
||||||
|
|
||||||
def addHeading1(self, text):
|
def addHeading1(self, text):
|
||||||
|
# Calculate current index
|
||||||
index1 = str(self.currY)+'.'+str(self.currX)
|
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.insert(index1, text+"\n")
|
||||||
self.content.tag_add("h1", index1, str(self.currY)+'.'+str(self.currX+len(text)))
|
self.content.tag_add("h1", index1, str(self.currY)+'.'+str(self.currX+len(text)))
|
||||||
self.currY+=1
|
self.currY+=1
|
||||||
|
|
||||||
def addHeading2(self, text):
|
def addHeading2(self, text):
|
||||||
|
# Calculate current index
|
||||||
index1 = str(self.currY)+'.'+str(self.currX)
|
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.insert(index1, text+"\n")
|
||||||
self.content.tag_add("h2", index1, str(self.currY)+'.'+str(self.currX+len(text)))
|
self.content.tag_add("h2", index1, str(self.currY)+'.'+str(self.currX+len(text)))
|
||||||
self.currY+=1
|
self.currY+=1
|
||||||
|
|
||||||
def addHeading3(self, text):
|
def addHeading3(self, text):
|
||||||
|
# Calculate current index
|
||||||
index1 = str(self.currY)+'.'+str(self.currX)
|
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.insert(index1, text+"\n")
|
||||||
self.content.tag_add("h3", index1, str(self.currY)+'.'+str(self.currX+len(text)))
|
self.content.tag_add("h3", index1, str(self.currY)+'.'+str(self.currX+len(text)))
|
||||||
self.currY+=1
|
self.currY+=1
|
||||||
|
|
||||||
def addListItem(self, text):
|
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.content.insert(str(self.currY)+'.'+str(self.currX), "\t• "+text+"\n")
|
||||||
self.currY+=1
|
self.currY+=1
|
||||||
|
|
||||||
def parse(self):
|
def parse(self):
|
||||||
|
# Make text editable
|
||||||
self.content['state'] = 'normal'
|
self.content['state'] = 'normal'
|
||||||
|
|
||||||
|
# Reinitialize position
|
||||||
self.currY=1
|
self.currY=1
|
||||||
self.currX=0
|
self.currX=0
|
||||||
|
|
||||||
|
# Delete all the current text
|
||||||
self.content.delete("1.0", tk.END)
|
self.content.delete("1.0", tk.END)
|
||||||
|
|
||||||
|
# Cut the text into line
|
||||||
lines = self.text.split("\n")
|
lines = self.text.split("\n")
|
||||||
|
|
||||||
|
# Parse each line
|
||||||
for line in lines:
|
for line in lines:
|
||||||
if(line.startswith("###")):
|
if(line.startswith("###")):
|
||||||
self.addHeading3(line[3:].strip())
|
self.addHeading3(line[3:].strip())
|
||||||
|
@ -68,12 +97,20 @@ class Renderer():
|
||||||
self.addListItem(line[1:].strip())
|
self.addListItem(line[1:].strip())
|
||||||
elif(line.startswith("=>")):
|
elif(line.startswith("=>")):
|
||||||
line = line[2:].strip()
|
line = line[2:].strip()
|
||||||
|
|
||||||
|
# Link location is before the first whitespace
|
||||||
url = line.split()[0]
|
url = line.split()[0]
|
||||||
|
|
||||||
|
# Link text is after the first whitespace
|
||||||
text = line[len(url):len(line)].strip()
|
text = line[len(url):len(line)].strip()
|
||||||
|
|
||||||
|
# If there is no text, then the link location become the text
|
||||||
if text == "":
|
if text == "":
|
||||||
text = url
|
text = url
|
||||||
|
|
||||||
self.addLink(text, url)
|
self.addLink(text, url)
|
||||||
else:
|
else:
|
||||||
self.addNormalText(line)
|
self.addNormalText(line)
|
||||||
|
|
||||||
|
# Now the text should not be edited
|
||||||
self.content['state'] = 'disabled'
|
self.content['state'] = 'disabled'
|
||||||
|
|
Reference in New Issue