ITEM_STYLE = 2
ITEM_SCRIPT = 3
ITEM_NAVIGATION = 4
ITEM_VECTOR = 5
ITEM_FONT = 6
ITEM_VIDEO = 7
ITEM_AUDIO = 8
ITEM_DOCUMENT = 9
ITEM_COVER = 10
ITEM_SMIL = 11
EXTENSIONS = {ITEM_IMAGE: ['.jpg', '.jpeg', '.gif', '.tiff', '.tif', '.png'],
ITEM_STYLE: ['.css'],
ITEM_VECTOR: ['.svg'],
ITEM_FONT: ['.otf', '.woff', '.ttf'],
ITEM_SCRIPT: ['.js'],
ITEM_NAVIGATION: ['.ncx'],
ITEM_VIDEO: ['.mov', '.mp4', '.avi'],
ITEM_AUDIO: ['.mp3', '.ogg'],
ITEM_COVER: ['.jpg', '.jpeg', '.png'],
ITEM_SMIL: ['.smil']
class Section(object):
def __init__(self, title, href=''):
self.title = title
self.href = href
class Link(object):
def __init__(self, href, title, uid=None):
self.href = href
self.title = title
self.uid = uid
class EpubException(Exception):
def __init__(self, code, msg):
self.code = code
self.msg = msg
def __str__(self):
return repr(self.msg)
class EpubItem(object):
def __init__(self, uid=None, file_name='', media_type='', content=six.b(''), manifest=True):
self.id = uid
self.file_name = file_name
self.media_type = media_type
self.content = content
self.is_linear = True
self.manifest = manifest
self.book = None
def get_id(self):
return self.id
def get_name(self):
return self.file_name
}
def get_content(self, default=six.b('')):
return self.content or default
def set_content(self, content):
self.content = content
def __str__(self):
return '' % self.id
class EpubNcx(EpubItem):
def __init__(self, uid='ncx', file_name='toc.ncx'):
super(EpubNcx, self).__init__(uid=uid, file_name=file_name, media_type='application/x-dtbncx+xml')
def __str__(self):
return '' % self.id
class EpubCover(EpubItem):
def __init__(self, uid='cover-img', file_name=''):
super(EpubCover, self).__init__(uid=uid, file_name=file_name)
def get_type(self):
return ebooklib.ITEM_COVER
def __str__(self):
return '' % (self.id, self.file_name)
class EpubHtml(EpubItem):
_template_name = 'chapter'
def __init__(self, uid=None, file_name='', media_type='', content=None, title='',
lang=None, direction=None, media_overlay=None, media_duration=None):
super(EpubHtml, self).__init__(uid, file_name, media_type, content)
self.title = title
self.lang = lang
self.direction = direction
self.media_overlay = media_overlay
self.media_duration = media_duration
self.links = []
self.properties = []
self.pages = []
def is_chapter(self):
return True
def get_type(self):
return ebooklib.ITEM_DOCUMENT
def set_language(self, lang):
self.lang = lang
def get_language(self):
return self.lang
def add_link(self, **kwgs):
self.links.append(kwgs)
if kwgs.get('type') == 'text/javascript':
if 'scripted' not in self.properties:
self.properties.append('scripted')
def get_links(self):
return (link for link in self.links)
def get_links_of_type(self, link_type):
return (link for link in self.links if link.get('type', '') == link_type)
def add_item(self, item):
if item.get_type() == ebooklib.ITEM_STYLE:
self.add_link(href=item.get_name(), rel='stylesheet', type='text/css')
if item.get_type() == ebooklib.ITEM_SCRIPT:
self.add_link(src=item.get_name(), type='text/javascript')
def get_body_content(self):
try:
html_tree = parse_html_string(self.content)
except:
return ''
html_root = html_tree.getroottree()
if len(html_root.find('body')) != 0:
body = html_tree.find('body')
tree_str = etree.tostring(body, pretty_print=True, encoding='utf-8', xml_declaration=False)
if tree_str.startswith(six.b('')):
n = tree_str.rindex(six.b('
Do'stlaringiz bilan baham: |