SF.net SVN: docutils:[9854 ] trunk/docutils/docutils/w riters/_html_base.py

milde--- via Docutils-checkins <[email protected]>
Newsgroups gmane.text.docutils.cvs
Message-ID <[email protected]>
Revision: 9854
          http://sourceforge.net/p/docutils/code/9854
Author:   milde
Date:     2024-08-06 08:02:06 +0000 (Tue, 06 Aug 2024)
Log Message:
-----------
Read to-be-embedded images as text, if the MIME type is SVG.

Provides decoding and universal line endings for free.

(SVG images are directly embedded while others are base64-encoded.)

Modified Paths:
--------------
    trunk/docutils/docutils/writers/_html_base.py

Modified: trunk/docutils/docutils/writers/_html_base.py
===================================================================
--- trunk/docutils/docutils/writers/_html_base.py	2024-08-06 08:01:54 UTC (rev 9853)
+++ trunk/docutils/docutils/writers/_html_base.py	2024-08-06 08:02:06 UTC (rev 9854)
@@ -461,8 +461,7 @@
         # Use ElementTree to add node attributes.
         # ET also removes comments and preamble code.
         #
-        # Provisional:
-        # interface and behaviour may change without notice.
+        # Internal: interface and behaviour may change without notice.
 
         # SVG namespace
         svg_ns = {'': 'http://www.w3.org/2000/svg',
@@ -471,15 +470,12 @@
         ET.register_namespace('', svg_ns[''])
         ET.register_namespace('xlink', svg_ns['xlink'])
         try:
-            svg = ET.fromstring(imagedata.decode('utf-8'))
+            svg = ET.fromstring(imagedata)
         except ET.ParseError as err:
             self.messages.append(self.document.reporter.error(
                 f'Cannot parse SVG image "{node["uri"]}":\n  {err}',
                 base_node=node))
-            # We initially open the file in binary mode,
-            # meaning universal newlines are not applied.
-            # Manually convert here before decoding.
-            return imagedata.replace(b'\r\n', b'\n').decode('utf-8')
+            return imagedata
         # apply image node attributes:
         if size_declaration:  # append to style, replacing width & height
             declarations = [d.strip() for d in svg.get('style', '').split(';')]
@@ -1169,8 +1165,11 @@
         elif loading == 'embed':
             try:
                 imagepath = self.uri2imagepath(uri)
-                imagedata = Path(imagepath).read_bytes()
-            except (ValueError, OSError) as err:
+                if mimetype == 'image/svg+xml':
+                    imagedata = Path(imagepath).read_text()
+                else:
+                    imagedata = Path(imagepath).read_bytes()
+            except (ValueError, OSError, UnicodeError) as err:
                 self.messages.append(self.document.reporter.error(
                     f'Cannot embed image "{uri}":\n  {err}', base_node=node))
                 # TODO: get external files with urllib.request (cf. odtwriter)?

This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.