Beispiel #1
0
    def test_epub3_covers(self):
        # cover image
        ce = partial(self.create_epub, ver=3)
        c = ce([cmi('c.jpg')])
        self.assertIsNone(find_cover_image(c))
        c = ce([cmi('c.jpg')], meta_cover='c.jpg')
        self.assertEqual('c.jpg', find_cover_image(c))
        c = ce([cmi('c.jpg', b'z', 'cover-image'), cmi('d.jpg')], meta_cover='d.jpg')
        self.assertEqual('c.jpg', find_cover_image(c))
        mark_as_cover(c, 'd.jpg')
        self.assertEqual('d.jpg', find_cover_image(c))
        self.assertFalse(c.opf_xpath('//*/@name'))

        # title page
        c = ce([cmi('c.html'), cmi('a.html')])
        self.assertIsNone(find_cover_page(c))
        mark_as_titlepage(c, 'a.html', move_to_start=False)
        self.assertEqual('a.html', find_cover_page(c))
        self.assertEqual('c.html', next(c.spine_names)[0])
        mark_as_titlepage(c, 'a.html', move_to_start=True)
        self.assertEqual('a.html', find_cover_page(c))
        self.assertEqual('a.html', next(c.spine_names)[0])

        # clean opf of all cover information
        c = ce([cmi('c.jpg', b'z', 'cover-image'), cmi('c.html', b'', 'calibre:title-page'), cmi('d.html')],
                             meta_cover='c.jpg', guide=[('c.jpg', 'cover'), ('d.html', 'cover')])
        self.assertEqual(set(clean_opf(c)), {'c.jpg', 'c.html', 'd.html'})
        self.assertFalse(c.opf_xpath('//*/@name'))
        self.assertFalse(c.opf_xpath('//*/@type'))
        for prop in 'cover-image calibre:title-page'.split():
            self.assertEqual([], list(c.manifest_items_with_property(prop)))
Beispiel #2
0
 def create_cover_page(self, input_fmt):
     templ = '''
     <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
     <head><style>
     html, body, img { height: 100%%; display: block; margin: 0; padding: 0; border-width: 0; }
     img { width: auto; margin-left:auto; margin-right: auto; }
     </style></head><body><img src="%s"/></body></html>
     '''
     if input_fmt == 'epub':
         def cover_path(action, data):
             if action == 'write_image':
                 data.write(BLANK_JPEG)
         return set_epub_cover(self, cover_path, (lambda *a: None), options={'template':templ})
     raster_cover_name = find_cover_image(self, strict=True)
     if raster_cover_name is None:
         item = self.generate_item(name='cover.jpeg', id_prefix='cover')
         raster_cover_name = self.href_to_name(item.get('href'), self.opf_name)
     with self.open(raster_cover_name, 'wb') as dest:
         dest.write(BLANK_JPEG)
     item = self.generate_item(name='titlepage.html', id_prefix='titlepage')
     titlepage_name = self.href_to_name(item.get('href'), self.opf_name)
     raw = templ % prepare_string_for_xml(self.name_to_href(raster_cover_name, titlepage_name), True)
     with self.open(titlepage_name, 'wb') as f:
         f.write(raw.encode('utf-8'))
     spine = self.opf_xpath('//opf:spine')[0]
     ref = spine.makeelement(OPF('itemref'), idref=item.get('id'))
     self.insert_into_xml(spine, ref, index=0)
     self.dirty(self.opf_name)
     return raster_cover_name, titlepage_name
Beispiel #3
0
    def test_epub2_covers(self):
        # cover image
        ce = partial(self.create_epub, ver=2)
        c = ce([cmi('c.jpg')])
        self.assertIsNone(find_cover_image(c))
        c = ce([cmi('c.jpg')], meta_cover='c.jpg')
        self.assertEqual('c.jpg', find_cover_image(c))
        c = ce([cmi('c.jpg'), cmi('d.jpg')], guide=[('c.jpg', 'cover')])
        self.assertEqual('c.jpg', find_cover_image(c))
        mark_as_cover(c, 'd.jpg')
        self.assertEqual('d.jpg', find_cover_image(c))
        self.assertEqual({'cover':'d.jpg'}, c.guide_type_map)

        # title page
        c = ce([cmi('c.html'), cmi('a.html')])
        self.assertIsNone(find_cover_page(c))
        mark_as_titlepage(c, 'a.html', move_to_start=False)
        self.assertEqual('a.html', find_cover_page(c))
        self.assertEqual('c.html', next(c.spine_names)[0])
        mark_as_titlepage(c, 'a.html', move_to_start=True)
        self.assertEqual('a.html', find_cover_page(c))
        self.assertEqual('a.html', next(c.spine_names)[0])
Beispiel #4
0
 def create_cover_page(self, input_fmt):
     if input_fmt == 'epub':
         def cover_path(action, data):
             if action == 'write_image':
                 data.write(BLANK_JPEG)
         return set_epub_cover(self, cover_path, (lambda *a: None))
     from calibre.ebooks.oeb.transforms.cover import CoverManager
     raster_cover_name = find_cover_image(self, strict=True)
     if raster_cover_name is None:
         item = self.generate_item(name='cover.jpeg', id_prefix='cover')
         raster_cover_name = self.href_to_name(item.get('href'), self.opf_name)
     with self.open(raster_cover_name, 'wb') as dest:
         dest.write(BLANK_JPEG)
     item = self.generate_item(name='titlepage.html', id_prefix='titlepage')
     titlepage_name = self.href_to_name(item.get('href'), self.opf_name)
     templ = CoverManager.SVG_TEMPLATE
     raw = templ % self.name_to_href(raster_cover_name, titlepage_name)
     with self.open(titlepage_name, 'wb') as f:
         f.write(raw.encode('utf-8'))
     spine = self.opf_xpath('//opf:spine')[0]
     ref = spine.makeelement(OPF('itemref'), idref=item.get('id'))
     self.insert_into_xml(spine, ref, index=0)
     self.dirty(self.opf_name)
     return raster_cover_name, titlepage_name