|
From: <mi...@us...> - 2023-11-25 21:09:09
|
Revision: 9485
http://sourceforge.net/p/docutils/code/9485
Author: milde
Date: 2023-11-25 21:09:05 +0000 (Sat, 25 Nov 2023)
Log Message:
-----------
Support `<video>` in "_html_base", use "style" attribute for size.
Move the handling of "moving images" from "html5_polyglot" to
"_html_base". Now all HTML writers support insertion of video content.
The :width: and :height: image directive options are passed to HTML
via the "style" attribute (supports length units).
Modified Paths:
--------------
trunk/docutils/HISTORY.txt
trunk/docutils/docutils/writers/_html_base.py
trunk/docutils/docutils/writers/html5_polyglot/__init__.py
trunk/docutils/test/functional/expected/misc_rst_html5.html
Modified: trunk/docutils/HISTORY.txt
===================================================================
--- trunk/docutils/HISTORY.txt 2023-11-25 21:08:53 UTC (rev 9484)
+++ trunk/docutils/HISTORY.txt 2023-11-25 21:09:05 UTC (rev 9485)
@@ -81,6 +81,11 @@
- Support video inclusion via `<object>` tags.
+* docutils/writers/_html_base.py
+
+ - Support for video inclusion via `<video>` tags
+ (moved here from writers/html5_polyglot/__init__.py).
+
* docutils/writers/latex2e/__init__.py
- Fix placement of hyperlink target (label) for tables (bug #440).
Modified: trunk/docutils/docutils/writers/_html_base.py
===================================================================
--- trunk/docutils/docutils/writers/_html_base.py 2023-11-25 21:08:53 UTC (rev 9484)
+++ trunk/docutils/docutils/writers/_html_base.py 2023-11-25 21:09:05 UTC (rev 9485)
@@ -1005,6 +1005,9 @@
self.header.extend(header)
del self.body[start:]
+ # MIME types supported by the HTML5 <video> element
+ videotypes = ('video/mp4', 'video/webm', 'video/ogg')
+
def visit_image(self, node):
atts = {}
uri = node['uri']
@@ -1011,7 +1014,6 @@
uri_parts = urllib.parse.urlparse(uri)
imagepath = ''
mimetype = mimetypes.guess_type(uri)[0]
- scaling_problems = []
# image size
if 'width' in node:
atts['width'] = node['width']
@@ -1020,10 +1022,13 @@
if 'scale' in node:
if 'width' not in node or 'height' not in node:
# try reading size from image file
+ scaling_problems = []
if uri_parts.scheme not in ('', 'file'):
- scaling_problems.append('Works only for local images.')
+ scaling_problems.append('Can only read local images.')
if not PIL:
scaling_problems.append('Requires Python Imaging Library.')
+ if mimetype in self.videotypes:
+ scaling_problems.append('PIL cannot read video images.')
if not self.settings.file_insertion_enabled:
scaling_problems.append('Reading external files disabled.')
if not scaling_problems:
@@ -1037,9 +1042,10 @@
self.settings.record_dependencies.add(
imagepath.replace('\\', '/'))
if scaling_problems:
- self.document.reporter.warning(
- '\n '.join([f'Cannot scale "{imagepath or uri}".']
- + scaling_problems))
+ msg = ['Cannot scale image!',
+ f'Could not get size from "{imagepath or uri}":',
+ *scaling_problems]
+ self.document.reporter.warning('\n '.join(msg))
else:
if 'width' not in atts:
atts['width'] = '%dpx' % imgsize[0]
@@ -1056,8 +1062,8 @@
style = []
for att_name in 'width', 'height':
if att_name in atts:
+ # Interpret unitless values as pixels:
if re.match(r'^[0-9.]+$', atts[att_name]):
- # Interpret unitless values as pixels.
atts[att_name] += 'px'
style.append('%s: %s;' % (att_name, atts[att_name]))
del atts[att_name]
@@ -1070,8 +1076,22 @@
suffix = ''
else:
suffix = '\n'
+
if 'align' in node:
atts['class'] = 'align-%s' % node['align']
+
+ # moving image -> use <video>
+ if mimetype in self.videotypes:
+ fallback = node.get('alt', uri)
+ atts['title'] = fallback
+ if 'controls' in node['classes']:
+ node['classes'].remove('controls')
+ atts['controls'] = 'controls'
+ self.body.append(
+ self.starttag(node, "video", suffix, src=uri, **atts)
+ + f'<a href="{uri}">{fallback}</a>{suffix}</video>{suffix}')
+ return
+
# Embed image file (embedded SVG or data URI):
if self.image_loading == 'embed':
if uri_parts.scheme not in ('', 'file'):
Modified: trunk/docutils/docutils/writers/html5_polyglot/__init__.py
===================================================================
--- trunk/docutils/docutils/writers/html5_polyglot/__init__.py 2023-11-25 21:08:53 UTC (rev 9484)
+++ trunk/docutils/docutils/writers/html5_polyglot/__init__.py 2023-11-25 21:09:05 UTC (rev 9485)
@@ -26,7 +26,6 @@
"""
__docformat__ = 'reStructuredText'
-import mimetypes
from pathlib import Path
from docutils import frontend, nodes
@@ -238,43 +237,6 @@
self.header.extend(header)
del self.body[start:]
- # MIME types supported by the HTML5 <video> element
- videotypes = ('video/mp4', 'video/webm', 'video/ogg')
-
- def visit_image(self, node):
- atts = {}
- uri = node['uri']
- mimetype = mimetypes.guess_type(uri)[0]
- if mimetype not in self.videotypes:
- return super().visit_image(node)
- # image size
- if 'width' in node:
- atts['width'] = node['width'].replace('px', '')
- if 'height' in node:
- atts['height'] = node['height'].replace('px', '')
- if 'align' in node:
- atts['class'] = f"align-{node['align']}"
- if 'controls' in node['classes']:
- atts['controls'] = 'controls'
- node['classes'].remove('controls')
- atts['title'] = node.get('alt', uri)
- if getattr(self.settings, 'image_loading', None) == 'lazy':
- atts['loading'] = 'lazy'
- # No newline in inline context or if surrounded by <a>...</a>.
- if (isinstance(node.parent, nodes.TextElement)
- or (isinstance(node.parent, nodes.reference)
- and not isinstance(node.parent.parent, nodes.TextElement))):
- suffix = ''
- else:
- suffix = '\n'
- fallback = node.get('alt', uri)
- self.body.append(
- self.starttag(node, "video", suffix, src=uri, **atts)
- + f'<a href="{uri}">{fallback}</a>{suffix}</video>{suffix}')
-
- def depart_image(self, node):
- pass
-
# use HTML text-level tags if matching class value found
supported_inline_tags = {'code', 'kbd', 'dfn', 'samp', 'var',
'bdi', 'del', 'ins', 'mark', 'small',
Modified: trunk/docutils/test/functional/expected/misc_rst_html5.html
===================================================================
--- trunk/docutils/test/functional/expected/misc_rst_html5.html 2023-11-25 21:08:53 UTC (rev 9484)
+++ trunk/docutils/test/functional/expected/misc_rst_html5.html 2023-11-25 21:09:05 UTC (rev 9485)
@@ -136,7 +136,7 @@
loads. According to the <a class="reference external" href="https://developer.mozilla.org/en-US/docs/Web/HTML/Element/video">HTML5 spec</a>, hight and width must be
specified as pixel values.</p>
<figure class="align-center">
-<video controls="controls" src="../../../docs/user/rst/images/pens.mp4" title="test video in a figure" width="200">
+<video controls="controls" src="../../../docs/user/rst/images/pens.mp4" style="width: 200px;" title="test video in a figure">
<a href="../../../docs/user/rst/images/pens.mp4">test video in a figure</a>
</video>
<figcaption>
@@ -143,7 +143,7 @@
<p>Simple test video in a centered figure</p>
</figcaption>
</figure>
-<p>A video like this <video src="../../../docs/user/rst/images/pens.mp4" title="rotating pens video" width="60"><a href="../../../docs/user/rst/images/pens.mp4">rotating pens video</a></video> can be included inline via substitution.</p>
+<p>A video like this <video src="../../../docs/user/rst/images/pens.mp4" style="width: 60px;" title="rotating pens video"><a href="../../../docs/user/rst/images/pens.mp4">rotating pens video</a></video> can be included inline via substitution.</p>
</section>
</main>
</body>
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|