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:]:
self.links[mode].append(l[0])
return self.links[mode]
#get_title is about the "content title", so the title in the page itself
def get_title(self):
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
# before displaying the page (images in HTML pages, typically)
@ -1179,10 +1208,13 @@ def get_mime(path):
def renderer_from_file(path,url=None):
mime = get_mime(path)
with open(path) as f:
body = f.read()
f.close()
return set_renderer(body,url,mime)
if os.path.exists(path):
with open(path) as f:
body = f.read()
f.close()
return set_renderer(body,url,mime)
else:
return None
def set_renderer(content,url,mime):
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/")
#Debug:
_CACHE_PATH = "/home/ploum/dev/netcache/"
_DATA_DIR = "/home/ploum/dev/netcache/"
if not os.path.exists(_CACHE_PATH):
print("Creating cache directory {}".format(_CACHE_PATH))
os.makedirs(_CACHE_PATH)

View File

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