def __init__(self, encoding='utf-8', css=None, term=None):
self._control_parser = ControlParser(encoding=encoding, term=term)
self._encoding = encoding
+ self._doctype = '<!DOCTYPE html>'
if css is None:
css = self._get_css()
self.css = css
html = _etree.Element('html')
head = _etree.Element('head')
html.append(head)
+ charset = _etree.Element('meta', charset=self._encoding.upper())
+ head.append(charset)
style = _etree.Element('style', type='text/css')
head.append(style)
style.text = self.css
body.append(pre)
tree = _etree.ElementTree(element=html)
html.text = html.tail = head.text = head.tail = '\n'
+ charset.tail = '\n'
body.text = body.tail = pre.tail = '\n'
return tree
return text
def _serialize(self, tree):
- return _etree.tostring(tree.getroot(), 'unicode')
+ return '\n'.join([
+ self._doctype,
+ _etree.tostring(tree.getroot(), encoding='unicode'),
+ ])
def _write_etree(self, tree, filename=None):
if filename:
_LOG.info('write {}'.format(filename))
- tree.write(filename, encoding=self._encoding)
+ with open(filename, 'wb') as f:
+ f.write(self._doctype.encode(self._encoding))
+ f.write('\n'.encode(self._encoding))
+ tree.write(f, encoding=self._encoding)
else:
_LOG.info('write to stdout')
result = self._serialize(tree)