~ubuntu-branches/ubuntu/karmic/calibre/karmic

« back to all changes in this revision

Viewing changes to src/calibre/ebooks/html/__init__.py

  • Committer: Bazaar Package Importer
  • Author(s): Martin Pitt
  • Date: 2009-07-30 12:49:41 UTC
  • mto: This revision was merged to the branch mainline in revision 13.
  • Revision ID: james.westby@ubuntu.com-20090730124941-kviipg9ypwgppulc
Tags: upstream-0.6.3+dfsg
ImportĀ upstreamĀ versionĀ 0.6.3+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
# vim:fileencoding=UTF-8:ts=4:sw=4:sta:et:sts=4:ai
 
3
from __future__ import with_statement
 
4
 
 
5
__license__   = 'GPL v3'
 
6
__copyright__ = '2009, Kovid Goyal <kovid@kovidgoyal.net>'
 
7
__docformat__ = 'restructuredtext en'
 
8
 
 
9
import re
 
10
 
 
11
from lxml.etree import tostring as _tostring
 
12
 
 
13
def tostring(root, strip_comments=False, pretty_print=False):
 
14
    '''
 
15
    Serialize processed XHTML.
 
16
    '''
 
17
    root.set('xmlns', 'http://www.w3.org/1999/xhtml')
 
18
    root.set('{http://www.w3.org/1999/xhtml}xlink', 'http://www.w3.org/1999/xlink')
 
19
    for x in root.iter():
 
20
        if hasattr(x.tag, 'rpartition') and x.tag.rpartition('}')[-1].lower() == 'svg':
 
21
            x.set('xmlns', 'http://www.w3.org/2000/svg')
 
22
 
 
23
    ans = _tostring(root, encoding='utf-8', pretty_print=pretty_print)
 
24
    if strip_comments:
 
25
        ans = re.compile(r'<!--.*?-->', re.DOTALL).sub('', ans)
 
26
    ans = '<?xml version="1.0" encoding="utf-8" ?>\n'+ans
 
27
 
 
28
    return ans
 
29
 
 
30