Commit 8fa68dca authored by Nicolas Delaby's avatar Nicolas Delaby

Recover broken HTML documents, specially regarding encoding used. reviewed by Kazuhiko

git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@31627 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent 4c3154f9
# -*- coding: utf-8 -*-
from Products.PortalTransforms.interfaces import itransform from Products.PortalTransforms.interfaces import itransform
from zope.interface import implements from zope.interface import implements
from oood_commandtransform import OOOdCommandTransform, OOoDocumentDataStream from oood_commandtransform import OOOdCommandTransform, OOoDocumentDataStream
from zLOG import LOG from zLOG import LOG
from lxml import etree, html
from lxml.etree import Element, SubElement
html_parser = etree.HTMLParser(remove_blank_text=True, encoding='utf-8')
class HTMLToOdt: class HTMLToOdt:
"""Transforms HTML to odt by using oood""" """Transforms HTML to odt by using oood"""
...@@ -25,6 +30,19 @@ class HTMLToOdt: ...@@ -25,6 +30,19 @@ class HTMLToOdt:
raise AttributeError(attr) raise AttributeError(attr)
def convert(self, orig, data, cache=None, filename=None, context=None, **kwargs): def convert(self, orig, data, cache=None, filename=None, context=None, **kwargs):
# Try to recover broken HTML documents, specially regarding encoding used
html_node = etree.XML(orig, parser=html_parser)
html_tree = html_node.getroottree()
head = html_tree.find('head')
if head is None:
# This part of code is supposed to be useless
# lxml.html.tostring function with include_meta_content_type
# parameter to True, should do the same things. But it does not.
head = Element('head')
html_node.insert(0, head)
SubElement(head, 'meta', **{'http-equiv': 'Content-Type', 'content': 'text/html; charset=utf-8'})
orig = html.tostring(html_tree, encoding='utf-8')
doc = OOOdCommandTransform(context, filename, orig, self.inputs[0]) doc = OOOdCommandTransform(context, filename, orig, self.inputs[0])
doc.convert() doc.convert()
odt = doc.convertTo('odt') odt = doc.convertTo('odt')
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment