SF.net SVN: docutils:[9953] trunk/docutils

milde--- via Docutils-checkins <[email protected]>
Newsgroups gmane.text.docutils.cvs
Message-ID <[email protected]>
Revision: 9953
          http://sourceforge.net/p/docutils/code/9953
Author:   milde
Date:     2024-10-20 17:44:36 +0000 (Sun, 20 Oct 2024)
Log Message:
-----------
ODT writer: Fix handling of `*.xml` stylesheets.

According to the ODT writer documentation, the "stylesheet" setting
should work with either "styles.odt" or "styles.xml" files.

Fix errors with "*.xml" style files [bugs:#494].
Thanks to Paul Kishimoto for report and analysis.

Modified Paths:
--------------
    trunk/docutils/HISTORY.rst
    trunk/docutils/docs/user/odt.rst
    trunk/docutils/docutils/writers/odf_odt/__init__.py

Modified: trunk/docutils/HISTORY.rst
===================================================================
--- trunk/docutils/HISTORY.rst	2024-10-20 10:48:13 UTC (rev 9952)
+++ trunk/docutils/HISTORY.rst	2024-10-20 17:44:36 UTC (rev 9953)
@@ -204,6 +204,7 @@
   - Fix conversion factor of "pc" (pica) to "cm".
   - Fix conversion of image width in "%" if the height is specified.
   - Adjust fallback DPI value (currently not used) to match CSS units.
+  - Fix errors with "*.xml" style files (bug #494).
 
 * pyproject.toml
 

Modified: trunk/docutils/docs/user/odt.rst
===================================================================
--- trunk/docutils/docs/user/odt.rst	2024-10-20 10:48:13 UTC (rev 9952)
+++ trunk/docutils/docs/user/odt.rst	2024-10-20 17:44:36 UTC (rev 9953)
@@ -157,7 +157,9 @@
 
 Note that with the ``--stylesheet`` command line option, you can
 use either ``styles.odt`` or ``styles.xml``, as described below.
-Use of ``styles.odt`` is recommended over ``styles.xml``.
+Use of ``styles.odt`` is recommended over ``styles.xml`` as, e.g.,
+customizing `table styles`_ does not work with an ``*.xml``
+stylesheet file.
 
 You can modify the look of documents generated by ``odtwriter`` in
 several ways:

Modified: trunk/docutils/docutils/writers/odf_odt/__init__.py
===================================================================
--- trunk/docutils/docutils/writers/odf_odt/__init__.py	2024-10-20 10:48:13 UTC (rev 9952)
+++ trunk/docutils/docutils/writers/odf_odt/__init__.py	2024-10-20 17:44:36 UTC (rev 9953)
@@ -673,8 +673,10 @@
         """Copy images, settings, etc from the stylesheet doc into target doc.
         """
         stylespath = self.settings.stylesheet
+        if not stylespath.endswith('.odt'):
+            return  # an '.xml' stylesheet does not have settings or images
         inzipfile = zipfile.ZipFile(stylespath, 'r')
-        # Copy the styles.
+        # Copy the settings.
         s1 = inzipfile.read('settings.xml')
         self.write_zip_str(outzipfile, 'settings.xml', s1)
         # Copy the images.
@@ -923,14 +925,15 @@
         return self.str_stylesheet
 
     def retrieve_styles(self, extension):
-        """Retrieve the stylesheet from either a .xml file or from
-        a .odt (zip) file.  Return the content as a string.
+        """Retrieve the stylesheet from a .xml or .odt (zip) file.
+
+        Store in `self.*_styles*` attributes.
         """
-        s2 = None
         stylespath = self.settings.stylesheet
         ext = os.path.splitext(stylespath)[1]
         if ext == '.xml':
             s1 = Path(stylespath).read_text(encoding='utf-8')
+            s2 = ''
         elif ext == extension:
             zfile = zipfile.ZipFile(stylespath, 'r')
             s1 = zfile.read('styles.xml')
@@ -942,6 +945,9 @@
         self.str_stylesheet = s1
         self.str_stylesheetcontent = s2
         self.dom_stylesheet = etree.fromstring(self.str_stylesheet)
+        if not s2:
+            return
+        # TODO: dom_stylesheetcontent is never used. Remove?
         self.dom_stylesheetcontent = etree.fromstring(
             self.str_stylesheetcontent)
         self.table_styles = self.extract_table_styles(s2)

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.