offpunk starts to work

This commit is contained in:
Lionel Dricot 2023-07-21 17:33:55 +02:00
parent 1a3d839ab3
commit d95d4dc5af
3 changed files with 59 additions and 47 deletions

View File

@ -447,8 +447,37 @@ class AbstractRenderer():
for l in self.get_subscribe_links()[1:]: for l in self.get_subscribe_links()[1:]:
self.links[mode].append(l[0]) self.links[mode].append(l[0])
return self.links[mode] return self.links[mode]
#get_title is about the "content title", so the title in the page itself
def get_title(self): def get_title(self):
return "Abstract title" return "Abstract title"
#this function is about creating a title derived from the URL
def get_url_title(self):
#small intelligence to try to find a good name for a capsule
#we try to find eithe ~username or /users/username
#else we fallback to hostname
#TODO: handle local name
# if self.local:
# if self.name != "":
# red_title = self.name
# else:
# red_title = self.path
# else:
#TODO: handle host and path separation
red_title = "TODO:host" #self.host
path = self.url
if "user" in path:
i = 0
splitted = path.split("/")
while i < (len(splitted)-1):
if splitted[i].startswith("user"):
red_title = splitted[i+1]
i += 1
if "~" in path:
for pp in path.split("/"):
if pp.startswith("~"):
red_title = pp[1:]
return red_title
# This function return a list of URL which should be downloaded # This function return a list of URL which should be downloaded
# before displaying the page (images in HTML pages, typically) # before displaying the page (images in HTML pages, typically)
@ -1179,10 +1208,13 @@ def get_mime(path):
def renderer_from_file(path,url=None): def renderer_from_file(path,url=None):
mime = get_mime(path) mime = get_mime(path)
with open(path) as f: if os.path.exists(path):
body = f.read() with open(path) as f:
f.close() body = f.read()
return set_renderer(body,url,mime) f.close()
return set_renderer(body,url,mime)
else:
return None
def set_renderer(content,url,mime): def set_renderer(content,url,mime):
renderer = None renderer = None

View File

@ -21,7 +21,7 @@ cache_home = os.environ.get('XDG_CACHE_HOME') or\
#_CACHE_PATH = os.path.join(cache_home,"offpunk/") #_CACHE_PATH = os.path.join(cache_home,"offpunk/")
#Debug: #Debug:
_CACHE_PATH = "/home/ploum/dev/netcache/" _CACHE_PATH = "/home/ploum/dev/netcache/"
_DATA_DIR = "/home/ploum/dev/netcache/"
if not os.path.exists(_CACHE_PATH): if not os.path.exists(_CACHE_PATH):
print("Creating cache directory {}".format(_CACHE_PATH)) print("Creating cache directory {}".format(_CACHE_PATH))
os.makedirs(_CACHE_PATH) os.makedirs(_CACHE_PATH)

View File

@ -176,6 +176,8 @@ class GeminiItem():
self.mime = None self.mime = None
self.renderer = ansirenderer.renderer_from_file(self._cache_path,self.url) self.renderer = ansirenderer.renderer_from_file(self._cache_path,self.url)
#TODO : stuff have been migrated to netcache. What are we missing here ? #TODO : stuff have been migrated to netcache. What are we missing here ?
self.scheme = "https"
self.local = False
def get_cache_path(self): def get_cache_path(self):
# if we already have a _cache_path, we returns it. # if we already have a _cache_path, we returns it.
@ -186,38 +188,14 @@ class GeminiItem():
# return self._cache_path # return self._cache_path
return netcache.get_cache_path(self.url) return netcache.get_cache_path(self.url)
def get_capsule_title(self):
#small intelligence to try to find a good name for a capsule
#we try to find eithe ~username or /users/username
#else we fallback to hostname
if self.local:
if self.name != "":
red_title = self.name
else:
red_title = self.path
else:
red_title = self.host
if "user" in self.path:
i = 0
splitted = self.path.split("/")
while i < (len(splitted)-1):
if splitted[i].startswith("user"):
red_title = splitted[i+1]
i += 1
if "~" in self.path:
for pp in self.path.split("/"):
if pp.startswith("~"):
red_title = pp[1:]
return red_title
def get_page_title(self): def get_page_title(self):
title = "" title = ""
if self.renderer: if self.renderer:
title = self.renderer.get_title() title = self.renderer.get_title()
if not title or len(title) == 0: if not title or len(title) == 0:
title = self.get_capsule_title() title = self.renderer.get_url_title()
else: else:
title += " (%s)" %self.get_capsule_title() title += " (%s)" %self.renderer.get_url_title()
return title return title
def is_cache_valid(self,validity=0): def is_cache_valid(self,validity=0):
@ -319,7 +297,7 @@ class GeminiItem():
mode = self.last_mode mode = self.last_mode
else: else:
self.last_mode = mode self.last_mode = mode
title = self.get_capsule_title() title = self.renderer.get_url_title()
if self.is_cache_valid(): #and self.offline_only and not self.local: if self.is_cache_valid(): #and self.offline_only and not self.local:
nbr = len(self.get_links(mode=mode)) nbr = len(self.get_links(mode=mode))
if self.local: if self.local:
@ -429,6 +407,7 @@ class GeminiItem():
url += "##offpunk_mode=" + self.last_mode url += "##offpunk_mode=" + self.last_mode
return url return url
#what is the line to add to a list for this url?
def to_map_line(self): def to_map_line(self):
return "=> {} {}\n".format(self.url_mode(), self.get_page_title()) return "=> {} {}\n".format(self.url_mode(), self.get_page_title())
@ -617,21 +596,22 @@ class GeminiClient(cmd.Cmd):
if not gi: if not gi:
return return
# Don't try to speak to servers running other protocols # Don't try to speak to servers running other protocols
elif gi.scheme == "mailto": #TODO: support for mailto and unsupported protocols
if handle and not self.sync_only: # elif gi.scheme == "mailto":
resp = input("Send an email to %s Y/N? " %gi.path) # if handle and not self.sync_only:
self.gi = gi # resp = input("Send an email to %s Y/N? " %gi.path)
if resp.strip().lower() in ("y", "yes"): # self.gi = gi
if _HAS_XDGOPEN : # if resp.strip().lower() in ("y", "yes"):
run("xdg-open mailto:%s", parameter=gi.path ,direct_output=True) # if _HAS_XDGOPEN :
else: # run("xdg-open mailto:%s", parameter=gi.path ,direct_output=True)
print("Cannot find a mail client to send mail to %s" %gi.path) # else:
print("Please install xdg-open (usually from xdg-util package)") # print("Cannot find a mail client to send mail to %s" %gi.path)
return # print("Please install xdg-open (usually from xdg-util package)")
elif gi.scheme not in ["file","list"] and gi.scheme not in netcache.standard_ports \ # return
and not self.sync_only: # elif gi.scheme not in ["file","list"] and gi.scheme not in netcache.standard_ports \
print("Sorry, no support for {} links.".format(gi.scheme)) # and not self.sync_only:
return # print("Sorry, no support for {} links.".format(gi.scheme))
# return
if not mode: if not mode:
mode = gi.last_mode mode = gi.last_mode