Re: <img> instead of <video> tag inserted for image:: foo.mp4

Agathe Porte <[email protected]> Tue, 3 May 2022 09:14:59 +0200
Newsgroups gmane.text.docutils.user
Message-ID <[email protected]>
Hi,

29/04/2022 10:54, Guenter MildeĀ :
>> However, according to the documentation [1], this should export a
>> <video> tag instead of a <img> tag given that the file extension is ".mp4".
> It seems that Pelican (at least with your configuration) uses the
> traditional HTML4 writer for HTML export.
>
> Only the `HTML5 writer`_ supports video. Finding out how/whether Pelican
> can be configured to use the HTML5 writer is left as an exercise for the
> reader.
>
I was able to configure pelican to use html5_polyglot using 
rst_with_html5 Pelican plugin [1]. As I dig further and further into 
html5_polyglot, I am sad to see that <video> has no first class support 
in reStructuredText. I did a local patch to add more attributes to the 
renderer, then another patch for allowing alternative source format, but 
I think that a new video class should be added instead of hacking the 
image class so deeply.

This has been done in a plugin for sphinx [2]: full support of a 
distinct video node that can support extra attributes. One quirk of 
using "classes" for detecting {loop,controls,autoplay} is that the video 
keeps the "loop" CSS style class, which seems not useful and can 
conflict with CSS classes.

I was not able to see prior discussion for proper video support in 
docutils using a search engine. Please enlighten my light if anything 
seems wrong with this proposal. I can volonteer to implement this 
feature and document it for docutils. I think it would be better than 
the current two alternatives: using out-of-tree plugins or hacking image 
nodes.

Bests,

Agata

[1] https://github.com/bekcpear/pelican-rst_with_html5

[2] 
https://github.com/sphinx-contrib/video/blob/master/sphinxcontrib/video.py
0002-feat-html5_polyglot-allow-alt_sources-for-video.patch (text/x-patch, 2.1 KB)
From 2532bc971163fcabe2e743e32fdcdd7ff017156e Mon Sep 17 00:00:00 2001
From: Agathe Porte <[email protected]>
Date: Tue, 3 May 2022 09:10:30 +0200
Subject: [PATCH 2/2] feat(html5_polyglot): allow alt_sources for video

---
 docutils/writers/html5_polyglot/__init__.py | 17 ++++++++++++++---
 1 file changed, 14 insertions(+), 3 deletions(-)

diff --git a/docutils/writers/html5_polyglot/__init__.py b/docutils/writers/html5_polyglot/__init__.py
index d72fe37..bf49417 100644
--- a/docutils/writers/html5_polyglot/__init__.py
+++ b/docutils/writers/html5_polyglot/__init__.py
@@ -275,6 +275,7 @@ class HTMLTranslator(_html_base.HTMLTranslator):
         mimetype = mimetypes.guess_type(uri)[0]
         if mimetype not in self.videotypes:
             return super().visit_image(node)
+        sources = [(uri, mimetype)]
         # image size
         if 'width' in node:
             atts['width'] = node['width'].replace('px', '')
@@ -288,6 +289,11 @@ class HTMLTranslator(_html_base.HTMLTranslator):
             atts['autoplay'] = 'autoplay'
         if 'loop' in node['classes']:
             atts['loop'] = 'loop'
+        if 'alt_src' in node:
+            for s_uri in node['alt_src']:
+                s_mimetype = mimetypes.guess_type(s_ui)[0]
+                assert s_mimetype in videotypes
+                sources.append((s_uri, s_mimetype))
         atts['title'] = node.get('alt', uri)
         if getattr(self.settings, 'image_loading', None) == 'lazy':
             atts['loading'] = 'lazy'
@@ -298,10 +304,15 @@ class HTMLTranslator(_html_base.HTMLTranslator):
             suffix = ''
         else:
             suffix = '\n'
+
+        sources = suffix.join(
+            ['<source src="%s" type="%s"/>' % s for s in sources])
+
         self.body.append(
-            '%s<a href="%s">%s</a>%s</video>%s'
-            % (self.starttag(node, 'video', suffix, src=uri, **atts),
-               uri,
+            '%s%s%s%s%s</video>%s'
+            % (self.starttag(node, 'video', suffix, **atts),
+               sources,
+               suffix,
                node.get('alt', uri),
                suffix,
                suffix))
-- 
2.36.0
0001-feat-html5_polyglot-support-autoplay-loop-for-video.patch (text/x-patch, 1.1 KB)
From 027b5dc56eb46fa8bca006b52ad75cd88db2b1cf Mon Sep 17 00:00:00 2001
From: Agathe Porte <[email protected]>
Date: Tue, 3 May 2022 08:35:44 +0200
Subject: [PATCH 1/2] feat(html5_polyglot): support {autoplay,loop} for video

---
 docutils/writers/html5_polyglot/__init__.py | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/docutils/writers/html5_polyglot/__init__.py b/docutils/writers/html5_polyglot/__init__.py
index 8a632f9..d72fe37 100644
--- a/docutils/writers/html5_polyglot/__init__.py
+++ b/docutils/writers/html5_polyglot/__init__.py
@@ -284,6 +284,10 @@ class HTMLTranslator(_html_base.HTMLTranslator):
             atts['class'] = 'align-%s' % node['align']
         if 'controls' in node['classes']:
             atts['controls'] = 'controls'
+        if 'autoplay' in node['classes']:
+            atts['autoplay'] = 'autoplay'
+        if 'loop' in node['classes']:
+            atts['loop'] = 'loop'
         atts['title'] = node.get('alt', uri)
         if getattr(self.settings, 'image_loading', None) == 'lazy':
             atts['loading'] = 'lazy'
-- 
2.36.0