fix roundtrip with image files (#3934)

This commit is contained in:
Frédéric Péters 2013-11-04 19:29:30 +01:00
parent 323787a66e
commit 8e826655f2
2 changed files with 36 additions and 6 deletions

View File

@ -39,18 +39,23 @@ def get_as_cm(value):
return qty * 0.03514598035
def convert(input, output):
extra_files = []
try:
z = zipfile.ZipFile(input)
except zipfile.BadZipfile:
content = file(input).read()
else:
content_zfile = None
for zfile in z.namelist():
if zfile == 'contents.xml':
break
else:
print >> sys.stderr, 'Missing contents.xml, is this a legi file?'
content_zfile = zfile
else:
# extra files
extra_files.append((zfile, z.read(zfile)))
if content_zfile is None:
# missing contents.xml
sys.exit(1)
content = z.read(zfile)
content = z.read(content_zfile)
styledoc = libxml2.parseFile(os.path.join(base_dir, 'legi2odf.xsl'))
style = libxslt.parseStylesheetDoc(styledoc)
@ -119,13 +124,17 @@ def convert(input, output):
'Configurations2/menubar/menubar.xml')
z.writestr('content.xml', data)
z.writestr('meta.xml', meta)
for filename, filecontent in extra_files:
z.writestr('Pictures/%s' % filename, filecontent)
extra_manifest_entries = '\n'.join([' <manifest:file-entry manifest:media-type="" manifest:full-path="Pictures/%s"/>\n' % x[0] for x in extra_files])
z.writestr('META-INF/manifest.xml', '''<?xml version="1.0" encoding="UTF-8"?>
<manifest:manifest xmlns:manifest="urn:oasis:names:tc:opendocument:xmlns:manifest:1.0">
<manifest:file-entry manifest:media-type="application/vnd.oasis.opendocument.text" manifest:full-path="/"/>
<manifest:file-entry manifest:media-type="text/xml" manifest:full-path="content.xml"/>
<manifest:file-entry manifest:media-type="text/xml" manifest:full-path="styles.xml"/>
<manifest:file-entry manifest:media-type="" manifest:full-path="Configurations2/menubar/menubar.xml"/>
</manifest:manifest>''')
%s
</manifest:manifest>''' % extra_manifest_entries)
z.close()

View File

@ -5,6 +5,9 @@
xmlns:style="urn:oasis:names:tc:opendocument:xmlns:style:1.0"
xmlns:fo="urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0"
xmlns:table="urn:oasis:names:tc:opendocument:xmlns:table:1.0"
xmlns:draw="urn:oasis:names:tc:opendocument:xmlns:drawing:1.0"
xmlns:svg="urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0"
xmlns:xlink="http://www.w3.org/1999/xlink"
xmlns:tabellio="urn:tabellio"
>
@ -292,7 +295,25 @@
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
<xsl:template match="mediaobject" mode="inline">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="imageobject">
<draw:frame text:anchor-type="paragraph">
<xsl:attribute name="svg:width">
<xsl:value-of select="imagedata/@width"/>
</xsl:attribute>
<xsl:attribute name="svg:height">
<xsl:value-of select="imagedata/@depth"/>
</xsl:attribute>
<draw:image xlink:type="simple" xlink:show="embed" xlink:actuate="onLoad">
<xsl:attribute name="xlink:href">Pictures/<xsl:value-of select="imagedata/@fileref"/></xsl:attribute>
</draw:image>
</draw:frame>
</xsl:template>
<xsl:template match="entry">
<xsl:element name="table:table-cell">
<xsl:if test="ancestor-or-self::thead">