3 """tests for epub.py"""
7 sys
.path
.extend(('.', '..'))
11 from pprint
import pprint
, pformat
12 from objavi
import epub
14 from lxml
.etree
import Element
18 def _xhtml_parse(*args
, **kwargs
):
19 kwargs
['parser'] = lxml
.html
.XHTMLParser(encoding
="utf-8")
20 return lxml
.html
.parse(*args
, **kwargs
)
22 def _html_parse(*args
, **kwargs
):
23 kwargs
['parser'] = lxml
.etree
.HTMLParser(encoding
="utf-8")
24 return lxml
.html
.parse(*args
, **kwargs
)
26 def _find_tag(doc
, tag
):
28 return doc
.iter(epub
.XHTMLNS
+ tag
).next()
30 return doc
.iter(tag
).next()
32 def add_guts(src
, dest
):
33 """Append the contents of the <body> of one tree onto that of
34 another. The source tree will be emptied."""
35 #print lxml.etree.tostring(src)
36 sbody
= _find_tag(src
, 'body')
37 dbody
= _find_tag(dest
, 'body')
39 dbody
[-1].tail
= ((dbody
[-1].tail
or '') +
40 (sbody
.text
or '')) or None
45 dbody
.tail
= ((dbody
.tail
or '') +
46 (sbody
.tail
or '')) or None
48 def add_marker(doc
, ID
, title
=None, klass
="espri-marker"):
49 marker
= lxml
.etree
.Element('hr')
51 marker
.set('class', klass
)
53 marker
.set('title', title
)
54 dbody
= _find_tag(doc
, 'body')
57 def concat_chapters(fn
):
59 e
.load(open(fn
).read())
65 lang
= e
.find_language() or 'UND'
66 chapter_depth
, toc_points
= e
.find_probable_chapters()
68 doc
= epub
.new_doc(lang
=lang
)
70 fn
, mimetype
= e
.manifest
[ID
]
72 if mimetype
.startswith('image'):
73 tree
= epub
.new_doc(guts
='<img src="%s" alt="" />' % fn
)
75 tree
= e
.gettree(fn
, parse
=_html_parse
)
77 add_marker(doc
, 'espri-new-page-%s' % ID
, fn
)
82 if __name__
== '__main__':
83 concat_chapters(sys
.argv
[1])