~lioploum/offpunk-devel

Added initial support for html <video> elements, including for their poster tag. v1 PROPOSED

Bert Livens: 2
 Added initial support for html <video> elements, including for their poster tag.
 Added initial support for html <video> elements, including for their poster tag.

 3 files changed, 82 insertions(+), 0 deletions(-)
This is not a bug. These are links to videos from a <video>-tag without 
a thumbnail or when the linked thumbnail-file doesn't exists. In cases 
like this, it doesn't show an image (as it doesn't know one) but does 
show a link the the video. In this case they are all to the same video 
file. Maybe I should have added different alt-texts to make it clearer.
If you look at the HTML, there is exactly one [VIDEO] in the output of 
ansicat for every <video> tag. If possible, it shows an image and the 
alt-text and links to the video-file (the last one doesn't because the 
<video>-tag doesn't contain a src-element).

Bert
Next
Ok, perfect. Let’s see how it goes in the wild now. Thanks a lot for 
your contributon!

(nice flute)
Next
Export patchset (mbox)
How do I use this?

Copy & paste the following snippet into your terminal to import this patchset into git:

curl -s https://lists.sr.ht/~lioploum/offpunk-devel/patches/47867/mbox | git am -3
Learn more about email & git

[PATCH] Added initial support for html <video> elements, including for their poster tag. Export this patch

Signed-off-by: Bert Livens <bert@bertlivens.be>
---
This shoud add support for video-tags in html, showing the “poster”
attribute as an image if available and link to the video file.
Tested on a few websites and local files. I don't know if posters (the
thumbnail) and the video get correctly marked for download. For me,
images don't seem to get downloaded on any website unless I explicitly
open them. For now it uses the image mode and image theme. In one way,
this is right because all it does is render and image but it would be
cleaner to create a video mode. We could also, if a poster is missing,
try to look at the video file if it has a thumbnail or even take the
first frame.
 ansicat.py | 35 +++++++++++++++++++++++++++++++++++
 1 file changed, 35 insertions(+)
diff --git a/ansicat.py b/ansicat.py
index a4496dc..06742f0 100755
--- a/ansicat.py
+++ b/ansicat.py
@@ -1196,6 +1196,41 @@ class HtmlRenderer(AbstractRenderer):
                        r.add_text(text + link_id)
                        r.close_theme("image_link")
                        r.newline()

            elif element.name == "video":
                poster = element.get("poster")
                src = element.get("src")
                for child in element.children:
                    if not src:
                        if child.name == "source":
                            src = child.get("src")
                text = ""
                if poster:
                    ansi_img = render_image(poster,width=width,mode=mode)
                alt = element.get("alt")
                if alt:
                    alt = sanitize_string(alt)
                    text += "[VIDEO] %s"%alt
                else:
                    text += "[VIDEO]"
                if src:
                    if not mode in self.images:
                        self.images[mode] = []
                    abs_url,data = looks_like_base64(src,self.url)
                    poster_url,data_poster = looks_like_base64(poster,self.url)
                    if poster_url:
                        self.images[mode].append(poster)
                    if abs_url:
                        links.append(abs_url+" "+text)
                        self.images[mode].append(abs_url)
                        link_id = " [%s]"%(len(links)+startlinks)
                        r.add_block(ansi_img)
                        r.open_theme("image_link")
                        r.center_line()
                        r.add_text(text + link_id)
                        r.close_theme("image_link")
                        r.newline()

            elif element.name == "br":
                r.newline()
            elif element.name not in ["script","style","template"] and type(element) != Comment:
-- 
2.43.0

[PATCH v2] Added initial support for html <video> elements, including for their poster tag. Export this patch

Signed-off-by: Bert Livens <bert@bertlivens.be>
---
I tested it on almost all possible combinations, I put some on
https://klaank.be/test.html and also tested on an invidious-video.
It seems to work.
A video mode wouldn't do anything different as far as I can imagine
but it just seems more semantic. Maybe we should change it to "media"
and also support included audio files.
(I hope this gets send as a reply, I never tried sending one with
git send-email)


 CHANGELOG  |  1 +
 ansicat.py | 46 ++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 47 insertions(+)

diff --git a/CHANGELOG b/CHANGELOG
index 34e2f97..75abe86 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -4,6 +4,7 @@
## 2.2 - Unpublished

- netcache : solve an infinite gemini loop with code 6X (see also bug #31)
- ansicat: added support for <video> HTML-element

## 2.1 - December 15th 2023
- freshly updated gemtext/rss links are highlighted ("new_link" theme option)
diff --git a/ansicat.py b/ansicat.py
index a4496dc..4150525 100755
--- a/ansicat.py
+++ b/ansicat.py
@@ -1196,6 +1196,52 @@ class HtmlRenderer(AbstractRenderer):
                        r.add_text(text + link_id)
                        r.close_theme("image_link")
                        r.newline()

            elif element.name == "video":
                poster = element.get("poster")
                src = element.get("src")
                for child in element.children:
                    if not src:
                        if child.name == "source":
                            src = child.get("src")
                text = ""
                if poster:
                    ansi_img = render_image(poster,width=width,mode=mode)
                alt = element.get("alt")
                if alt:
                    alt = sanitize_string(alt)
                    text += "[VIDEO] %s"%alt
                else:
                    text += "[VIDEO]"

                if poster:
                    if not mode in self.images:
                        self.images[mode] = []
                    poster_url,d = looks_like_base64(poster,self.url)
                    if poster_url:
                        vid_url,d2 = looks_like_base64(src,self.url)
                        self.images[mode].append(poster_url)
                        r.add_block(ansi_img)
                        r.open_theme("image_link")
                        r.center_line()
                        if vid_url and src:
                            links.append(vid_url+" "+text)
                            link_id = " [%s]"%(len(links)+startlinks)
                            r.add_text(text + link_id)
                        else:
                            r.add_text(text)
                        r.close_theme("image_link")
                        r.newline()
                elif src:
                    vid_url,d = looks_like_base64(src,self.url)
                    links.append(vid_url+" "+text)
                    link_id = " [%s]"%(len(links)+startlinks)
                    r.open_theme("image_link")
                    r.center_line()
                    r.add_text(text + link_id)
                    r.close_theme("image_link")
                    r.newline()

            elif element.name == "br":
                r.newline()
            elif element.name not in ["script","style","template"] and type(element) != Comment:
-- 
2.43.0