From f0177f62f8562c895b608a8cb6a6adc479f66b45 Mon Sep 17 00:00:00 2001 From: Lionel Dricot Date: Thu, 10 Feb 2022 17:19:20 +0100 Subject: [PATCH] support for images nested in links --- CHANGELOG | 2 +- README.md | 1 + offpunk.py | 17 ++++++++++++----- 3 files changed, 14 insertions(+), 6 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 65d0d47..346d3d4 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -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 diff --git a/README.md b/README.md index e8a73fb..0e341e5 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/offpunk.py b/offpunk.py index fc8a7f4..0df19f5 100755 --- a/offpunk.py +++ b/offpunk.py @@ -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: