support for images nested in links

This commit is contained in:
Lionel Dricot 2022-02-10 17:19:20 +01:00
parent 691d2ee07c
commit f0177f62f8
3 changed files with 14 additions and 6 deletions

View File

@ -7,8 +7,8 @@ New Features:
(also works with feeds to see descriptions of each post instead of a simple list)
- Option --depth to customize your sync. Be warned, more than 1 is crazy.
- Option --disable-http to allows deep syncing of gemini-only ressources
- Vastly improved HTML rendering with support for images (you need the binary "chafa" on your system)
Other Small Improvements:
- Vastly improved HTML rendering with support for images (displayed as links)
- Disabled https_everywhere by default (caching problems and some websites not supporting it)
- Modified --sync logic to make it more intuitive (thanks Bjorn Westergard)
- Caching more problems to avoid refetch

View File

@ -81,6 +81,7 @@ To avoid using unstable or too recent libraries, the rule of thumb is that a lib
* [Python magic](https://github.com/ahupp/python-magic/) is useful to determine the MIME type of cached object. If not present, the file extension will be used but some capsules provide wrong extension or no extension at all. (apt-get install python3-magic)
* [Python editor](https://github.com/fmoo/python-editor) is used to edit your lists with "list edit". (apt-get install python3-editor)
* [Xsel](http://www.vergenet.net/~conrad/software/xsel/) allows to `go` to the URL copied in the clipboard without having to paste it (both X and traditional clipboards are supported). Also needed to use the `copy` command. (apt-get install xsel)
* [Chafa](https://hpjansson.org/chafa/) allows to display pictures in your console. Install it and browse to an HTML page with picture to see the magic.
## Features

View File

@ -501,11 +501,16 @@ class HtmlRenderer():
rendered_body += "\x1b[22m"
elif element.name == "a":
text = sanitize_string(element.get_text())
# support for images nested in links
for child in element.children:
if child.name == "img":
img = recursive_render(child)
rendered_body += img
link = element.get('href')
if link:
links.append(link+" "+text)
link_id = " [%s]"%(len(links))
rendered_body = "\x1b[2;34m" + text + link_id + "\x1b[0m"
rendered_body += "\x1b[2;34m" + text + link_id + "\x1b[0m"
else:
#No real link found
rendered_body = text
@ -516,9 +521,11 @@ class HtmlRenderer():
if shutil.which('chafa'):
abs_url = urllib.parse.urljoin(self.url, src)
g = GeminiItem(abs_url)
img = g.get_cache_path()
return_code = subprocess.run("chafa --bg white -s 40 %s"%img, shell=True, capture_output=True)
ansi_img = return_code.stdout.decode() + "\n"
if g.is_cache_valid():
img = g.get_cache_path()
return_code = subprocess.run("chafa --bg white -s 40 %s"%img, \
shell=True, capture_output=True)
ansi_img = return_code.stdout.decode()
alt = element.get("alt")
if alt:
alt = sanitize_string(alt)
@ -528,7 +535,7 @@ class HtmlRenderer():
if src:
links.append(src+" "+text)
link_id = " [%s]"%(len(links))
rendered_body = ansi_img + "\n\x1b[2;33m" + text + link_id + "\x1b[0m\n"
rendered_body = ansi_img + "\x1b[2;33m" + text + link_id + "\x1b[0m\n\n"
elif element.name == "br":
rendered_body = "\n"
elif element.string: