mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Merge branch 'py3' of https://github.com/eli-schwartz/calibre
This commit is contained in:
commit
d9fcdbe1a2
@ -1,4 +1,5 @@
|
|||||||
from __future__ import with_statement
|
from __future__ import absolute_import, division, print_function, unicode_literals
|
||||||
|
|
||||||
__license__ = 'GPL v3'
|
__license__ = 'GPL v3'
|
||||||
__copyright__ = '2008, Kovid Goyal kovid@kovidgoyal.net'
|
__copyright__ = '2008, Kovid Goyal kovid@kovidgoyal.net'
|
||||||
__docformat__ = 'restructuredtext en'
|
__docformat__ = 'restructuredtext en'
|
||||||
@ -102,7 +103,8 @@ class ComicInput(InputFormatPlugin):
|
|||||||
'%s is not a valid comic collection'
|
'%s is not a valid comic collection'
|
||||||
' no comics.txt was found in the file')
|
' no comics.txt was found in the file')
|
||||||
%stream.name)
|
%stream.name)
|
||||||
raw = open('comics.txt', 'rb').read()
|
with open('comics.txt', 'rb') as f:
|
||||||
|
raw = f.read()
|
||||||
if raw.startswith(codecs.BOM_UTF16_BE):
|
if raw.startswith(codecs.BOM_UTF16_BE):
|
||||||
raw = raw.decode('utf-16-be')[1:]
|
raw = raw.decode('utf-16-be')[1:]
|
||||||
elif raw.startswith(codecs.BOM_UTF16_LE):
|
elif raw.startswith(codecs.BOM_UTF16_LE):
|
||||||
@ -176,7 +178,7 @@ class ComicInput(InputFormatPlugin):
|
|||||||
comics = []
|
comics = []
|
||||||
for i, x in enumerate(comics_):
|
for i, x in enumerate(comics_):
|
||||||
title, fname = x
|
title, fname = x
|
||||||
cdir = u'comic_%d'%(i+1) if len(comics_) > 1 else u'.'
|
cdir = 'comic_%d'%(i+1) if len(comics_) > 1 else '.'
|
||||||
cdir = os.path.abspath(cdir)
|
cdir = os.path.abspath(cdir)
|
||||||
if not os.path.exists(cdir):
|
if not os.path.exists(cdir):
|
||||||
os.makedirs(cdir)
|
os.makedirs(cdir)
|
||||||
@ -230,14 +232,14 @@ class ComicInput(InputFormatPlugin):
|
|||||||
_('Page')+' %d'%(i+1), play_order=po)
|
_('Page')+' %d'%(i+1), play_order=po)
|
||||||
po += 1
|
po += 1
|
||||||
opf.set_toc(toc)
|
opf.set_toc(toc)
|
||||||
m, n = open(u'metadata.opf', 'wb'), open('toc.ncx', 'wb')
|
with open('metadata.opf', 'wb') as m, open('toc.ncx', 'wb') as n:
|
||||||
opf.render(m, n, u'toc.ncx')
|
opf.render(m, n, 'toc.ncx')
|
||||||
return os.path.abspath(u'metadata.opf')
|
return os.path.abspath('metadata.opf')
|
||||||
|
|
||||||
def create_wrappers(self, pages):
|
def create_wrappers(self, pages):
|
||||||
from calibre.ebooks.oeb.base import XHTML_NS
|
from calibre.ebooks.oeb.base import XHTML_NS
|
||||||
wrappers = []
|
wrappers = []
|
||||||
WRAPPER = textwrap.dedent(u'''\
|
WRAPPER = textwrap.dedent('''\
|
||||||
<html xmlns="%s">
|
<html xmlns="%s">
|
||||||
<head>
|
<head>
|
||||||
<meta charset="utf-8"/>
|
<meta charset="utf-8"/>
|
||||||
@ -258,7 +260,7 @@ class ComicInput(InputFormatPlugin):
|
|||||||
dir = os.path.dirname(pages[0])
|
dir = os.path.dirname(pages[0])
|
||||||
for i, page in enumerate(pages):
|
for i, page in enumerate(pages):
|
||||||
wrapper = WRAPPER%(XHTML_NS, i+1, os.path.basename(page), i+1)
|
wrapper = WRAPPER%(XHTML_NS, i+1, os.path.basename(page), i+1)
|
||||||
page = os.path.join(dir, u'page_%d.xhtml'%(i+1))
|
page = os.path.join(dir, 'page_%d.xhtml'%(i+1))
|
||||||
with open(page, 'wb') as f:
|
with open(page, 'wb') as f:
|
||||||
f.write(wrapper.encode('utf-8'))
|
f.write(wrapper.encode('utf-8'))
|
||||||
wrappers.append(page)
|
wrappers.append(page)
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
#!/usr/bin/env python2
|
#!/usr/bin/env python2
|
||||||
# vim:fileencoding=UTF-8:ts=4:sw=4:sta:et:sts=4:ai
|
# vim:fileencoding=UTF-8:ts=4:sw=4:sta:et:sts=4:ai
|
||||||
from __future__ import with_statement
|
from __future__ import absolute_import, division, print_function, unicode_literals
|
||||||
|
|
||||||
__license__ = 'GPL v3'
|
__license__ = 'GPL v3'
|
||||||
__copyright__ = '2009, Kovid Goyal <kovid@kovidgoyal.net>'
|
__copyright__ = '2009, Kovid Goyal <kovid@kovidgoyal.net>'
|
||||||
@ -233,7 +233,7 @@ class EPUBOutput(OutputFormatPlugin):
|
|||||||
if uuid is None:
|
if uuid is None:
|
||||||
self.log.warn('No UUID identifier found')
|
self.log.warn('No UUID identifier found')
|
||||||
from uuid import uuid4
|
from uuid import uuid4
|
||||||
uuid = str(uuid4())
|
uuid = unicode_type(uuid4())
|
||||||
oeb.metadata.add('identifier', uuid, scheme='uuid', id=uuid)
|
oeb.metadata.add('identifier', uuid, scheme='uuid', id=uuid)
|
||||||
|
|
||||||
if encrypted_fonts and not uuid.startswith('urn:uuid:'):
|
if encrypted_fonts and not uuid.startswith('urn:uuid:'):
|
||||||
@ -244,7 +244,7 @@ class EPUBOutput(OutputFormatPlugin):
|
|||||||
if unicode_type(x) == uuid:
|
if unicode_type(x) == uuid:
|
||||||
x.content = 'urn:uuid:'+uuid
|
x.content = 'urn:uuid:'+uuid
|
||||||
|
|
||||||
with TemporaryDirectory(u'_epub_output') as tdir:
|
with TemporaryDirectory('_epub_output') as tdir:
|
||||||
from calibre.customize.ui import plugin_for_output_format
|
from calibre.customize.ui import plugin_for_output_format
|
||||||
metadata_xml = None
|
metadata_xml = None
|
||||||
extra_entries = []
|
extra_entries = []
|
||||||
@ -252,7 +252,7 @@ class EPUBOutput(OutputFormatPlugin):
|
|||||||
if self.opts.output_profile.epub_periodical_format == 'sony':
|
if self.opts.output_profile.epub_periodical_format == 'sony':
|
||||||
from calibre.ebooks.epub.periodical import sony_metadata
|
from calibre.ebooks.epub.periodical import sony_metadata
|
||||||
metadata_xml, atom_xml = sony_metadata(oeb)
|
metadata_xml, atom_xml = sony_metadata(oeb)
|
||||||
extra_entries = [(u'atom.xml', 'application/atom+xml', atom_xml)]
|
extra_entries = [('atom.xml', 'application/atom+xml', atom_xml)]
|
||||||
oeb_output = plugin_for_output_format('oeb')
|
oeb_output = plugin_for_output_format('oeb')
|
||||||
oeb_output.convert(oeb, tdir, input_plugin, opts, log)
|
oeb_output.convert(oeb, tdir, input_plugin, opts, log)
|
||||||
opf = [x for x in os.listdir(tdir) if x.endswith('.opf')][0]
|
opf = [x for x in os.listdir(tdir) if x.endswith('.opf')][0]
|
||||||
@ -338,7 +338,7 @@ class EPUBOutput(OutputFormatPlugin):
|
|||||||
self.log.warn('Font', path, 'is invalid, ignoring')
|
self.log.warn('Font', path, 'is invalid, ignoring')
|
||||||
if not isinstance(uri, unicode_type):
|
if not isinstance(uri, unicode_type):
|
||||||
uri = uri.decode('utf-8')
|
uri = uri.decode('utf-8')
|
||||||
fonts.append(u'''
|
fonts.append('''
|
||||||
<enc:EncryptedData>
|
<enc:EncryptedData>
|
||||||
<enc:EncryptionMethod Algorithm="http://ns.adobe.com/pdf/enc#RC"/>
|
<enc:EncryptionMethod Algorithm="http://ns.adobe.com/pdf/enc#RC"/>
|
||||||
<enc:CipherData>
|
<enc:CipherData>
|
||||||
@ -347,13 +347,13 @@ class EPUBOutput(OutputFormatPlugin):
|
|||||||
</enc:EncryptedData>
|
</enc:EncryptedData>
|
||||||
'''%(uri.replace('"', '\\"')))
|
'''%(uri.replace('"', '\\"')))
|
||||||
if fonts:
|
if fonts:
|
||||||
ans = '''<encryption
|
ans = b'''<encryption
|
||||||
xmlns="urn:oasis:names:tc:opendocument:xmlns:container"
|
xmlns="urn:oasis:names:tc:opendocument:xmlns:container"
|
||||||
xmlns:enc="http://www.w3.org/2001/04/xmlenc#"
|
xmlns:enc="http://www.w3.org/2001/04/xmlenc#"
|
||||||
xmlns:deenc="http://ns.adobe.com/digitaleditions/enc">
|
xmlns:deenc="http://ns.adobe.com/digitaleditions/enc">
|
||||||
'''
|
'''
|
||||||
ans += (u'\n'.join(fonts)).encode('utf-8')
|
ans += '\n'.join(fonts).encode('utf-8')
|
||||||
ans += '\n</encryption>'
|
ans += b'\n</encryption>'
|
||||||
return ans
|
return ans
|
||||||
# }}}
|
# }}}
|
||||||
|
|
||||||
@ -367,7 +367,8 @@ class EPUBOutput(OutputFormatPlugin):
|
|||||||
if tag.tail:
|
if tag.tail:
|
||||||
tag.tail = tag.tail.strip()
|
tag.tail = tag.tail.strip()
|
||||||
compressed = etree.tostring(tree.getroot(), encoding='utf-8')
|
compressed = etree.tostring(tree.getroot(), encoding='utf-8')
|
||||||
open(ncx_path, 'wb').write(compressed)
|
with open(ncx_path, 'wb') as f:
|
||||||
|
f.write(compressed)
|
||||||
# }}}
|
# }}}
|
||||||
|
|
||||||
def workaround_ade_quirks(self): # {{{
|
def workaround_ade_quirks(self): # {{{
|
||||||
@ -430,7 +431,7 @@ class EPUBOutput(OutputFormatPlugin):
|
|||||||
if priortext:
|
if priortext:
|
||||||
priortext = priortext.strip()
|
priortext = priortext.strip()
|
||||||
br.tag = XHTML('p')
|
br.tag = XHTML('p')
|
||||||
br.text = u'\u00a0'
|
br.text = '\u00a0'
|
||||||
style = br.get('style', '').split(';')
|
style = br.get('style', '').split(';')
|
||||||
style = list(filter(None, map(lambda x: x.strip(), style)))
|
style = list(filter(None, map(lambda x: x.strip(), style)))
|
||||||
style.append('margin:0pt; border:0pt')
|
style.append('margin:0pt; border:0pt')
|
||||||
@ -483,14 +484,14 @@ class EPUBOutput(OutputFormatPlugin):
|
|||||||
tag.tag = XHTML('div')
|
tag.tag = XHTML('div')
|
||||||
|
|
||||||
# ADE fails to render non breaking hyphens/soft hyphens/zero width spaces
|
# ADE fails to render non breaking hyphens/soft hyphens/zero width spaces
|
||||||
special_chars = re.compile(u'[\u200b\u00ad]')
|
special_chars = re.compile('[\u200b\u00ad]')
|
||||||
for elem in root.iterdescendants('*'):
|
for elem in root.iterdescendants('*'):
|
||||||
if elem.text:
|
if elem.text:
|
||||||
elem.text = special_chars.sub('', elem.text)
|
elem.text = special_chars.sub('', elem.text)
|
||||||
elem.text = elem.text.replace(u'\u2011', '-')
|
elem.text = elem.text.replace('\u2011', '-')
|
||||||
if elem.tail:
|
if elem.tail:
|
||||||
elem.tail = special_chars.sub('', elem.tail)
|
elem.tail = special_chars.sub('', elem.tail)
|
||||||
elem.tail = elem.tail.replace(u'\u2011', '-')
|
elem.tail = elem.tail.replace('\u2011', '-')
|
||||||
|
|
||||||
if stylesheet is not None:
|
if stylesheet is not None:
|
||||||
# ADE doesn't render lists correctly if they have left margins
|
# ADE doesn't render lists correctly if they have left margins
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
from __future__ import with_statement
|
from __future__ import absolute_import, division, print_function, unicode_literals
|
||||||
|
|
||||||
__license__ = 'GPL v3'
|
__license__ = 'GPL v3'
|
||||||
__copyright__ = '2008, Anatoly Shipitsin <norguhtar at gmail.com>'
|
__copyright__ = '2008, Anatoly Shipitsin <norguhtar at gmail.com>'
|
||||||
"""
|
"""
|
||||||
@ -125,9 +126,9 @@ class FB2Input(InputFormatPlugin):
|
|||||||
src = img.get('src')
|
src = img.get('src')
|
||||||
img.set('src', self.binary_map.get(src, src))
|
img.set('src', self.binary_map.get(src, src))
|
||||||
index = transform.tostring(result)
|
index = transform.tostring(result)
|
||||||
with open(u'index.xhtml', 'wb') as f:
|
with open('index.xhtml', 'wb') as f:
|
||||||
f.write(index.encode('utf-8'))
|
f.write(index.encode('utf-8'))
|
||||||
with open(u'inline-styles.css', 'wb') as f:
|
with open('inline-styles.css', 'wb') as f:
|
||||||
f.write(css.encode('utf-8'))
|
f.write(css.encode('utf-8'))
|
||||||
stream.seek(0)
|
stream.seek(0)
|
||||||
mi = get_metadata(stream, 'fb2')
|
mi = get_metadata(stream, 'fb2')
|
||||||
@ -137,9 +138,9 @@ class FB2Input(InputFormatPlugin):
|
|||||||
mi.authors = [_('Unknown')]
|
mi.authors = [_('Unknown')]
|
||||||
cpath = None
|
cpath = None
|
||||||
if mi.cover_data and mi.cover_data[1]:
|
if mi.cover_data and mi.cover_data[1]:
|
||||||
with open(u'fb2_cover_calibre_mi.jpg', 'wb') as f:
|
with open('fb2_cover_calibre_mi.jpg', 'wb') as f:
|
||||||
f.write(mi.cover_data[1])
|
f.write(mi.cover_data[1])
|
||||||
cpath = os.path.abspath(u'fb2_cover_calibre_mi.jpg')
|
cpath = os.path.abspath('fb2_cover_calibre_mi.jpg')
|
||||||
else:
|
else:
|
||||||
for img in doc.xpath('//f:coverpage/f:image', namespaces=NAMESPACES):
|
for img in doc.xpath('//f:coverpage/f:image', namespaces=NAMESPACES):
|
||||||
href = img.get('{%s}href'%XLINK_NS, img.get('href', None))
|
href = img.get('{%s}href'%XLINK_NS, img.get('href', None))
|
||||||
@ -152,12 +153,12 @@ class FB2Input(InputFormatPlugin):
|
|||||||
opf = OPFCreator(getcwd(), mi)
|
opf = OPFCreator(getcwd(), mi)
|
||||||
entries = [(f2, guess_type(f2)[0]) for f2 in os.listdir(u'.')]
|
entries = [(f2, guess_type(f2)[0]) for f2 in os.listdir(u'.')]
|
||||||
opf.create_manifest(entries)
|
opf.create_manifest(entries)
|
||||||
opf.create_spine([u'index.xhtml'])
|
opf.create_spine(['index.xhtml'])
|
||||||
if cpath:
|
if cpath:
|
||||||
opf.guide.set_cover(cpath)
|
opf.guide.set_cover(cpath)
|
||||||
with open(u'metadata.opf', 'wb') as f:
|
with open('metadata.opf', 'wb') as f:
|
||||||
opf.render(f)
|
opf.render(f)
|
||||||
return os.path.join(getcwd(), u'metadata.opf')
|
return os.path.join(getcwd(), 'metadata.opf')
|
||||||
|
|
||||||
def extract_embedded_content(self, doc):
|
def extract_embedded_content(self, doc):
|
||||||
from calibre.ebooks.fb2 import base64_decode
|
from calibre.ebooks.fb2 import base64_decode
|
||||||
|
@ -120,7 +120,7 @@ class HTMLInput(InputFormatPlugin):
|
|||||||
if not metadata.language:
|
if not metadata.language:
|
||||||
l = canonicalize_lang(getattr(opts, 'language', None))
|
l = canonicalize_lang(getattr(opts, 'language', None))
|
||||||
if not l:
|
if not l:
|
||||||
oeb.logger.warn(u'Language not specified')
|
oeb.logger.warn('Language not specified')
|
||||||
l = get_lang().replace('_', '-')
|
l = get_lang().replace('_', '-')
|
||||||
metadata.add('language', l)
|
metadata.add('language', l)
|
||||||
if not metadata.creator:
|
if not metadata.creator:
|
||||||
@ -135,7 +135,7 @@ class HTMLInput(InputFormatPlugin):
|
|||||||
if not metadata.title:
|
if not metadata.title:
|
||||||
oeb.logger.warn('Title not specified')
|
oeb.logger.warn('Title not specified')
|
||||||
metadata.add('title', self.oeb.translate(__('Unknown')))
|
metadata.add('title', self.oeb.translate(__('Unknown')))
|
||||||
bookid = str(uuid.uuid4())
|
bookid = unicode_type(uuid.uuid4())
|
||||||
metadata.add('identifier', bookid, id='uuid_id', scheme='uuid')
|
metadata.add('identifier', bookid, id='uuid_id', scheme='uuid')
|
||||||
for ident in metadata.identifier:
|
for ident in metadata.identifier:
|
||||||
if 'id' in ident.attrib:
|
if 'id' in ident.attrib:
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
from __future__ import with_statement
|
from __future__ import absolute_import, division, print_function, unicode_literals
|
||||||
|
|
||||||
__license__ = 'GPL 3'
|
__license__ = 'GPL 3'
|
||||||
__copyright__ = '2010, Fabian Grassl <fg@jusmeum.de>'
|
__copyright__ = '2010, Fabian Grassl <fg@jusmeum.de>'
|
||||||
__docformat__ = 'restructuredtext en'
|
__docformat__ = 'restructuredtext en'
|
||||||
@ -91,17 +92,20 @@ class HTMLOutput(OutputFormatPlugin):
|
|||||||
|
|
||||||
# read template files
|
# read template files
|
||||||
if opts.template_html_index is not None:
|
if opts.template_html_index is not None:
|
||||||
template_html_index_data = open(opts.template_html_index, 'rb').read()
|
with open(opts.template_html_index, 'rb') as f:
|
||||||
|
template_html_index_data = f.read()
|
||||||
else:
|
else:
|
||||||
template_html_index_data = P('templates/html_export_default_index.tmpl', data=True)
|
template_html_index_data = P('templates/html_export_default_index.tmpl', data=True)
|
||||||
|
|
||||||
if opts.template_html is not None:
|
if opts.template_html is not None:
|
||||||
template_html_data = open(opts.template_html, 'rb').read()
|
with open(opts.template_html, 'rb') as f:
|
||||||
|
template_html_data = f.read()
|
||||||
else:
|
else:
|
||||||
template_html_data = P('templates/html_export_default.tmpl', data=True)
|
template_html_data = P('templates/html_export_default.tmpl', data=True)
|
||||||
|
|
||||||
if opts.template_css is not None:
|
if opts.template_css is not None:
|
||||||
template_css_data = open(opts.template_css, 'rb').read()
|
with open(opts.template_css, 'rb') as f:
|
||||||
|
template_css_data = f.read()
|
||||||
else:
|
else:
|
||||||
template_css_data = P('templates/html_export_default.css', data=True)
|
template_css_data = P('templates/html_export_default.css', data=True)
|
||||||
|
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
|
from __future__ import absolute_import, division, print_function, unicode_literals
|
||||||
|
|
||||||
__license__ = 'GPL 3'
|
__license__ = 'GPL 3'
|
||||||
__copyright__ = '2010, Li Fanxi <lifanxi@freemindworld.com>'
|
__copyright__ = '2010, Li Fanxi <lifanxi@freemindworld.com>'
|
||||||
@ -9,12 +10,13 @@ import os
|
|||||||
from calibre.customize.conversion import InputFormatPlugin
|
from calibre.customize.conversion import InputFormatPlugin
|
||||||
from calibre.ptempfile import TemporaryDirectory
|
from calibre.ptempfile import TemporaryDirectory
|
||||||
from calibre.utils.filenames import ascii_filename
|
from calibre.utils.filenames import ascii_filename
|
||||||
|
from polyglot.builtins import unicode_type
|
||||||
|
|
||||||
HTML_TEMPLATE = u'<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"/><title>%s</title></head><body>\n%s\n</body></html>'
|
HTML_TEMPLATE = '<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"/><title>%s</title></head><body>\n%s\n</body></html>'
|
||||||
|
|
||||||
|
|
||||||
def html_encode(s):
|
def html_encode(s):
|
||||||
return s.replace(u'&', u'&').replace(u'<', u'<').replace(u'>', u'>').replace(u'"', u'"').replace(u"'", u''').replace(u'\n', u'<br/>').replace(u' ', u' ') # noqa
|
return s.replace('&', '&').replace('<', '<').replace('>', '>').replace('"', '"').replace("'", ''').replace('\n', '<br/>').replace(' ', ' ') # noqa
|
||||||
|
|
||||||
|
|
||||||
class SNBInput(InputFormatPlugin):
|
class SNBInput(InputFormatPlugin):
|
||||||
@ -43,7 +45,7 @@ class SNBInput(InputFormatPlugin):
|
|||||||
except:
|
except:
|
||||||
raise ValueError("Invalid SNB file")
|
raise ValueError("Invalid SNB file")
|
||||||
if not snbFile.IsValid():
|
if not snbFile.IsValid():
|
||||||
log.debug("Invaild SNB file")
|
log.debug("Invalid SNB file")
|
||||||
raise ValueError("Invalid SNB file")
|
raise ValueError("Invalid SNB file")
|
||||||
log.debug("Handle meta data ...")
|
log.debug("Handle meta data ...")
|
||||||
from calibre.ebooks.conversion.plumber import create_oebbook
|
from calibre.ebooks.conversion.plumber import create_oebbook
|
||||||
@ -74,7 +76,7 @@ class SNBInput(InputFormatPlugin):
|
|||||||
if d['cover'] != '':
|
if d['cover'] != '':
|
||||||
oeb.guide.add('cover', 'Cover', d['cover'])
|
oeb.guide.add('cover', 'Cover', d['cover'])
|
||||||
|
|
||||||
bookid = str(uuid.uuid4())
|
bookid = unicode_type(uuid.uuid4())
|
||||||
oeb.metadata.add('identifier', bookid, id='uuid_id', scheme='uuid')
|
oeb.metadata.add('identifier', bookid, id='uuid_id', scheme='uuid')
|
||||||
for ident in oeb.metadata.identifier:
|
for ident in oeb.metadata.identifier:
|
||||||
if 'id' in ident.attrib:
|
if 'id' in ident.attrib:
|
||||||
@ -96,15 +98,14 @@ class SNBInput(InputFormatPlugin):
|
|||||||
if data is None:
|
if data is None:
|
||||||
continue
|
continue
|
||||||
snbc = etree.fromstring(data)
|
snbc = etree.fromstring(data)
|
||||||
outputFile = open(os.path.join(tdir, fname), 'wb')
|
|
||||||
lines = []
|
lines = []
|
||||||
for line in snbc.find('.//body'):
|
for line in snbc.find('.//body'):
|
||||||
if line.tag == 'text':
|
if line.tag == 'text':
|
||||||
lines.append(u'<p>%s</p>' % html_encode(line.text))
|
lines.append('<p>%s</p>' % html_encode(line.text))
|
||||||
elif line.tag == 'img':
|
elif line.tag == 'img':
|
||||||
lines.append(u'<p><img src="%s" /></p>' % html_encode(line.text))
|
lines.append('<p><img src="%s" /></p>' % html_encode(line.text))
|
||||||
outputFile.write((HTML_TEMPLATE % (chapterName, u'\n'.join(lines))).encode('utf-8', 'replace'))
|
with open(os.path.join(tdir, fname), 'wb') as f:
|
||||||
outputFile.close()
|
f.write((HTML_TEMPLATE % (chapterName, '\n'.join(lines))).encode('utf-8', 'replace'))
|
||||||
oeb.toc.add(ch.text, fname)
|
oeb.toc.add(ch.text, fname)
|
||||||
id, href = oeb.manifest.generate(id='html',
|
id, href = oeb.manifest.generate(id='html',
|
||||||
href=ascii_filename(fname))
|
href=ascii_filename(fname))
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
|
from __future__ import absolute_import, division, print_function, unicode_literals
|
||||||
|
|
||||||
__license__ = 'GPL 3'
|
__license__ = 'GPL 3'
|
||||||
__copyright__ = '2010, Li Fanxi <lifanxi@freemindworld.com>'
|
__copyright__ = '2010, Li Fanxi <lifanxi@freemindworld.com>'
|
||||||
@ -113,9 +114,8 @@ class SNBOutput(OutputFormatPlugin):
|
|||||||
etree.SubElement(headTree, "cover").text = ProcessFileName(href)
|
etree.SubElement(headTree, "cover").text = ProcessFileName(href)
|
||||||
else:
|
else:
|
||||||
etree.SubElement(headTree, "cover")
|
etree.SubElement(headTree, "cover")
|
||||||
bookInfoFile = open(os.path.join(snbfDir, 'book.snbf'), 'wb')
|
with open(os.path.join(snbfDir, 'book.snbf'), 'wb') as f:
|
||||||
bookInfoFile.write(etree.tostring(bookInfoTree, pretty_print=True, encoding='utf-8'))
|
f.write(etree.tostring(bookInfoTree, pretty_print=True, encoding='utf-8'))
|
||||||
bookInfoFile.close()
|
|
||||||
|
|
||||||
# Output TOC
|
# Output TOC
|
||||||
tocInfoTree = etree.Element("toc-snbf")
|
tocInfoTree = etree.Element("toc-snbf")
|
||||||
@ -168,9 +168,8 @@ class SNBOutput(OutputFormatPlugin):
|
|||||||
|
|
||||||
etree.SubElement(tocHead, "chapters").text = '%d' % len(tocBody)
|
etree.SubElement(tocHead, "chapters").text = '%d' % len(tocBody)
|
||||||
|
|
||||||
tocInfoFile = open(os.path.join(snbfDir, 'toc.snbf'), 'wb')
|
with open(os.path.join(snbfDir, 'toc.snbf'), 'wb') as f:
|
||||||
tocInfoFile.write(etree.tostring(tocInfoTree, pretty_print=True, encoding='utf-8'))
|
f.write(etree.tostring(tocInfoTree, pretty_print=True, encoding='utf-8'))
|
||||||
tocInfoFile.close()
|
|
||||||
|
|
||||||
# Output Files
|
# Output Files
|
||||||
oldTree = None
|
oldTree = None
|
||||||
@ -185,9 +184,8 @@ class SNBOutput(OutputFormatPlugin):
|
|||||||
else:
|
else:
|
||||||
if oldTree is not None and mergeLast:
|
if oldTree is not None and mergeLast:
|
||||||
log.debug('Output the modified chapter again: %s' % lastName)
|
log.debug('Output the modified chapter again: %s' % lastName)
|
||||||
outputFile = open(os.path.join(snbcDir, lastName), 'wb')
|
with open(os.path.join(snbcDir, lastName), 'wb') as f:
|
||||||
outputFile.write(etree.tostring(oldTree, pretty_print=True, encoding='utf-8'))
|
f.write(etree.tostring(oldTree, pretty_print=True, encoding='utf-8'))
|
||||||
outputFile.close()
|
|
||||||
mergeLast = False
|
mergeLast = False
|
||||||
|
|
||||||
log.debug('Converting %s to snbc...' % item.href)
|
log.debug('Converting %s to snbc...' % item.href)
|
||||||
@ -201,9 +199,8 @@ class SNBOutput(OutputFormatPlugin):
|
|||||||
postfix = '_' + subName
|
postfix = '_' + subName
|
||||||
lastName = ProcessFileName(item.href + postfix + ".snbc")
|
lastName = ProcessFileName(item.href + postfix + ".snbc")
|
||||||
oldTree = snbcTrees[subName]
|
oldTree = snbcTrees[subName]
|
||||||
outputFile = open(os.path.join(snbcDir, lastName), 'wb')
|
with open(os.path.join(snbcDir, lastName), 'wb') as f:
|
||||||
outputFile.write(etree.tostring(oldTree, pretty_print=True, encoding='utf-8'))
|
f.write(etree.tostring(oldTree, pretty_print=True, encoding='utf-8'))
|
||||||
outputFile.close()
|
|
||||||
else:
|
else:
|
||||||
log.debug('Merge %s with last TOC item...' % item.href)
|
log.debug('Merge %s with last TOC item...' % item.href)
|
||||||
snbwriter.merge_content(oldTree, oeb_book, item, [('', _("Start"))], opts)
|
snbwriter.merge_content(oldTree, oeb_book, item, [('', _("Start"))], opts)
|
||||||
@ -211,9 +208,8 @@ class SNBOutput(OutputFormatPlugin):
|
|||||||
# Output the last one if needed
|
# Output the last one if needed
|
||||||
log.debug('Output the last modified chapter again: %s' % lastName)
|
log.debug('Output the last modified chapter again: %s' % lastName)
|
||||||
if oldTree is not None and mergeLast:
|
if oldTree is not None and mergeLast:
|
||||||
outputFile = open(os.path.join(snbcDir, lastName), 'wb')
|
with open(os.path.join(snbcDir, lastName), 'wb') as f:
|
||||||
outputFile.write(etree.tostring(oldTree, pretty_print=True, encoding='utf-8'))
|
f.write(etree.tostring(oldTree, pretty_print=True, encoding='utf-8'))
|
||||||
outputFile.close()
|
|
||||||
mergeLast = False
|
mergeLast = False
|
||||||
|
|
||||||
for item in m:
|
for item in m:
|
||||||
@ -248,7 +244,7 @@ class SNBOutput(OutputFormatPlugin):
|
|||||||
# TODO : intelligent image rotation
|
# TODO : intelligent image rotation
|
||||||
# img = img.rotate(90)
|
# img = img.rotate(90)
|
||||||
# x,y = y,x
|
# x,y = y,x
|
||||||
img = resize_image(img, x / scale, y / scale)
|
img = resize_image(img, x // scale, y // scale)
|
||||||
with lopen(imagePath, 'wb') as f:
|
with lopen(imagePath, 'wb') as f:
|
||||||
f.write(image_to_data(img, fmt=imagePath.rpartition('.')[-1]))
|
f.write(image_to_data(img, fmt=imagePath.rpartition('.')[-1]))
|
||||||
|
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
from __future__ import print_function
|
from __future__ import absolute_import, division, print_function, unicode_literals
|
||||||
|
|
||||||
__license__ = 'GPL 3'
|
__license__ = 'GPL 3'
|
||||||
__copyright__ = '2009, John Schember <john@nachtimwald.com>'
|
__copyright__ = '2009, John Schember <john@nachtimwald.com>'
|
||||||
|
@ -8,7 +8,7 @@ __copyright__ = '2013, Kovid Goyal <kovid at kovidgoyal.net>'
|
|||||||
import re
|
import re
|
||||||
|
|
||||||
from calibre.ebooks.docx.index import process_index, polish_index_markup
|
from calibre.ebooks.docx.index import process_index, polish_index_markup
|
||||||
from polyglot.builtins import iteritems
|
from polyglot.builtins import iteritems, native_string_type
|
||||||
|
|
||||||
|
|
||||||
class Field(object):
|
class Field(object):
|
||||||
@ -75,7 +75,7 @@ def parser(name, field_map, default_field_name=None):
|
|||||||
ans.pop(null, None)
|
ans.pop(null, None)
|
||||||
return ans
|
return ans
|
||||||
|
|
||||||
parse.__name__ = str('parse_' + name)
|
parse.__name__ = native_string_type('parse_' + name)
|
||||||
|
|
||||||
return parse
|
return parse
|
||||||
|
|
||||||
|
@ -14,7 +14,7 @@ from lxml.html.builder import OL, UL, SPAN
|
|||||||
from calibre.ebooks.docx.block_styles import ParagraphStyle
|
from calibre.ebooks.docx.block_styles import ParagraphStyle
|
||||||
from calibre.ebooks.docx.char_styles import RunStyle, inherit
|
from calibre.ebooks.docx.char_styles import RunStyle, inherit
|
||||||
from calibre.ebooks.metadata import roman
|
from calibre.ebooks.metadata import roman
|
||||||
from polyglot.builtins import iteritems
|
from polyglot.builtins import iteritems, unicode_type
|
||||||
|
|
||||||
STYLE_MAP = {
|
STYLE_MAP = {
|
||||||
'aiueo': 'hiragana',
|
'aiueo': 'hiragana',
|
||||||
@ -291,7 +291,7 @@ class Numbering(object):
|
|||||||
seen_instances.add(num_id)
|
seen_instances.add(num_id)
|
||||||
p.tag = 'li'
|
p.tag = 'li'
|
||||||
p.set('value', '%s' % counter[ilvl])
|
p.set('value', '%s' % counter[ilvl])
|
||||||
p.set('list-lvl', str(ilvl))
|
p.set('list-lvl', unicode_type(ilvl))
|
||||||
p.set('list-id', num_id)
|
p.set('list-id', num_id)
|
||||||
if lvl.num_template is not None:
|
if lvl.num_template is not None:
|
||||||
val = lvl.format_template(counter, ilvl, lvl.num_template)
|
val = lvl.format_template(counter, ilvl, lvl.num_template)
|
||||||
|
@ -28,7 +28,7 @@ from calibre.ebooks.docx.fields import Fields
|
|||||||
from calibre.ebooks.docx.settings import Settings
|
from calibre.ebooks.docx.settings import Settings
|
||||||
from calibre.ebooks.metadata.opf2 import OPFCreator
|
from calibre.ebooks.metadata.opf2 import OPFCreator
|
||||||
from calibre.utils.localization import canonicalize_lang, lang_as_iso639_1
|
from calibre.utils.localization import canonicalize_lang, lang_as_iso639_1
|
||||||
from polyglot.builtins import iteritems, itervalues, filter, getcwd, map
|
from polyglot.builtins import iteritems, itervalues, filter, getcwd, map, unicode_type
|
||||||
|
|
||||||
|
|
||||||
NBSP = '\xa0'
|
NBSP = '\xa0'
|
||||||
@ -480,7 +480,7 @@ class Convert(object):
|
|||||||
current_hyperlink = x
|
current_hyperlink = x
|
||||||
elif x.tag.endswith('}instrText') and x.text and x.text.strip().startswith('TOC '):
|
elif x.tag.endswith('}instrText') and x.text and x.text.strip().startswith('TOC '):
|
||||||
old_anchor = current_anchor
|
old_anchor = current_anchor
|
||||||
anchor = str(uuid.uuid4())
|
anchor = unicode_type(uuid.uuid4())
|
||||||
self.anchor_map[anchor] = current_anchor = generate_anchor('toc', frozenset(itervalues(self.anchor_map)))
|
self.anchor_map[anchor] = current_anchor = generate_anchor('toc', frozenset(itervalues(self.anchor_map)))
|
||||||
self.toc_anchor = current_anchor
|
self.toc_anchor = current_anchor
|
||||||
if old_anchor is not None:
|
if old_anchor is not None:
|
||||||
@ -693,9 +693,9 @@ class Convert(object):
|
|||||||
ans.append(text.elem)
|
ans.append(text.elem)
|
||||||
ans[-1].set('class', 'tab')
|
ans[-1].set('class', 'tab')
|
||||||
elif self.namespace.is_tag(child, 'w:noBreakHyphen'):
|
elif self.namespace.is_tag(child, 'w:noBreakHyphen'):
|
||||||
text.buf.append(u'\u2011')
|
text.buf.append('\u2011')
|
||||||
elif self.namespace.is_tag(child, 'w:softHyphen'):
|
elif self.namespace.is_tag(child, 'w:softHyphen'):
|
||||||
text.buf.append(u'\u00ad')
|
text.buf.append('\u00ad')
|
||||||
if text.buf:
|
if text.buf:
|
||||||
setattr(text.elem, text.attr, ''.join(text.buf))
|
setattr(text.elem, text.attr, ''.join(text.buf))
|
||||||
|
|
||||||
|
@ -18,7 +18,7 @@ from calibre.utils.date import utcnow
|
|||||||
from calibre.utils.localization import canonicalize_lang, lang_as_iso639_1
|
from calibre.utils.localization import canonicalize_lang, lang_as_iso639_1
|
||||||
from calibre.utils.zipfile import ZipFile
|
from calibre.utils.zipfile import ZipFile
|
||||||
from calibre.ebooks.pdf.render.common import PAPER_SIZES
|
from calibre.ebooks.pdf.render.common import PAPER_SIZES
|
||||||
from polyglot.builtins import iteritems, map
|
from polyglot.builtins import iteritems, map, unicode_type
|
||||||
|
|
||||||
|
|
||||||
def xml2str(root, pretty_print=False, with_tail=False):
|
def xml2str(root, pretty_print=False, with_tail=False):
|
||||||
@ -65,9 +65,9 @@ def create_skeleton(opts, namespaces=None):
|
|||||||
|
|
||||||
def margin(which):
|
def margin(which):
|
||||||
val = page_margin(opts, which)
|
val = page_margin(opts, which)
|
||||||
return w(which), str(int(val * 20))
|
return w(which), unicode_type(int(val * 20))
|
||||||
body.append(E.sectPr(
|
body.append(E.sectPr(
|
||||||
E.pgSz(**{w('w'):str(width), w('h'):str(height)}),
|
E.pgSz(**{w('w'):unicode_type(width), w('h'):unicode_type(height)}),
|
||||||
E.pgMar(**dict(map(margin, 'left top right bottom'.split()))),
|
E.pgMar(**dict(map(margin, 'left top right bottom'.split()))),
|
||||||
E.cols(**{w('space'):'720'}),
|
E.cols(**{w('space'):'720'}),
|
||||||
E.docGrid(**{w('linePitch'):"360"}),
|
E.docGrid(**{w('linePitch'):"360"}),
|
||||||
@ -243,7 +243,7 @@ class DOCX(object):
|
|||||||
namespaces = self.namespace.namespaces
|
namespaces = self.namespace.namespaces
|
||||||
E = ElementMaker(namespace=namespaces['cp'], nsmap={x:namespaces[x] for x in 'cp dc dcterms xsi'.split()})
|
E = ElementMaker(namespace=namespaces['cp'], nsmap={x:namespaces[x] for x in 'cp dc dcterms xsi'.split()})
|
||||||
cp = E.coreProperties(E.revision("1"), E.lastModifiedBy('calibre'))
|
cp = E.coreProperties(E.revision("1"), E.lastModifiedBy('calibre'))
|
||||||
ts = utcnow().isoformat(str('T')).rpartition('.')[0] + 'Z'
|
ts = utcnow().isoformat(unicode_type('T')).rpartition('.')[0] + 'Z'
|
||||||
for x in 'created modified'.split():
|
for x in 'created modified'.split():
|
||||||
x = cp.makeelement('{%s}%s' % (namespaces['dcterms'], x), **{'{%s}type' % namespaces['xsi']:'dcterms:W3CDTF'})
|
x = cp.makeelement('{%s}%s' % (namespaces['dcterms'], x), **{'{%s}type' % namespaces['xsi']:'dcterms:W3CDTF'})
|
||||||
x.text = ts
|
x.text = ts
|
||||||
|
@ -101,7 +101,7 @@ class TextRun(object):
|
|||||||
for text, preserve_whitespace, bookmark in self.texts:
|
for text, preserve_whitespace, bookmark in self.texts:
|
||||||
if bookmark is not None:
|
if bookmark is not None:
|
||||||
bid = links_manager.bookmark_id
|
bid = links_manager.bookmark_id
|
||||||
makeelement(r, 'w:bookmarkStart', w_id=str(bid), w_name=bookmark)
|
makeelement(r, 'w:bookmarkStart', w_id=unicode_type(bid), w_name=bookmark)
|
||||||
if text is None:
|
if text is None:
|
||||||
makeelement(r, 'w:br', w_clear=preserve_whitespace)
|
makeelement(r, 'w:br', w_clear=preserve_whitespace)
|
||||||
elif hasattr(text, 'xpath'):
|
elif hasattr(text, 'xpath'):
|
||||||
@ -112,7 +112,7 @@ class TextRun(object):
|
|||||||
if preserve_whitespace:
|
if preserve_whitespace:
|
||||||
t.set('{http://www.w3.org/XML/1998/namespace}space', 'preserve')
|
t.set('{http://www.w3.org/XML/1998/namespace}space', 'preserve')
|
||||||
if bookmark is not None:
|
if bookmark is not None:
|
||||||
makeelement(r, 'w:bookmarkEnd', w_id=str(bid))
|
makeelement(r, 'w:bookmarkEnd', w_id=unicode_type(bid))
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return repr(self.texts)
|
return repr(self.texts)
|
||||||
@ -207,7 +207,7 @@ class Block(object):
|
|||||||
p = makeelement(body, 'w:p')
|
p = makeelement(body, 'w:p')
|
||||||
end_bookmarks = []
|
end_bookmarks = []
|
||||||
for bmark in self.bookmarks:
|
for bmark in self.bookmarks:
|
||||||
end_bookmarks.append(str(self.links_manager.bookmark_id))
|
end_bookmarks.append(unicode_type(self.links_manager.bookmark_id))
|
||||||
makeelement(p, 'w:bookmarkStart', w_id=end_bookmarks[-1], w_name=bmark)
|
makeelement(p, 'w:bookmarkStart', w_id=end_bookmarks[-1], w_name=bmark)
|
||||||
if self.block_lang:
|
if self.block_lang:
|
||||||
rpr = makeelement(p, 'w:rPr')
|
rpr = makeelement(p, 'w:rPr')
|
||||||
@ -220,8 +220,8 @@ class Block(object):
|
|||||||
self.float_spec.serialize(self, ppr)
|
self.float_spec.serialize(self, ppr)
|
||||||
if self.numbering_id is not None:
|
if self.numbering_id is not None:
|
||||||
numpr = makeelement(ppr, 'w:numPr')
|
numpr = makeelement(ppr, 'w:numPr')
|
||||||
makeelement(numpr, 'w:ilvl', w_val=str(self.numbering_id[1]))
|
makeelement(numpr, 'w:ilvl', w_val=unicode_type(self.numbering_id[1]))
|
||||||
makeelement(numpr, 'w:numId', w_val=str(self.numbering_id[0]))
|
makeelement(numpr, 'w:numId', w_val=unicode_type(self.numbering_id[0]))
|
||||||
if self.linked_style is not None:
|
if self.linked_style is not None:
|
||||||
makeelement(ppr, 'w:pStyle', w_val=self.linked_style.id)
|
makeelement(ppr, 'w:pStyle', w_val=self.linked_style.id)
|
||||||
elif self.style.id:
|
elif self.style.id:
|
||||||
|
@ -9,6 +9,7 @@ import posixpath, re
|
|||||||
from uuid import uuid4
|
from uuid import uuid4
|
||||||
|
|
||||||
from calibre.utils.filenames import ascii_text
|
from calibre.utils.filenames import ascii_text
|
||||||
|
from polyglot.builtins import unicode_type
|
||||||
from polyglot.urllib import urlparse
|
from polyglot.urllib import urlparse
|
||||||
|
|
||||||
|
|
||||||
@ -35,7 +36,7 @@ class TOCItem(object):
|
|||||||
p = makeelement(body, 'w:p', append=False)
|
p = makeelement(body, 'w:p', append=False)
|
||||||
ppr = makeelement(p, 'w:pPr')
|
ppr = makeelement(p, 'w:pPr')
|
||||||
makeelement(ppr, 'w:pStyle', w_val="Normal")
|
makeelement(ppr, 'w:pStyle', w_val="Normal")
|
||||||
makeelement(ppr, 'w:ind', w_left='0', w_firstLineChars='0', w_firstLine='0', w_leftChars=str(200 * self.level))
|
makeelement(ppr, 'w:ind', w_left='0', w_firstLineChars='0', w_firstLine='0', w_leftChars=unicode_type(200 * self.level))
|
||||||
if self.is_first:
|
if self.is_first:
|
||||||
makeelement(ppr, 'w:pageBreakBefore', w_val='off')
|
makeelement(ppr, 'w:pageBreakBefore', w_val='off')
|
||||||
r = makeelement(p, 'w:r')
|
r = makeelement(p, 'w:r')
|
||||||
@ -69,7 +70,7 @@ class LinksManager(object):
|
|||||||
self.namespace = namespace
|
self.namespace = namespace
|
||||||
self.log = log
|
self.log = log
|
||||||
self.document_relationships = document_relationships
|
self.document_relationships = document_relationships
|
||||||
self.top_anchor = type('')(uuid4().hex)
|
self.top_anchor = uuid4().hex
|
||||||
self.anchor_map = {}
|
self.anchor_map = {}
|
||||||
self.used_bookmark_names = set()
|
self.used_bookmark_names = set()
|
||||||
self.bmark_id = 0
|
self.bmark_id = 0
|
||||||
|
@ -8,7 +8,7 @@ __copyright__ = '2015, Kovid Goyal <kovid at kovidgoyal.net>'
|
|||||||
from collections import defaultdict
|
from collections import defaultdict
|
||||||
from operator import attrgetter
|
from operator import attrgetter
|
||||||
|
|
||||||
from polyglot.builtins import iteritems, itervalues
|
from polyglot.builtins import iteritems, itervalues, unicode_type
|
||||||
|
|
||||||
LIST_STYLES = frozenset(
|
LIST_STYLES = frozenset(
|
||||||
'disc circle square decimal decimal-leading-zero lower-roman upper-roman'
|
'disc circle square decimal decimal-leading-zero lower-roman upper-roman'
|
||||||
@ -83,7 +83,7 @@ class NumberingDefinition(object):
|
|||||||
|
|
||||||
def serialize(self, parent):
|
def serialize(self, parent):
|
||||||
makeelement = self.namespace.makeelement
|
makeelement = self.namespace.makeelement
|
||||||
an = makeelement(parent, 'w:abstractNum', w_abstractNumId=str(self.num_id))
|
an = makeelement(parent, 'w:abstractNum', w_abstractNumId=unicode_type(self.num_id))
|
||||||
makeelement(an, 'w:multiLevelType', w_val='hybridMultilevel')
|
makeelement(an, 'w:multiLevelType', w_val='hybridMultilevel')
|
||||||
makeelement(an, 'w:name', w_val='List %d' % (self.num_id + 1))
|
makeelement(an, 'w:name', w_val='List %d' % (self.num_id + 1))
|
||||||
for level in self.levels:
|
for level in self.levels:
|
||||||
@ -114,12 +114,12 @@ class Level(object):
|
|||||||
return hash((self.start, self.num_fmt, self.lvl_text))
|
return hash((self.start, self.num_fmt, self.lvl_text))
|
||||||
|
|
||||||
def serialize(self, parent, makeelement):
|
def serialize(self, parent, makeelement):
|
||||||
lvl = makeelement(parent, 'w:lvl', w_ilvl=str(self.ilvl))
|
lvl = makeelement(parent, 'w:lvl', w_ilvl=unicode_type(self.ilvl))
|
||||||
makeelement(lvl, 'w:start', w_val=str(self.start))
|
makeelement(lvl, 'w:start', w_val=unicode_type(self.start))
|
||||||
makeelement(lvl, 'w:numFmt', w_val=self.num_fmt)
|
makeelement(lvl, 'w:numFmt', w_val=self.num_fmt)
|
||||||
makeelement(lvl, 'w:lvlText', w_val=self.lvl_text)
|
makeelement(lvl, 'w:lvlText', w_val=self.lvl_text)
|
||||||
makeelement(lvl, 'w:lvlJc', w_val='left')
|
makeelement(lvl, 'w:lvlJc', w_val='left')
|
||||||
makeelement(makeelement(lvl, 'w:pPr'), 'w:ind', w_hanging='360', w_left=str(1152 + self.ilvl * 360))
|
makeelement(makeelement(lvl, 'w:pPr'), 'w:ind', w_hanging='360', w_left=unicode_type(1152 + self.ilvl * 360))
|
||||||
if self.num_fmt == 'bullet':
|
if self.num_fmt == 'bullet':
|
||||||
ff = {'\uf0b7':'Symbol', '\uf0a7':'Wingdings'}.get(self.lvl_text, 'Courier New')
|
ff = {'\uf0b7':'Symbol', '\uf0a7':'Wingdings'}.get(self.lvl_text, 'Courier New')
|
||||||
makeelement(makeelement(lvl, 'w:rPr'), 'w:rFonts', w_ascii=ff, w_hAnsi=ff, w_hint="default")
|
makeelement(makeelement(lvl, 'w:rPr'), 'w:rFonts', w_ascii=ff, w_hAnsi=ff, w_hint="default")
|
||||||
@ -165,5 +165,5 @@ class ListsManager(object):
|
|||||||
defn.serialize(parent)
|
defn.serialize(parent)
|
||||||
makeelement = self.namespace.makeelement
|
makeelement = self.namespace.makeelement
|
||||||
for defn in self.definitions:
|
for defn in self.definitions:
|
||||||
n = makeelement(parent, 'w:num', w_numId=str(defn.num_id + 1))
|
n = makeelement(parent, 'w:num', w_numId=unicode_type(defn.num_id + 1))
|
||||||
makeelement(n, 'w:abstractNumId', w_val=str(defn.num_id))
|
makeelement(n, 'w:abstractNumId', w_val=unicode_type(defn.num_id))
|
||||||
|
@ -14,7 +14,7 @@ from lxml import etree
|
|||||||
from calibre.ebooks import parse_css_length
|
from calibre.ebooks import parse_css_length
|
||||||
from calibre.ebooks.docx.writer.utils import convert_color, int_or_zero
|
from calibre.ebooks.docx.writer.utils import convert_color, int_or_zero
|
||||||
from calibre.utils.localization import lang_as_iso639_1
|
from calibre.utils.localization import lang_as_iso639_1
|
||||||
from polyglot.builtins import iteritems, filter
|
from polyglot.builtins import iteritems, filter, unicode_type
|
||||||
from tinycss.css21 import CSS21Parser
|
from tinycss.css21 import CSS21Parser
|
||||||
|
|
||||||
css_parser = CSS21Parser()
|
css_parser = CSS21Parser()
|
||||||
@ -76,7 +76,7 @@ class CombinedStyle(object):
|
|||||||
pPr = makeelement(block, 'w:pPr')
|
pPr = makeelement(block, 'w:pPr')
|
||||||
self.bs.serialize_properties(pPr, normal_style.bs)
|
self.bs.serialize_properties(pPr, normal_style.bs)
|
||||||
if self.outline_level is not None:
|
if self.outline_level is not None:
|
||||||
makeelement(pPr, 'w:outlineLvl', w_val=str(self.outline_level + 1))
|
makeelement(pPr, 'w:outlineLvl', w_val=unicode_type(self.outline_level + 1))
|
||||||
rPr = makeelement(block, 'w:rPr')
|
rPr = makeelement(block, 'w:rPr')
|
||||||
self.rs.serialize_properties(rPr, normal_style.rs)
|
self.rs.serialize_properties(rPr, normal_style.rs)
|
||||||
|
|
||||||
@ -109,16 +109,16 @@ class FloatSpec(object):
|
|||||||
|
|
||||||
def serialize(self, block, parent):
|
def serialize(self, block, parent):
|
||||||
if self.is_dropcaps:
|
if self.is_dropcaps:
|
||||||
attrs = dict(w_dropCap='drop', w_lines=str(self.dropcaps_lines), w_wrap='around', w_vAnchor='text', w_hAnchor='text')
|
attrs = dict(w_dropCap='drop', w_lines=unicode_type(self.dropcaps_lines), w_wrap='around', w_vAnchor='text', w_hAnchor='text')
|
||||||
else:
|
else:
|
||||||
attrs = dict(
|
attrs = dict(
|
||||||
w_wrap='around', w_vAnchor='text', w_hAnchor='text', w_xAlign=self.x_align, w_y='1',
|
w_wrap='around', w_vAnchor='text', w_hAnchor='text', w_xAlign=self.x_align, w_y='1',
|
||||||
w_hSpace=str(self.h_space), w_vSpace=str(self.v_space), w_hRule=self.h_rule
|
w_hSpace=unicode_type(self.h_space), w_vSpace=unicode_type(self.v_space), w_hRule=self.h_rule
|
||||||
)
|
)
|
||||||
if self.w is not None:
|
if self.w is not None:
|
||||||
attrs['w_w'] = str(self.w)
|
attrs['w_w'] = unicode_type(self.w)
|
||||||
if self.h is not None:
|
if self.h is not None:
|
||||||
attrs['w_h'] = str(self.h)
|
attrs['w_h'] = unicode_type(self.h)
|
||||||
self.makeelement(parent, 'w:framePr', **attrs)
|
self.makeelement(parent, 'w:framePr', **attrs)
|
||||||
# Margins are already applied by the frame style, so override them to
|
# Margins are already applied by the frame style, so override them to
|
||||||
# be zero on individual blocks
|
# be zero on individual blocks
|
||||||
@ -137,7 +137,7 @@ class FloatSpec(object):
|
|||||||
padding = getattr(self, 'padding_' + edge)
|
padding = getattr(self, 'padding_' + edge)
|
||||||
width = getattr(self, 'border_%s_width' % edge)
|
width = getattr(self, 'border_%s_width' % edge)
|
||||||
bstyle = getattr(self, 'border_%s_style' % edge)
|
bstyle = getattr(self, 'border_%s_style' % edge)
|
||||||
self.makeelement(bdr, 'w:'+edge, w_space=str(padding), w_val=bstyle, w_sz=str(width), w_color=getattr(self, 'border_%s_color' % edge))
|
self.makeelement(bdr, 'w:'+edge, w_space=unicode_type(padding), w_val=bstyle, w_sz=unicode_type(width), w_color=getattr(self, 'border_%s_color' % edge))
|
||||||
|
|
||||||
|
|
||||||
class DOCXStyle(object):
|
class DOCXStyle(object):
|
||||||
@ -233,7 +233,7 @@ class TextStyle(DOCXStyle):
|
|||||||
self.spacing = None
|
self.spacing = None
|
||||||
va = css.first_vertical_align
|
va = css.first_vertical_align
|
||||||
if isinstance(va, numbers.Number):
|
if isinstance(va, numbers.Number):
|
||||||
self.vertical_align = str(int(va * 2))
|
self.vertical_align = unicode_type(int(va * 2))
|
||||||
else:
|
else:
|
||||||
val = {
|
val = {
|
||||||
'top':'superscript', 'text-top':'superscript', 'sup':'superscript', 'super':'superscript',
|
'top':'superscript', 'text-top':'superscript', 'sup':'superscript', 'super':'superscript',
|
||||||
@ -289,9 +289,9 @@ class TextStyle(DOCXStyle):
|
|||||||
w = self.w
|
w = self.w
|
||||||
is_normal_style = self is normal_style
|
is_normal_style = self is normal_style
|
||||||
if is_normal_style or self.padding != normal_style.padding:
|
if is_normal_style or self.padding != normal_style.padding:
|
||||||
bdr.set(w('space'), str(self.padding))
|
bdr.set(w('space'), unicode_type(self.padding))
|
||||||
if is_normal_style or self.border_width != normal_style.border_width:
|
if is_normal_style or self.border_width != normal_style.border_width:
|
||||||
bdr.set(w('sz'), str(self.border_width))
|
bdr.set(w('sz'), unicode_type(self.border_width))
|
||||||
if is_normal_style or self.border_style != normal_style.border_style:
|
if is_normal_style or self.border_style != normal_style.border_style:
|
||||||
bdr.set(w('val'), self.border_style)
|
bdr.set(w('val'), self.border_style)
|
||||||
if is_normal_style or self.border_color != normal_style.border_color:
|
if is_normal_style or self.border_color != normal_style.border_color:
|
||||||
@ -341,7 +341,7 @@ class TextStyle(DOCXStyle):
|
|||||||
if check_attr('shadow'):
|
if check_attr('shadow'):
|
||||||
rPr.append(makeelement(rPr, 'shadow', val=bmap(self.shadow)))
|
rPr.append(makeelement(rPr, 'shadow', val=bmap(self.shadow)))
|
||||||
if check_attr('spacing'):
|
if check_attr('spacing'):
|
||||||
rPr.append(makeelement(rPr, 'spacing', val=str(self.spacing or 0)))
|
rPr.append(makeelement(rPr, 'spacing', val=unicode_type(self.spacing or 0)))
|
||||||
if is_normal_style:
|
if is_normal_style:
|
||||||
rPr.append(makeelement(rPr, 'vertAlign', val=self.vertical_align if self.vertical_align in {'superscript', 'subscript'} else 'baseline'))
|
rPr.append(makeelement(rPr, 'vertAlign', val=self.vertical_align if self.vertical_align in {'superscript', 'subscript'} else 'baseline'))
|
||||||
elif self.vertical_align != normal_style.vertical_align:
|
elif self.vertical_align != normal_style.vertical_align:
|
||||||
@ -379,7 +379,7 @@ class DescendantTextStyle(object):
|
|||||||
for name, attr in (('sz', 'font_size'), ('b', 'bold'), ('i', 'italic')):
|
for name, attr in (('sz', 'font_size'), ('b', 'bold'), ('i', 'italic')):
|
||||||
pval, cval = vals(attr)
|
pval, cval = vals(attr)
|
||||||
if pval != cval:
|
if pval != cval:
|
||||||
val = 'on' if attr in {'bold', 'italic'} else str(cval) # bold, italic are toggle properties
|
val = 'on' if attr in {'bold', 'italic'} else unicode_type(cval) # bold, italic are toggle properties
|
||||||
for suffix in ('', 'Cs'):
|
for suffix in ('', 'Cs'):
|
||||||
add(name + suffix, val=val)
|
add(name + suffix, val=val)
|
||||||
|
|
||||||
@ -400,7 +400,7 @@ class DescendantTextStyle(object):
|
|||||||
if check('shadow'):
|
if check('shadow'):
|
||||||
add('shadow', val='on') # toggle property
|
add('shadow', val='on') # toggle property
|
||||||
if check('spacing'):
|
if check('spacing'):
|
||||||
add('spacing', val=str(child_style.spacing or 0))
|
add('spacing', val=unicode_type(child_style.spacing or 0))
|
||||||
if check('vertical_align'):
|
if check('vertical_align'):
|
||||||
val = child_style.vertical_align
|
val = child_style.vertical_align
|
||||||
if val in {'superscript', 'subscript', 'baseline'}:
|
if val in {'superscript', 'subscript', 'baseline'}:
|
||||||
@ -410,9 +410,9 @@ class DescendantTextStyle(object):
|
|||||||
|
|
||||||
bdr = {}
|
bdr = {}
|
||||||
if check('padding'):
|
if check('padding'):
|
||||||
bdr['space'] = str(child_style.padding)
|
bdr['space'] = unicode_type(child_style.padding)
|
||||||
if check('border_width'):
|
if check('border_width'):
|
||||||
bdr['sz'] = str(child_style.border_width)
|
bdr['sz'] = unicode_type(child_style.border_width)
|
||||||
if check('border_style'):
|
if check('border_style'):
|
||||||
bdr['val'] = child_style.border_style
|
bdr['val'] = child_style.border_style
|
||||||
if check('border_color'):
|
if check('border_color'):
|
||||||
@ -536,14 +536,14 @@ class BlockStyle(DOCXStyle):
|
|||||||
e = bdr.makeelement(w(edge))
|
e = bdr.makeelement(w(edge))
|
||||||
padding = getattr(self, 'padding_' + edge)
|
padding = getattr(self, 'padding_' + edge)
|
||||||
if (self is normal_style and padding > 0) or (padding != getattr(normal_style, 'padding_' + edge)):
|
if (self is normal_style and padding > 0) or (padding != getattr(normal_style, 'padding_' + edge)):
|
||||||
e.set(w('space'), str(padding))
|
e.set(w('space'), unicode_type(padding))
|
||||||
width = getattr(self, 'border_%s_width' % edge)
|
width = getattr(self, 'border_%s_width' % edge)
|
||||||
bstyle = getattr(self, 'border_%s_style' % edge)
|
bstyle = getattr(self, 'border_%s_style' % edge)
|
||||||
if (self is normal_style and width > 0 and bstyle != 'none'
|
if (self is normal_style and width > 0 and bstyle != 'none'
|
||||||
) or width != getattr(normal_style, 'border_%s_width' % edge
|
) or width != getattr(normal_style, 'border_%s_width' % edge
|
||||||
) or bstyle != getattr(normal_style, 'border_%s_style' % edge):
|
) or bstyle != getattr(normal_style, 'border_%s_style' % edge):
|
||||||
e.set(w('val'), bstyle)
|
e.set(w('val'), bstyle)
|
||||||
e.set(w('sz'), str(width))
|
e.set(w('sz'), unicode_type(width))
|
||||||
e.set(w('color'), getattr(self, 'border_%s_color' % edge))
|
e.set(w('color'), getattr(self, 'border_%s_color' % edge))
|
||||||
if e.attrib:
|
if e.attrib:
|
||||||
bdr.append(e)
|
bdr.append(e)
|
||||||
@ -567,15 +567,15 @@ class BlockStyle(DOCXStyle):
|
|||||||
if css_unit in ('em', 'ex'):
|
if css_unit in ('em', 'ex'):
|
||||||
lines = max(0, int(css_val * (50 if css_unit == 'ex' else 100)))
|
lines = max(0, int(css_val * (50 if css_unit == 'ex' else 100)))
|
||||||
if (self is normal_style and lines > 0) or getter(self) != getter(normal_style):
|
if (self is normal_style and lines > 0) or getter(self) != getter(normal_style):
|
||||||
spacing.set(w(attr + 'Lines'), str(lines))
|
spacing.set(w(attr + 'Lines'), unicode_type(lines))
|
||||||
else:
|
else:
|
||||||
getter = attrgetter('margin_' + edge)
|
getter = attrgetter('margin_' + edge)
|
||||||
val = getter(self)
|
val = getter(self)
|
||||||
if (self is normal_style and val > 0) or val != getter(normal_style):
|
if (self is normal_style and val > 0) or val != getter(normal_style):
|
||||||
spacing.set(w(attr), str(val))
|
spacing.set(w(attr), unicode_type(val))
|
||||||
|
|
||||||
if self is normal_style or self.line_height != normal_style.line_height:
|
if self is normal_style or self.line_height != normal_style.line_height:
|
||||||
spacing.set(w('line'), str(self.line_height))
|
spacing.set(w('line'), unicode_type(self.line_height))
|
||||||
spacing.set(w('lineRule'), 'atLeast')
|
spacing.set(w('lineRule'), 'atLeast')
|
||||||
|
|
||||||
if spacing.attrib:
|
if spacing.attrib:
|
||||||
@ -588,31 +588,31 @@ class BlockStyle(DOCXStyle):
|
|||||||
if css_unit in ('em', 'ex'):
|
if css_unit in ('em', 'ex'):
|
||||||
chars = max(0, int(css_val * (50 if css_unit == 'ex' else 100)))
|
chars = max(0, int(css_val * (50 if css_unit == 'ex' else 100)))
|
||||||
if (self is normal_style and chars > 0) or getter(self) != getter(normal_style):
|
if (self is normal_style and chars > 0) or getter(self) != getter(normal_style):
|
||||||
ind.set(w(edge + 'Chars'), str(chars))
|
ind.set(w(edge + 'Chars'), unicode_type(chars))
|
||||||
else:
|
else:
|
||||||
getter = attrgetter('margin_' + edge)
|
getter = attrgetter('margin_' + edge)
|
||||||
val = getter(self)
|
val = getter(self)
|
||||||
if (self is normal_style and val > 0) or val != getter(normal_style):
|
if (self is normal_style and val > 0) or val != getter(normal_style):
|
||||||
ind.set(w(edge), str(val))
|
ind.set(w(edge), unicode_type(val))
|
||||||
ind.set(w(edge + 'Chars'), '0') # This is needed to override any declaration in the parent style
|
ind.set(w(edge + 'Chars'), '0') # This is needed to override any declaration in the parent style
|
||||||
css_val, css_unit = parse_css_length(self.css_text_indent)
|
css_val, css_unit = parse_css_length(self.css_text_indent)
|
||||||
if css_unit in ('em', 'ex'):
|
if css_unit in ('em', 'ex'):
|
||||||
chars = int(css_val * (50 if css_unit == 'ex' else 100))
|
chars = int(css_val * (50 if css_unit == 'ex' else 100))
|
||||||
if css_val >= 0:
|
if css_val >= 0:
|
||||||
if (self is normal_style and chars > 0) or self.css_text_indent != normal_style.css_text_indent:
|
if (self is normal_style and chars > 0) or self.css_text_indent != normal_style.css_text_indent:
|
||||||
ind.set(w('firstLineChars'), str(chars))
|
ind.set(w('firstLineChars'), unicode_type(chars))
|
||||||
else:
|
else:
|
||||||
if (self is normal_style and chars < 0) or self.css_text_indent != normal_style.css_text_indent:
|
if (self is normal_style and chars < 0) or self.css_text_indent != normal_style.css_text_indent:
|
||||||
ind.set(w('hangingChars'), str(abs(chars)))
|
ind.set(w('hangingChars'), unicode_type(abs(chars)))
|
||||||
else:
|
else:
|
||||||
val = self.text_indent
|
val = self.text_indent
|
||||||
if val >= 0:
|
if val >= 0:
|
||||||
if (self is normal_style and val > 0) or self.text_indent != normal_style.text_indent:
|
if (self is normal_style and val > 0) or self.text_indent != normal_style.text_indent:
|
||||||
ind.set(w('firstLine'), str(val))
|
ind.set(w('firstLine'), unicode_type(val))
|
||||||
ind.set(w('firstLineChars'), '0') # This is needed to override any declaration in the parent style
|
ind.set(w('firstLineChars'), '0') # This is needed to override any declaration in the parent style
|
||||||
else:
|
else:
|
||||||
if (self is normal_style and val < 0) or self.text_indent != normal_style.text_indent:
|
if (self is normal_style and val < 0) or self.text_indent != normal_style.text_indent:
|
||||||
ind.set(w('hanging'), str(abs(val)))
|
ind.set(w('hanging'), unicode_type(abs(val)))
|
||||||
ind.set(w('hangingChars'), '0')
|
ind.set(w('hangingChars'), '0')
|
||||||
if ind.attrib:
|
if ind.attrib:
|
||||||
pPr.append(ind)
|
pPr.append(ind)
|
||||||
@ -686,7 +686,7 @@ class StylesManager(object):
|
|||||||
pure_block_styles.add(bs)
|
pure_block_styles.add(bs)
|
||||||
|
|
||||||
self.pure_block_styles = sorted(pure_block_styles, key=block_counts.__getitem__)
|
self.pure_block_styles = sorted(pure_block_styles, key=block_counts.__getitem__)
|
||||||
bnum = len(str(max(1, len(pure_block_styles) - 1)))
|
bnum = len(unicode_type(max(1, len(pure_block_styles) - 1)))
|
||||||
for i, bs in enumerate(self.pure_block_styles):
|
for i, bs in enumerate(self.pure_block_styles):
|
||||||
bs.id = bs.name = '%0{}d Block'.format(bnum) % i
|
bs.id = bs.name = '%0{}d Block'.format(bnum) % i
|
||||||
bs.seq = i
|
bs.seq = i
|
||||||
@ -706,7 +706,7 @@ class StylesManager(object):
|
|||||||
heading_style = styles[-1]
|
heading_style = styles[-1]
|
||||||
heading_style.outline_level = i
|
heading_style.outline_level = i
|
||||||
|
|
||||||
snum = len(str(max(1, len(counts) - 1)))
|
snum = len(unicode_type(max(1, len(counts) - 1)))
|
||||||
heading_styles = []
|
heading_styles = []
|
||||||
for i, (style, count) in enumerate(counts.most_common()):
|
for i, (style, count) in enumerate(counts.most_common()):
|
||||||
if i == 0:
|
if i == 0:
|
||||||
@ -734,7 +734,7 @@ class StylesManager(object):
|
|||||||
if run.descendant_style is None:
|
if run.descendant_style is None:
|
||||||
run.descendant_style = descendant_style_map[ds] = ds
|
run.descendant_style = descendant_style_map[ds] = ds
|
||||||
ds_counts[run.descendant_style] += run.style_weight
|
ds_counts[run.descendant_style] += run.style_weight
|
||||||
rnum = len(str(max(1, len(ds_counts) - 1)))
|
rnum = len(unicode_type(max(1, len(ds_counts) - 1)))
|
||||||
for i, (text_style, count) in enumerate(ds_counts.most_common()):
|
for i, (text_style, count) in enumerate(ds_counts.most_common()):
|
||||||
text_style.id = 'Text%d' % i
|
text_style.id = 'Text%d' % i
|
||||||
text_style.name = '%0{}d Text'.format(rnum) % i
|
text_style.name = '%0{}d Text'.format(rnum) % i
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
from __future__ import with_statement
|
from __future__ import absolute_import, division, print_function, unicode_literals
|
||||||
|
|
||||||
__license__ = 'GPL v3'
|
__license__ = 'GPL v3'
|
||||||
__copyright__ = '2008, Kovid Goyal kovid@kovidgoyal.net'
|
__copyright__ = '2008, Kovid Goyal kovid@kovidgoyal.net'
|
||||||
__docformat__ = 'restructuredtext en'
|
__docformat__ = 'restructuredtext en'
|
||||||
@ -18,7 +19,7 @@ def rules(stylesheets):
|
|||||||
|
|
||||||
|
|
||||||
def simple_container_xml(opf_path, extra_entries=''):
|
def simple_container_xml(opf_path, extra_entries=''):
|
||||||
return u'''\
|
return '''\
|
||||||
<?xml version="1.0"?>
|
<?xml version="1.0"?>
|
||||||
<container version="1.0" xmlns="urn:oasis:names:tc:opendocument:xmlns:container">
|
<container version="1.0" xmlns="urn:oasis:names:tc:opendocument:xmlns:container">
|
||||||
<rootfiles>
|
<rootfiles>
|
||||||
@ -36,7 +37,7 @@ def initialize_container(path_to_container, opf_name='metadata.opf',
|
|||||||
'''
|
'''
|
||||||
rootfiles = ''
|
rootfiles = ''
|
||||||
for path, mimetype, _ in extra_entries:
|
for path, mimetype, _ in extra_entries:
|
||||||
rootfiles += u'<rootfile full-path="{0}" media-type="{1}"/>'.format(
|
rootfiles += '<rootfile full-path="{0}" media-type="{1}"/>'.format(
|
||||||
path, mimetype)
|
path, mimetype)
|
||||||
CONTAINER = simple_container_xml(opf_name, rootfiles).encode('utf-8')
|
CONTAINER = simple_container_xml(opf_name, rootfiles).encode('utf-8')
|
||||||
zf = ZipFile(path_to_container, 'w')
|
zf = ZipFile(path_to_container, 'w')
|
||||||
|
@ -2,7 +2,7 @@
|
|||||||
Add page mapping information to an EPUB book.
|
Add page mapping information to an EPUB book.
|
||||||
'''
|
'''
|
||||||
|
|
||||||
from __future__ import with_statement
|
from __future__ import absolute_import, division, print_function, unicode_literals
|
||||||
|
|
||||||
__license__ = 'GPL v3'
|
__license__ = 'GPL v3'
|
||||||
__copyright__ = '2008, Marshall T. Vandegrift <llasram@gmail.com>'
|
__copyright__ = '2008, Marshall T. Vandegrift <llasram@gmail.com>'
|
||||||
@ -13,6 +13,7 @@ from itertools import count
|
|||||||
from calibre.ebooks.oeb.base import XHTML_NS
|
from calibre.ebooks.oeb.base import XHTML_NS
|
||||||
from calibre.ebooks.oeb.base import OEBBook
|
from calibre.ebooks.oeb.base import OEBBook
|
||||||
from lxml.etree import XPath
|
from lxml.etree import XPath
|
||||||
|
from polyglot.builtins import unicode_type
|
||||||
|
|
||||||
NSMAP = {'h': XHTML_NS, 'html': XHTML_NS, 'xhtml': XHTML_NS}
|
NSMAP = {'h': XHTML_NS, 'html': XHTML_NS, 'xhtml': XHTML_NS}
|
||||||
PAGE_RE = re.compile(r'page', re.IGNORECASE)
|
PAGE_RE = re.compile(r'page', re.IGNORECASE)
|
||||||
@ -32,7 +33,7 @@ def filter_name(name):
|
|||||||
def build_name_for(expr):
|
def build_name_for(expr):
|
||||||
if not expr:
|
if not expr:
|
||||||
counter = count(1)
|
counter = count(1)
|
||||||
return lambda elem: str(next(counter))
|
return lambda elem: unicode_type(next(counter))
|
||||||
selector = XPath(expr, namespaces=NSMAP)
|
selector = XPath(expr, namespaces=NSMAP)
|
||||||
|
|
||||||
def name_for(elem):
|
def name_for(elem):
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
#!/usr/bin/env python2
|
#!/usr/bin/env python2
|
||||||
# vim:fileencoding=UTF-8:ts=4:sw=4:sta:et:sts=4:ai
|
# vim:fileencoding=UTF-8:ts=4:sw=4:sta:et:sts=4:ai
|
||||||
|
from __future__ import absolute_import, division, print_function, unicode_literals
|
||||||
|
|
||||||
__license__ = 'GPL v3'
|
__license__ = 'GPL v3'
|
||||||
__copyright__ = '2010, Kovid Goyal <kovid@kovidgoyal.net>'
|
__copyright__ = '2010, Kovid Goyal <kovid@kovidgoyal.net>'
|
||||||
@ -13,7 +14,7 @@ from calibre import strftime, prepare_string_for_xml as xml
|
|||||||
from calibre.utils.date import parse_date
|
from calibre.utils.date import parse_date
|
||||||
from polyglot.builtins import unicode_type, filter
|
from polyglot.builtins import unicode_type, filter
|
||||||
|
|
||||||
SONY_METADATA = u'''\
|
SONY_METADATA = '''\
|
||||||
<?xml version="1.0" encoding="utf-8"?>
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
|
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
|
||||||
xmlns:dcterms="http://purl.org/dc/terms/"
|
xmlns:dcterms="http://purl.org/dc/terms/"
|
||||||
@ -32,7 +33,7 @@ SONY_METADATA = u'''\
|
|||||||
</rdf:RDF>
|
</rdf:RDF>
|
||||||
'''
|
'''
|
||||||
|
|
||||||
SONY_ATOM = u'''\
|
SONY_ATOM = '''\
|
||||||
<?xml version="1.0" encoding="utf-8" ?>
|
<?xml version="1.0" encoding="utf-8" ?>
|
||||||
<feed xmlns="http://www.w3.org/2005/Atom"
|
<feed xmlns="http://www.w3.org/2005/Atom"
|
||||||
xmlns:dc="http://purl.org/dc/elements/1.1/"
|
xmlns:dc="http://purl.org/dc/elements/1.1/"
|
||||||
@ -49,7 +50,7 @@ SONY_ATOM = u'''\
|
|||||||
</feed>
|
</feed>
|
||||||
'''
|
'''
|
||||||
|
|
||||||
SONY_ATOM_SECTION = u'''\
|
SONY_ATOM_SECTION = '''\
|
||||||
<entry rdf:ID="{title}">
|
<entry rdf:ID="{title}">
|
||||||
<title>{title}</title>
|
<title>{title}</title>
|
||||||
<link href="{href}"/>
|
<link href="{href}"/>
|
||||||
@ -63,7 +64,7 @@ SONY_ATOM_SECTION = u'''\
|
|||||||
</entry>
|
</entry>
|
||||||
'''
|
'''
|
||||||
|
|
||||||
SONY_ATOM_ENTRY = u'''\
|
SONY_ATOM_ENTRY = '''\
|
||||||
<entry>
|
<entry>
|
||||||
<title>{title}</title>
|
<title>{title}</title>
|
||||||
<author><name>{author}</name></author>
|
<author><name>{author}</name></author>
|
||||||
@ -86,7 +87,7 @@ def sony_metadata(oeb):
|
|||||||
publisher = __appname__ + ' ' + __version__
|
publisher = __appname__ + ' ' + __version__
|
||||||
try:
|
try:
|
||||||
pt = unicode_type(oeb.metadata.publication_type[0])
|
pt = unicode_type(oeb.metadata.publication_type[0])
|
||||||
short_title = u':'.join(pt.split(':')[2:])
|
short_title = ':'.join(pt.split(':')[2:])
|
||||||
except:
|
except:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
@ -116,7 +117,7 @@ def sony_metadata(oeb):
|
|||||||
try:
|
try:
|
||||||
base_id = unicode_type(list(filter(cal_id, m.identifier))[0])
|
base_id = unicode_type(list(filter(cal_id, m.identifier))[0])
|
||||||
except:
|
except:
|
||||||
base_id = str(uuid4())
|
base_id = unicode_type(uuid4())
|
||||||
|
|
||||||
toc = oeb.toc
|
toc = oeb.toc
|
||||||
|
|
||||||
@ -144,7 +145,7 @@ def sony_metadata(oeb):
|
|||||||
d = 1
|
d = 1
|
||||||
bsectitle = sectitle
|
bsectitle = sectitle
|
||||||
while sectitle in seen_titles:
|
while sectitle in seen_titles:
|
||||||
sectitle = bsectitle + ' ' + str(d)
|
sectitle = bsectitle + ' ' + unicode_type(d)
|
||||||
d += 1
|
d += 1
|
||||||
seen_titles.add(sectitle)
|
seen_titles.add(sectitle)
|
||||||
sectitle = xml(sectitle, True)
|
sectitle = xml(sectitle, True)
|
||||||
@ -163,7 +164,7 @@ def sony_metadata(oeb):
|
|||||||
btitle = atitle
|
btitle = atitle
|
||||||
d = 1
|
d = 1
|
||||||
while atitle in seen_titles:
|
while atitle in seen_titles:
|
||||||
atitle = btitle + ' ' + str(d)
|
atitle = btitle + ' ' + unicode_type(d)
|
||||||
d += 1
|
d += 1
|
||||||
|
|
||||||
auth = article.author if article.author else ''
|
auth = article.author if article.author else ''
|
||||||
@ -180,7 +181,7 @@ def sony_metadata(oeb):
|
|||||||
short_title=short_title,
|
short_title=short_title,
|
||||||
section_title=sectitle,
|
section_title=sectitle,
|
||||||
href=article.href,
|
href=article.href,
|
||||||
word_count=str(1),
|
word_count=unicode_type(1),
|
||||||
id=xml(base_id)+'/'+secid+'/'+aid
|
id=xml(base_id)+'/'+secid+'/'+aid
|
||||||
))
|
))
|
||||||
|
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
|
from __future__ import absolute_import, division, print_function, unicode_literals
|
||||||
|
|
||||||
__license__ = 'GPL 3'
|
__license__ = 'GPL 3'
|
||||||
__copyright__ = '2009, John Schember <john@nachtimwald.com>'
|
__copyright__ = '2009, John Schember <john@nachtimwald.com>'
|
||||||
@ -62,12 +63,12 @@ class FB2MLizer(object):
|
|||||||
output.append(self.get_text())
|
output.append(self.get_text())
|
||||||
output.append(self.fb2mlize_images())
|
output.append(self.fb2mlize_images())
|
||||||
output.append(self.fb2_footer())
|
output.append(self.fb2_footer())
|
||||||
output = self.clean_text(u''.join(output))
|
output = self.clean_text(''.join(output))
|
||||||
|
|
||||||
if self.opts.pretty_print:
|
if self.opts.pretty_print:
|
||||||
return u'<?xml version="1.0" encoding="UTF-8"?>\n%s' % etree.tostring(etree.fromstring(output), encoding='unicode', pretty_print=True)
|
return '<?xml version="1.0" encoding="UTF-8"?>\n%s' % etree.tostring(etree.fromstring(output), encoding='unicode', pretty_print=True)
|
||||||
else:
|
else:
|
||||||
return u'<?xml version="1.0" encoding="UTF-8"?>' + output
|
return '<?xml version="1.0" encoding="UTF-8"?>' + output
|
||||||
|
|
||||||
def clean_text(self, text):
|
def clean_text(self, text):
|
||||||
# Condense empty paragraphs into a line break.
|
# Condense empty paragraphs into a line break.
|
||||||
@ -116,11 +117,11 @@ class FB2MLizer(object):
|
|||||||
metadata['cover'] = self.get_cover()
|
metadata['cover'] = self.get_cover()
|
||||||
metadata['genre'] = self.opts.fb2_genre
|
metadata['genre'] = self.opts.fb2_genre
|
||||||
|
|
||||||
metadata['author'] = u''
|
metadata['author'] = ''
|
||||||
for auth in self.oeb_book.metadata.creator:
|
for auth in self.oeb_book.metadata.creator:
|
||||||
author_first = u''
|
author_first = ''
|
||||||
author_middle = u''
|
author_middle = ''
|
||||||
author_last = u''
|
author_last = ''
|
||||||
author_parts = auth.value.split(' ')
|
author_parts = auth.value.split(' ')
|
||||||
if len(author_parts) == 1:
|
if len(author_parts) == 1:
|
||||||
author_last = author_parts[0]
|
author_last = author_parts[0]
|
||||||
@ -138,22 +139,22 @@ class FB2MLizer(object):
|
|||||||
metadata['author'] += '<last-name>%s</last-name>' % prepare_string_for_xml(author_last)
|
metadata['author'] += '<last-name>%s</last-name>' % prepare_string_for_xml(author_last)
|
||||||
metadata['author'] += '</author>'
|
metadata['author'] += '</author>'
|
||||||
if not metadata['author']:
|
if not metadata['author']:
|
||||||
metadata['author'] = u'<author><first-name></first-name><last-name></last-name></author>'
|
metadata['author'] = '<author><first-name></first-name><last-name></last-name></author>'
|
||||||
|
|
||||||
metadata['keywords'] = u''
|
metadata['keywords'] = ''
|
||||||
tags = list(map(unicode_type, self.oeb_book.metadata.subject))
|
tags = list(map(unicode_type, self.oeb_book.metadata.subject))
|
||||||
if tags:
|
if tags:
|
||||||
tags = ', '.join(prepare_string_for_xml(x) for x in tags)
|
tags = ', '.join(prepare_string_for_xml(x) for x in tags)
|
||||||
metadata['keywords'] = '<keywords>%s</keywords>'%tags
|
metadata['keywords'] = '<keywords>%s</keywords>'%tags
|
||||||
|
|
||||||
metadata['sequence'] = u''
|
metadata['sequence'] = ''
|
||||||
if self.oeb_book.metadata.series:
|
if self.oeb_book.metadata.series:
|
||||||
index = '1'
|
index = '1'
|
||||||
if self.oeb_book.metadata.series_index:
|
if self.oeb_book.metadata.series_index:
|
||||||
index = self.oeb_book.metadata.series_index[0]
|
index = self.oeb_book.metadata.series_index[0]
|
||||||
metadata['sequence'] = u'<sequence name="%s" number="%s" />' % (prepare_string_for_xml(u'%s' % self.oeb_book.metadata.series[0]), index)
|
metadata['sequence'] = '<sequence name="%s" number="%s" />' % (prepare_string_for_xml('%s' % self.oeb_book.metadata.series[0]), index)
|
||||||
|
|
||||||
year = publisher = isbn = u''
|
year = publisher = isbn = ''
|
||||||
identifiers = self.oeb_book.metadata['identifier']
|
identifiers = self.oeb_book.metadata['identifier']
|
||||||
for x in identifiers:
|
for x in identifiers:
|
||||||
if x.get(OPF('scheme'), None).lower() == 'uuid' or unicode_type(x).startswith('urn:uuid:'):
|
if x.get(OPF('scheme'), None).lower() == 'uuid' or unicode_type(x).startswith('urn:uuid:'):
|
||||||
@ -161,7 +162,7 @@ class FB2MLizer(object):
|
|||||||
break
|
break
|
||||||
if metadata['id'] is None:
|
if metadata['id'] is None:
|
||||||
self.log.warn('No UUID identifier found')
|
self.log.warn('No UUID identifier found')
|
||||||
metadata['id'] = str(uuid.uuid4())
|
metadata['id'] = unicode_type(uuid.uuid4())
|
||||||
|
|
||||||
try:
|
try:
|
||||||
date = self.oeb_book.metadata['date'][0]
|
date = self.oeb_book.metadata['date'][0]
|
||||||
@ -194,7 +195,7 @@ class FB2MLizer(object):
|
|||||||
from calibre.utils.html2text import html2text
|
from calibre.utils.html2text import html2text
|
||||||
metadata['comments'] = '<annotation>{}</annotation>'.format(prepare_string_for_xml(html2text(comments.value.strip())))
|
metadata['comments'] = '<annotation>{}</annotation>'.format(prepare_string_for_xml(html2text(comments.value.strip())))
|
||||||
|
|
||||||
return textwrap.dedent(u'''
|
return textwrap.dedent('''
|
||||||
<FictionBook xmlns="http://www.gribuser.ru/xml/fictionbook/2.0" xmlns:xlink="http://www.w3.org/1999/xlink">
|
<FictionBook xmlns="http://www.gribuser.ru/xml/fictionbook/2.0" xmlns:xlink="http://www.w3.org/1999/xlink">
|
||||||
<description>
|
<description>
|
||||||
<title-info>
|
<title-info>
|
||||||
@ -222,7 +223,7 @@ class FB2MLizer(object):
|
|||||||
</description>\n''') % metadata
|
</description>\n''') % metadata
|
||||||
|
|
||||||
def fb2_footer(self):
|
def fb2_footer(self):
|
||||||
return u'\n</FictionBook>'
|
return '\n</FictionBook>'
|
||||||
|
|
||||||
def get_cover(self):
|
def get_cover(self):
|
||||||
from calibre.ebooks.oeb.base import OEB_RASTER_IMAGES
|
from calibre.ebooks.oeb.base import OEB_RASTER_IMAGES
|
||||||
@ -255,9 +256,9 @@ class FB2MLizer(object):
|
|||||||
if cover_href in self.oeb_book.manifest.hrefs.keys():
|
if cover_href in self.oeb_book.manifest.hrefs.keys():
|
||||||
if cover_href not in self.image_hrefs.keys():
|
if cover_href not in self.image_hrefs.keys():
|
||||||
self.image_hrefs[cover_href] = '_%s.jpg' % len(self.image_hrefs.keys())
|
self.image_hrefs[cover_href] = '_%s.jpg' % len(self.image_hrefs.keys())
|
||||||
return u'<coverpage><image xlink:href="#%s" /></coverpage>' % self.image_hrefs[cover_href]
|
return '<coverpage><image xlink:href="#%s" /></coverpage>' % self.image_hrefs[cover_href]
|
||||||
|
|
||||||
return u''
|
return ''
|
||||||
|
|
||||||
def get_text(self):
|
def get_text(self):
|
||||||
from calibre.ebooks.oeb.base import XHTML
|
from calibre.ebooks.oeb.base import XHTML
|
||||||
|
@ -1,31 +1,7 @@
|
|||||||
#!/usr/bin/env python2
|
#!/usr/bin/env python2
|
||||||
# vim:fileencoding=UTF-8:ts=4:sw=4:sta:et:sts=4:ai
|
# vim:fileencoding=UTF-8:ts=4:sw=4:sta:et:sts=4:ai
|
||||||
from __future__ import with_statement
|
from __future__ import absolute_import, division, print_function, unicode_literals
|
||||||
|
|
||||||
__license__ = 'GPL v3'
|
__license__ = 'GPL v3'
|
||||||
__copyright__ = '2009, Kovid Goyal <kovid@kovidgoyal.net>'
|
__copyright__ = '2009, Kovid Goyal <kovid@kovidgoyal.net>'
|
||||||
__docformat__ = 'restructuredtext en'
|
__docformat__ = 'restructuredtext en'
|
||||||
|
|
||||||
import re
|
|
||||||
|
|
||||||
|
|
||||||
def tostring(root, strip_comments=False, pretty_print=False):
|
|
||||||
'''
|
|
||||||
Serialize processed XHTML.
|
|
||||||
'''
|
|
||||||
from lxml.etree import tostring as _tostring
|
|
||||||
|
|
||||||
root.set('xmlns', 'http://www.w3.org/1999/xhtml')
|
|
||||||
root.set('{http://www.w3.org/1999/xhtml}xlink', 'http://www.w3.org/1999/xlink')
|
|
||||||
for x in root.iter():
|
|
||||||
if hasattr(x.tag, 'rpartition') and x.tag.rpartition('}')[-1].lower() == 'svg':
|
|
||||||
x.set('xmlns', 'http://www.w3.org/2000/svg')
|
|
||||||
|
|
||||||
ans = _tostring(root, encoding='utf-8', pretty_print=pretty_print)
|
|
||||||
if strip_comments:
|
|
||||||
ans = re.compile(r'<!--.*?-->', re.DOTALL).sub('', ans)
|
|
||||||
ans = '<?xml version="1.0" encoding="utf-8" ?>\n'+ans
|
|
||||||
|
|
||||||
return ans
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
#!/usr/bin/env python2
|
#!/usr/bin/env python2
|
||||||
# vim:fileencoding=UTF-8:ts=4:sw=4:sta:et:sts=4:ai
|
# vim:fileencoding=UTF-8:ts=4:sw=4:sta:et:sts=4:ai
|
||||||
from __future__ import with_statement
|
from __future__ import absolute_import, division, print_function, unicode_literals
|
||||||
from __future__ import print_function
|
|
||||||
|
|
||||||
__license__ = 'GPL v3'
|
__license__ = 'GPL v3'
|
||||||
__copyright__ = '2009, Kovid Goyal <kovid@kovidgoyal.net>'
|
__copyright__ = '2009, Kovid Goyal <kovid@kovidgoyal.net>'
|
||||||
@ -18,7 +17,7 @@ from calibre.ebooks.oeb.base import urlunquote
|
|||||||
from calibre.ebooks.chardet import detect_xml_encoding
|
from calibre.ebooks.chardet import detect_xml_encoding
|
||||||
from calibre.constants import iswindows
|
from calibre.constants import iswindows
|
||||||
from calibre import unicode_path, as_unicode, replace_entities
|
from calibre import unicode_path, as_unicode, replace_entities
|
||||||
from polyglot.builtins import unicode_type
|
from polyglot.builtins import is_py3, unicode_type
|
||||||
from polyglot.urllib import urlparse, urlunparse
|
from polyglot.urllib import urlparse, urlunparse
|
||||||
|
|
||||||
|
|
||||||
@ -68,6 +67,9 @@ class Link(object):
|
|||||||
def __str__(self):
|
def __str__(self):
|
||||||
return u'Link: %s --> %s'%(self.url, self.path)
|
return u'Link: %s --> %s'%(self.url, self.path)
|
||||||
|
|
||||||
|
if not is_py3:
|
||||||
|
__unicode__ = __str__
|
||||||
|
|
||||||
|
|
||||||
class IgnoreFile(Exception):
|
class IgnoreFile(Exception):
|
||||||
|
|
||||||
@ -149,10 +151,10 @@ class HTMLFile(object):
|
|||||||
return hash(self.path)
|
return hash(self.path)
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return u'HTMLFile:%d:%s:%s'%(self.level, 'b' if self.is_binary else 'a', self.path)
|
return 'HTMLFile:%d:%s:%s'%(self.level, 'b' if self.is_binary else 'a', self.path)
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return str(self)
|
return unicode_type(self)
|
||||||
|
|
||||||
def find_links(self, src):
|
def find_links(self, src):
|
||||||
for match in self.LINK_PAT.finditer(src):
|
for match in self.LINK_PAT.finditer(src):
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
from __future__ import with_statement
|
from __future__ import absolute_import, division, print_function, unicode_literals
|
||||||
|
|
||||||
__license__ = 'GPL 3'
|
__license__ = 'GPL 3'
|
||||||
__copyright__ = '2010, Fabian Grassl <fg@jusmeum.de>'
|
__copyright__ = '2010, Fabian Grassl <fg@jusmeum.de>'
|
||||||
__docformat__ = 'restructuredtext en'
|
__docformat__ = 'restructuredtext en'
|
||||||
|
@ -1,9 +1,7 @@
|
|||||||
#!/usr/bin/env python2
|
#!/usr/bin/env python2
|
||||||
# vim:fileencoding=UTF-8:ts=4:sw=4:sta:et:sts=4:ai
|
# vim:fileencoding=UTF-8:ts=4:sw=4:sta:et:sts=4:ai
|
||||||
|
from __future__ import absolute_import, division, print_function, unicode_literals
|
||||||
|
|
||||||
__license__ = 'GPL v3'
|
__license__ = 'GPL v3'
|
||||||
__copyright__ = '2010, Kovid Goyal <kovid@kovidgoyal.net>'
|
__copyright__ = '2010, Kovid Goyal <kovid@kovidgoyal.net>'
|
||||||
__docformat__ = 'restructuredtext en'
|
__docformat__ = 'restructuredtext en'
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -1,3 +1,5 @@
|
|||||||
|
from __future__ import absolute_import, division, print_function, unicode_literals
|
||||||
|
|
||||||
__license__ = 'GPL v3'
|
__license__ = 'GPL v3'
|
||||||
__copyright__ = '2008, Marshall T. Vandegrift <llasram@gmail.com>'
|
__copyright__ = '2008, Marshall T. Vandegrift <llasram@gmail.com>'
|
||||||
|
|
||||||
|
@ -1,7 +1,8 @@
|
|||||||
|
from __future__ import absolute_import, division, print_function, unicode_literals
|
||||||
|
|
||||||
'''
|
'''
|
||||||
LZX compression/decompression wrapper.
|
LZX compression/decompression wrapper.
|
||||||
'''
|
'''
|
||||||
from __future__ import with_statement
|
|
||||||
|
|
||||||
__license__ = 'GPL v3'
|
__license__ = 'GPL v3'
|
||||||
__copyright__ = '2008, Marshall T. Vandegrift <llasram@gmail.com>'
|
__copyright__ = '2008, Marshall T. Vandegrift <llasram@gmail.com>'
|
||||||
|
@ -1,3 +1,5 @@
|
|||||||
|
from __future__ import absolute_import, division, print_function, unicode_literals
|
||||||
|
|
||||||
__license__ = 'GPL v3'
|
__license__ = 'GPL v3'
|
||||||
__copyright__ = '2008, Marshall T. Vandegrift <llasram@gmail.com>'
|
__copyright__ = '2008, Marshall T. Vandegrift <llasram@gmail.com>'
|
||||||
|
|
||||||
|
@ -1,3 +1,5 @@
|
|||||||
|
from __future__ import absolute_import, division, print_function, unicode_literals
|
||||||
|
|
||||||
__license__ = 'GPL v3'
|
__license__ = 'GPL v3'
|
||||||
__copyright__ = '2008, Marshall T. Vandegrift <llasram@gmail.com>'
|
__copyright__ = '2008, Marshall T. Vandegrift <llasram@gmail.com>'
|
||||||
|
|
||||||
|
@ -1,3 +1,5 @@
|
|||||||
|
from __future__ import absolute_import, division, print_function, unicode_literals
|
||||||
|
|
||||||
__license__ = 'GPL v3'
|
__license__ = 'GPL v3'
|
||||||
__copyright__ = '2008, Marshall T. Vandegrift <llasram@gmail.com>'
|
__copyright__ = '2008, Marshall T. Vandegrift <llasram@gmail.com>'
|
||||||
|
|
||||||
|
@ -1,3 +1,5 @@
|
|||||||
|
from __future__ import absolute_import, division, print_function, unicode_literals
|
||||||
|
|
||||||
__license__ = 'GPL v3'
|
__license__ = 'GPL v3'
|
||||||
__copyright__ = '2008, Kovid Goyal <kovid at kovidgoyal.net>'
|
__copyright__ = '2008, Kovid Goyal <kovid at kovidgoyal.net>'
|
||||||
|
|
||||||
|
@ -1,3 +1,5 @@
|
|||||||
|
from __future__ import absolute_import, division, print_function, unicode_literals
|
||||||
|
|
||||||
__license__ = 'GPL v3'
|
__license__ = 'GPL v3'
|
||||||
__copyright__ = '2008, Kovid Goyal <kovid at kovidgoyal.net>'
|
__copyright__ = '2008, Kovid Goyal <kovid at kovidgoyal.net>'
|
||||||
''''''
|
''''''
|
||||||
@ -79,47 +81,49 @@ class LRFDocument(LRFMetaFile):
|
|||||||
|
|
||||||
def write_files(self):
|
def write_files(self):
|
||||||
for obj in chain(itervalues(self.image_map), itervalues(self.font_map)):
|
for obj in chain(itervalues(self.image_map), itervalues(self.font_map)):
|
||||||
open(obj.file, 'wb').write(obj.stream)
|
with open(obj.file, 'wb') as f:
|
||||||
|
f.write(obj.stream)
|
||||||
|
|
||||||
def to_xml(self, write_files=True):
|
def to_xml(self, write_files=True):
|
||||||
bookinfo = u'<BookInformation>\n<Info version="1.1">\n<BookInfo>\n'
|
bookinfo = '<BookInformation>\n<Info version="1.1">\n<BookInfo>\n'
|
||||||
bookinfo += u'<Title reading="%s">%s</Title>\n'%(self.metadata.title_reading, self.metadata.title)
|
bookinfo += '<Title reading="%s">%s</Title>\n'%(self.metadata.title_reading, self.metadata.title)
|
||||||
bookinfo += u'<Author reading="%s">%s</Author>\n'%(self.metadata.author_reading, self.metadata.author)
|
bookinfo += '<Author reading="%s">%s</Author>\n'%(self.metadata.author_reading, self.metadata.author)
|
||||||
bookinfo += u'<BookID>%s</BookID>\n'%(self.metadata.book_id,)
|
bookinfo += '<BookID>%s</BookID>\n'%(self.metadata.book_id,)
|
||||||
bookinfo += u'<Publisher reading="">%s</Publisher>\n'%(self.metadata.publisher,)
|
bookinfo += '<Publisher reading="">%s</Publisher>\n'%(self.metadata.publisher,)
|
||||||
bookinfo += u'<Label reading="">%s</Label>\n'%(self.metadata.label,)
|
bookinfo += '<Label reading="">%s</Label>\n'%(self.metadata.label,)
|
||||||
bookinfo += u'<Category reading="">%s</Category>\n'%(self.metadata.category,)
|
bookinfo += '<Category reading="">%s</Category>\n'%(self.metadata.category,)
|
||||||
bookinfo += u'<Classification reading="">%s</Classification>\n'%(self.metadata.classification,)
|
bookinfo += '<Classification reading="">%s</Classification>\n'%(self.metadata.classification,)
|
||||||
bookinfo += u'<FreeText reading="">%s</FreeText>\n</BookInfo>\n<DocInfo>\n'%(self.metadata.free_text,)
|
bookinfo += '<FreeText reading="">%s</FreeText>\n</BookInfo>\n<DocInfo>\n'%(self.metadata.free_text,)
|
||||||
th = self.doc_info.thumbnail
|
th = self.doc_info.thumbnail
|
||||||
if th:
|
if th:
|
||||||
prefix = ascii_filename(self.metadata.title)
|
prefix = ascii_filename(self.metadata.title)
|
||||||
bookinfo += u'<CThumbnail file="%s" />\n'%(prefix+'_thumbnail.'+self.doc_info.thumbnail_extension,)
|
bookinfo += '<CThumbnail file="%s" />\n'%(prefix+'_thumbnail.'+self.doc_info.thumbnail_extension,)
|
||||||
if write_files:
|
if write_files:
|
||||||
open(prefix+'_thumbnail.'+self.doc_info.thumbnail_extension, 'wb').write(th)
|
with open(prefix+'_thumbnail.'+self.doc_info.thumbnail_extension, 'wb') as f:
|
||||||
bookinfo += u'<Language reading="">%s</Language>\n'%(self.doc_info.language,)
|
f.write(th)
|
||||||
bookinfo += u'<Creator reading="">%s</Creator>\n'%(self.doc_info.creator,)
|
bookinfo += '<Language reading="">%s</Language>\n'%(self.doc_info.language,)
|
||||||
bookinfo += u'<Producer reading="">%s</Producer>\n'%(self.doc_info.producer,)
|
bookinfo += '<Creator reading="">%s</Creator>\n'%(self.doc_info.creator,)
|
||||||
bookinfo += u'<SumPage>%s</SumPage>\n</DocInfo>\n</Info>\n%s</BookInformation>\n'%(self.doc_info.page,self.toc)
|
bookinfo += '<Producer reading="">%s</Producer>\n'%(self.doc_info.producer,)
|
||||||
pages = u''
|
bookinfo += '<SumPage>%s</SumPage>\n</DocInfo>\n</Info>\n%s</BookInformation>\n'%(self.doc_info.page,self.toc)
|
||||||
|
pages = ''
|
||||||
done_main = False
|
done_main = False
|
||||||
pt_id = -1
|
pt_id = -1
|
||||||
for page_tree in self:
|
for page_tree in self:
|
||||||
if not done_main:
|
if not done_main:
|
||||||
done_main = True
|
done_main = True
|
||||||
pages += u'<Main>\n'
|
pages += '<Main>\n'
|
||||||
close = u'</Main>\n'
|
close = '</Main>\n'
|
||||||
pt_id = page_tree.id
|
pt_id = page_tree.id
|
||||||
else:
|
else:
|
||||||
pages += u'<PageTree objid="%d">\n'%(page_tree.id,)
|
pages += '<PageTree objid="%d">\n'%(page_tree.id,)
|
||||||
close = u'</PageTree>\n'
|
close = '</PageTree>\n'
|
||||||
for page in page_tree:
|
for page in page_tree:
|
||||||
pages += unicode_type(page)
|
pages += unicode_type(page)
|
||||||
pages += close
|
pages += close
|
||||||
traversed_objects = [int(i) for i in re.findall(r'objid="(\w+)"', pages)] + [pt_id]
|
traversed_objects = [int(i) for i in re.findall(r'objid="(\w+)"', pages)] + [pt_id]
|
||||||
|
|
||||||
objects = u'\n<Objects>\n'
|
objects = '\n<Objects>\n'
|
||||||
styles = u'\n<Style>\n'
|
styles = '\n<Style>\n'
|
||||||
for obj in self.objects:
|
for obj in self.objects:
|
||||||
obj = self.objects[obj]
|
obj = self.objects[obj]
|
||||||
if obj.id in traversed_objects:
|
if obj.id in traversed_objects:
|
||||||
@ -159,13 +163,13 @@ def main(args=sys.argv, logger=None):
|
|||||||
return 1
|
return 1
|
||||||
if opts.out is None:
|
if opts.out is None:
|
||||||
opts.out = os.path.join(os.path.dirname(args[1]), os.path.splitext(os.path.basename(args[1]))[0]+".lrs")
|
opts.out = os.path.join(os.path.dirname(args[1]), os.path.splitext(os.path.basename(args[1]))[0]+".lrs")
|
||||||
o = codecs.open(os.path.abspath(os.path.expanduser(opts.out)), 'wb', 'utf-8')
|
|
||||||
o.write(u'<?xml version="1.0" encoding="UTF-8"?>\n')
|
|
||||||
logger.info(_('Parsing LRF...'))
|
logger.info(_('Parsing LRF...'))
|
||||||
d = LRFDocument(open(args[1], 'rb'))
|
d = LRFDocument(open(args[1], 'rb'))
|
||||||
d.parse()
|
d.parse()
|
||||||
logger.info(_('Creating XML...'))
|
logger.info(_('Creating XML...'))
|
||||||
o.write(d.to_xml(write_files=opts.output_resources))
|
with codecs.open(os.path.abspath(os.path.expanduser(opts.out)), 'wb', 'utf-8') as f:
|
||||||
|
f.write('<?xml version="1.0" encoding="UTF-8"?>\n')
|
||||||
|
f.write(d.to_xml(write_files=opts.output_resources))
|
||||||
logger.info(_('LRS written to ')+opts.out)
|
logger.info(_('LRS written to ')+opts.out)
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
from __future__ import print_function
|
from __future__ import absolute_import, division, print_function, unicode_literals
|
||||||
|
|
||||||
__license__ = 'GPL v3'
|
__license__ = 'GPL v3'
|
||||||
__copyright__ = '2008, Kovid Goyal <kovid at kovidgoyal.net>'
|
__copyright__ = '2008, Kovid Goyal <kovid at kovidgoyal.net>'
|
||||||
|
|
||||||
@ -9,7 +10,7 @@ Just create an L{LRFMetaFile} object and use its properties
|
|||||||
to get and set meta information. For example:
|
to get and set meta information. For example:
|
||||||
|
|
||||||
>>> lrf = LRFMetaFile("mybook.lrf")
|
>>> lrf = LRFMetaFile("mybook.lrf")
|
||||||
>>> print lrf.title, lrf.author
|
>>> print(lrf.title, lrf.author)
|
||||||
>>> lrf.category = "History"
|
>>> lrf.category = "History"
|
||||||
"""
|
"""
|
||||||
|
|
||||||
@ -50,8 +51,8 @@ class field(object):
|
|||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
typ = {DWORD: 'unsigned int', 'QWORD': 'unsigned long long', BYTE: 'unsigned char', WORD: 'unsigned short'}.get(self._fmt, '')
|
typ = {DWORD: 'unsigned int', 'QWORD': 'unsigned long long', BYTE: 'unsigned char', WORD: 'unsigned short'}.get(self._fmt, '')
|
||||||
return "An " + typ + " stored in " + \
|
return "An " + typ + " stored in " + \
|
||||||
str(struct.calcsize(self._fmt)) + \
|
unicode_type(struct.calcsize(self._fmt)) + \
|
||||||
" bytes starting at byte " + str(self._start)
|
" bytes starting at byte " + unicode_type(self._start)
|
||||||
|
|
||||||
|
|
||||||
class versioned_field(field):
|
class versioned_field(field):
|
||||||
@ -92,20 +93,20 @@ class fixed_stringfield(object):
|
|||||||
self._start = start
|
self._start = start
|
||||||
|
|
||||||
def __get__(self, obj, typ=None):
|
def __get__(self, obj, typ=None):
|
||||||
length = str(self._length)
|
length = unicode_type(self._length)
|
||||||
return obj.unpack(start=self._start, fmt="<"+length+"s")[0]
|
return obj.unpack(start=self._start, fmt="<"+length+"s")[0]
|
||||||
|
|
||||||
def __set__(self, obj, val):
|
def __set__(self, obj, val):
|
||||||
if val.__class__.__name__ != 'str':
|
if not isinstance(val, unicode_type):
|
||||||
val = str(val)
|
val = unicode_type(val)
|
||||||
if len(val) != self._length:
|
if len(val) != self._length:
|
||||||
raise LRFException("Trying to set fixed_stringfield with a " +
|
raise LRFException("Trying to set fixed_stringfield with a " +
|
||||||
"string of incorrect length")
|
"string of incorrect length")
|
||||||
obj.pack(val, start=self._start, fmt="<"+str(len(val))+"s")
|
obj.pack(val, start=self._start, fmt="<"+unicode_type(len(val))+"s")
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return "A string of length " + str(self._length) + \
|
return "A string of length " + unicode_type(self._length) + \
|
||||||
" starting at byte " + str(self._start)
|
" starting at byte " + unicode_type(self._start)
|
||||||
|
|
||||||
|
|
||||||
class xml_attr_field(object):
|
class xml_attr_field(object):
|
||||||
@ -191,7 +192,7 @@ class xml_field(object):
|
|||||||
return elem
|
return elem
|
||||||
|
|
||||||
if not val:
|
if not val:
|
||||||
val = u''
|
val = ''
|
||||||
if not isinstance(val, unicode_type):
|
if not isinstance(val, unicode_type):
|
||||||
val = val.decode('utf-8')
|
val = val.decode('utf-8')
|
||||||
|
|
||||||
@ -703,9 +704,8 @@ def main(args=sys.argv):
|
|||||||
lrf.producer = options.producer
|
lrf.producer = options.producer
|
||||||
if options.thumbnail:
|
if options.thumbnail:
|
||||||
path = os.path.expanduser(os.path.expandvars(options.thumbnail))
|
path = os.path.expanduser(os.path.expandvars(options.thumbnail))
|
||||||
f = open(path, "rb")
|
with open(path, "rb") as f:
|
||||||
lrf.thumbnail = f.read()
|
lrf.thumbnail = f.read()
|
||||||
f.close()
|
|
||||||
if options.book_id is not None:
|
if options.book_id is not None:
|
||||||
lrf.book_id = options.book_id
|
lrf.book_id = options.book_id
|
||||||
if options.comment:
|
if options.comment:
|
||||||
@ -716,15 +716,14 @@ def main(args=sys.argv):
|
|||||||
td = "None"
|
td = "None"
|
||||||
if t and len(t) > 0:
|
if t and len(t) > 0:
|
||||||
td = os.path.basename(args[1])+"_thumbnail."+lrf.thumbail_extension()
|
td = os.path.basename(args[1])+"_thumbnail."+lrf.thumbail_extension()
|
||||||
f = open(td, "w")
|
with open(td, "wb") as f:
|
||||||
f.write(t)
|
f.write(t)
|
||||||
f.close()
|
|
||||||
|
|
||||||
fields = LRFMetaFile.__dict__.items()
|
fields = LRFMetaFile.__dict__.items()
|
||||||
fields.sort()
|
fields.sort()
|
||||||
for f in fields:
|
for f in fields:
|
||||||
if "XML" in str(f):
|
if "XML" in unicode_type(f):
|
||||||
print(str(f[1]) + ":", lrf.__getattribute__(f[0]).encode('utf-8'))
|
print(unicode_type(f[1]) + ":", lrf.__getattribute__(f[0]).encode('utf-8'))
|
||||||
if options.get_thumbnail:
|
if options.get_thumbnail:
|
||||||
print("Thumbnail:", td)
|
print("Thumbnail:", td)
|
||||||
if options.get_cover:
|
if options.get_cover:
|
||||||
@ -734,7 +733,8 @@ def main(args=sys.argv):
|
|||||||
ext, data = None, None
|
ext, data = None, None
|
||||||
if data:
|
if data:
|
||||||
cover = os.path.splitext(os.path.basename(args[1]))[0]+"_cover."+ext
|
cover = os.path.splitext(os.path.basename(args[1]))[0]+"_cover."+ext
|
||||||
open(cover, 'wb').write(data)
|
with open(cover, 'wb') as f:
|
||||||
|
f.write(data)
|
||||||
print('Cover:', cover)
|
print('Cover:', cover)
|
||||||
else:
|
else:
|
||||||
print('Could not find cover in the LRF file')
|
print('Could not find cover in the LRF file')
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
from __future__ import print_function
|
from __future__ import absolute_import, division, print_function, unicode_literals
|
||||||
|
|
||||||
__license__ = 'GPL v3'
|
__license__ = 'GPL v3'
|
||||||
__copyright__ = '2008, Kovid Goyal <kovid at kovidgoyal.net>'
|
__copyright__ = '2008, Kovid Goyal <kovid at kovidgoyal.net>'
|
||||||
import struct, array, zlib, io, collections, re
|
import struct, array, zlib, io, collections, re
|
||||||
@ -127,7 +128,7 @@ class LRFContentObject(LRFObject):
|
|||||||
func, args = action[0], (action[1],)
|
func, args = action[0], (action[1],)
|
||||||
getattr(self, func)(tag, *args)
|
getattr(self, func)(tag, *args)
|
||||||
else:
|
else:
|
||||||
raise LRFParseError("Unknown tag in %s: %s" % (self.__class__.__name__, str(tag)))
|
raise LRFParseError("Unknown tag in %s: %s" % (self.__class__.__name__, unicode_type(tag)))
|
||||||
|
|
||||||
def __iter__(self):
|
def __iter__(self):
|
||||||
for i in self._contents:
|
for i in self._contents:
|
||||||
@ -195,17 +196,17 @@ class PageTree(LRFObject):
|
|||||||
class StyleObject(object):
|
class StyleObject(object):
|
||||||
|
|
||||||
def _tags_to_xml(self):
|
def _tags_to_xml(self):
|
||||||
s = u''
|
s = ''
|
||||||
for h in self.tag_map.values():
|
for h in self.tag_map.values():
|
||||||
attr = h[0]
|
attr = h[0]
|
||||||
if hasattr(self, attr):
|
if hasattr(self, attr):
|
||||||
s += u'%s="%s" '%(attr, getattr(self, attr))
|
s += '%s="%s" '%(attr, getattr(self, attr))
|
||||||
return s
|
return s
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
s = u'<%s objid="%s" stylelabel="%s" '%(self.__class__.__name__.replace('Attr', 'Style'), self.id, self.id)
|
s = '<%s objid="%s" stylelabel="%s" '%(self.__class__.__name__.replace('Attr', 'Style'), self.id, self.id)
|
||||||
s += self._tags_to_xml()
|
s += self._tags_to_xml()
|
||||||
s += u'/>\n'
|
s += '/>\n'
|
||||||
return s
|
return s
|
||||||
|
|
||||||
if not ispy3:
|
if not ispy3:
|
||||||
@ -254,7 +255,7 @@ class Color(object):
|
|||||||
self.a, self.r, self.g, self.b = val & 0xFF, (val>>8)&0xFF, (val>>16)&0xFF, (val>>24)&0xFF
|
self.a, self.r, self.g, self.b = val & 0xFF, (val>>8)&0xFF, (val>>16)&0xFF, (val>>24)&0xFF
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return u'0x%02x%02x%02x%02x'%(self.a, self.r, self.g, self.b)
|
return '0x%02x%02x%02x%02x'%(self.a, self.r, self.g, self.b)
|
||||||
|
|
||||||
if not ispy3:
|
if not ispy3:
|
||||||
__unicode__ = __str__
|
__unicode__ = __str__
|
||||||
@ -286,7 +287,7 @@ class PageDiv(EmptyPageElement):
|
|||||||
self.linecolor = Color(linecolor)
|
self.linecolor = Color(linecolor)
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return u'\n<PageDiv pain="%s" spacesize="%s" linewidth="%s" linecolor="%s" />\n'%\
|
return '\n<PageDiv pain="%s" spacesize="%s" linewidth="%s" linecolor="%s" />\n'%\
|
||||||
(self.pain, self.spacesize, self.linewidth, self.color)
|
(self.pain, self.spacesize, self.linewidth, self.color)
|
||||||
|
|
||||||
if not ispy3:
|
if not ispy3:
|
||||||
@ -304,7 +305,7 @@ class RuledLine(EmptyPageElement):
|
|||||||
self.id = -1
|
self.id = -1
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return u'\n<RuledLine linelength="%s" linetype="%s" linewidth="%s" linecolor="%s" />\n'%\
|
return '\n<RuledLine linelength="%s" linetype="%s" linewidth="%s" linecolor="%s" />\n'%\
|
||||||
(self.linelength, self.linetype, self.linewidth, self.linecolor)
|
(self.linelength, self.linetype, self.linewidth, self.linecolor)
|
||||||
|
|
||||||
if not ispy3:
|
if not ispy3:
|
||||||
@ -317,7 +318,7 @@ class Wait(EmptyPageElement):
|
|||||||
self.time = time
|
self.time = time
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return u'\n<Wait time="%d" />\n'%(self.time)
|
return '\n<Wait time="%d" />\n'%(self.time)
|
||||||
|
|
||||||
if not ispy3:
|
if not ispy3:
|
||||||
__unicode__ = __str__
|
__unicode__ = __str__
|
||||||
@ -331,7 +332,7 @@ class Locate(EmptyPageElement):
|
|||||||
self.pos = self.pos_map[pos]
|
self.pos = self.pos_map[pos]
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return u'\n<Locate pos="%s" />\n'%(self.pos)
|
return '\n<Locate pos="%s" />\n'%(self.pos)
|
||||||
|
|
||||||
if not ispy3:
|
if not ispy3:
|
||||||
__unicode__ = __str__
|
__unicode__ = __str__
|
||||||
@ -343,7 +344,7 @@ class BlockSpace(EmptyPageElement):
|
|||||||
self.xspace, self.yspace = xspace, yspace
|
self.xspace, self.yspace = xspace, yspace
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return u'\n<BlockSpace xspace="%d" yspace="%d" />\n'%\
|
return '\n<BlockSpace xspace="%d" yspace="%d" />\n'%\
|
||||||
(self.xspace, self.yspace)
|
(self.xspace, self.yspace)
|
||||||
|
|
||||||
if not ispy3:
|
if not ispy3:
|
||||||
@ -444,7 +445,7 @@ class Page(LRFStream):
|
|||||||
yield i
|
yield i
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
s = u'\n<Page pagestyle="%d" objid="%d">\n'%(self.style_id, self.id)
|
s = '\n<Page pagestyle="%d" objid="%d">\n'%(self.style_id, self.id)
|
||||||
for i in self:
|
for i in self:
|
||||||
s += unicode_type(i)
|
s += unicode_type(i)
|
||||||
s += '\n</Page>\n'
|
s += '\n</Page>\n'
|
||||||
@ -454,7 +455,7 @@ class Page(LRFStream):
|
|||||||
__unicode__ = __str__
|
__unicode__ = __str__
|
||||||
|
|
||||||
def to_html(self):
|
def to_html(self):
|
||||||
s = u''
|
s = ''
|
||||||
for i in self:
|
for i in self:
|
||||||
s += i.to_html()
|
s += i.to_html()
|
||||||
return s
|
return s
|
||||||
@ -629,7 +630,7 @@ class Block(LRFStream, TextCSS):
|
|||||||
self.attrs[attr] = getattr(self, attr)
|
self.attrs[attr] = getattr(self, attr)
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
s = u'\n<%s objid="%d" blockstyle="%d" '%(self.name, self.id, self.style_id)
|
s = '\n<%s objid="%d" blockstyle="%d" '%(self.name, self.id, self.style_id)
|
||||||
if hasattr(self, 'textstyle_id'):
|
if hasattr(self, 'textstyle_id'):
|
||||||
s += 'textstyle="%d" '%(self.textstyle_id,)
|
s += 'textstyle="%d" '%(self.textstyle_id,)
|
||||||
for attr in self.attrs:
|
for attr in self.attrs:
|
||||||
@ -646,8 +647,8 @@ class Block(LRFStream, TextCSS):
|
|||||||
|
|
||||||
def to_html(self):
|
def to_html(self):
|
||||||
if self.name == 'TextBlock':
|
if self.name == 'TextBlock':
|
||||||
return u'<div class="block%s text%s">%s</div>'%(self.style_id, self.textstyle_id, self.content.to_html())
|
return '<div class="block%s text%s">%s</div>'%(self.style_id, self.textstyle_id, self.content.to_html())
|
||||||
return u''
|
return ''
|
||||||
|
|
||||||
|
|
||||||
class MiniPage(LRFStream):
|
class MiniPage(LRFStream):
|
||||||
@ -668,7 +669,7 @@ class Text(LRFStream):
|
|||||||
|
|
||||||
style = property(fget=lambda self : self._document.objects[self.style_id])
|
style = property(fget=lambda self : self._document.objects[self.style_id])
|
||||||
|
|
||||||
text_map = {0x22: u'"', 0x26: u'&', 0x27: u'\'', 0x3c: u'<', 0x3e: u'>'}
|
text_map = {0x22: '"', 0x26: '&', 0x27: '\'', 0x3c: '<', 0x3e: '>'}
|
||||||
entity_pattern = re.compile(r'&(\S+?);')
|
entity_pattern = re.compile(r'&(\S+?);')
|
||||||
|
|
||||||
text_tags = {
|
text_tags = {
|
||||||
@ -717,20 +718,20 @@ class Text(LRFStream):
|
|||||||
self.self_closing = self_closing
|
self.self_closing = self_closing
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
s = u'<%s '%(self.name,)
|
s = '<%s '%(self.name,)
|
||||||
for name, val in self.attrs.items():
|
for name, val in self.attrs.items():
|
||||||
s += '%s="%s" '%(name, val)
|
s += '%s="%s" '%(name, val)
|
||||||
return s.rstrip() + (u' />' if self.self_closing else u'>')
|
return s.rstrip() + (' />' if self.self_closing else '>')
|
||||||
|
|
||||||
if not ispy3:
|
if not ispy3:
|
||||||
__unicode__ = __str__
|
__unicode__ = __str__
|
||||||
|
|
||||||
def to_html(self):
|
def to_html(self):
|
||||||
s = u''
|
s = ''
|
||||||
return s
|
return s
|
||||||
|
|
||||||
def close_html(self):
|
def close_html(self):
|
||||||
return u''
|
return ''
|
||||||
|
|
||||||
class Span(TextTag):
|
class Span(TextTag):
|
||||||
pass
|
pass
|
||||||
@ -901,7 +902,7 @@ class Text(LRFStream):
|
|||||||
self.stream = None
|
self.stream = None
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
s = u''
|
s = ''
|
||||||
open_containers = collections.deque()
|
open_containers = collections.deque()
|
||||||
for c in self.content:
|
for c in self.content:
|
||||||
if isinstance(c, string_or_bytes):
|
if isinstance(c, string_or_bytes):
|
||||||
@ -909,7 +910,7 @@ class Text(LRFStream):
|
|||||||
elif c is None:
|
elif c is None:
|
||||||
if open_containers:
|
if open_containers:
|
||||||
p = open_containers.pop()
|
p = open_containers.pop()
|
||||||
s += u'</%s>'%(p.name,)
|
s += '</%s>'%(p.name,)
|
||||||
else:
|
else:
|
||||||
s += unicode_type(c)
|
s += unicode_type(c)
|
||||||
if not c.self_closing:
|
if not c.self_closing:
|
||||||
@ -917,7 +918,7 @@ class Text(LRFStream):
|
|||||||
|
|
||||||
if len(open_containers) > 0:
|
if len(open_containers) > 0:
|
||||||
if len(open_containers) == 1:
|
if len(open_containers) == 1:
|
||||||
s += u'</%s>'%(open_containers[0].name,)
|
s += '</%s>'%(open_containers[0].name,)
|
||||||
else:
|
else:
|
||||||
raise LRFParseError('Malformed text stream %s'%([i.name for i in open_containers if isinstance(i, Text.TextTag)],))
|
raise LRFParseError('Malformed text stream %s'%([i.name for i in open_containers if isinstance(i, Text.TextTag)],))
|
||||||
return s
|
return s
|
||||||
@ -926,7 +927,7 @@ class Text(LRFStream):
|
|||||||
__unicode__ = __str__
|
__unicode__ = __str__
|
||||||
|
|
||||||
def to_html(self):
|
def to_html(self):
|
||||||
s = u''
|
s = ''
|
||||||
open_containers = collections.deque()
|
open_containers = collections.deque()
|
||||||
in_p = False
|
in_p = False
|
||||||
for c in self.content:
|
for c in self.content:
|
||||||
@ -970,7 +971,7 @@ class Image(LRFObject):
|
|||||||
data = property(fget=lambda self : self._document.objects[self.refstream].stream)
|
data = property(fget=lambda self : self._document.objects[self.refstream].stream)
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return u'<Image objid="%s" x0="%d" y0="%d" x1="%d" y1="%d" xsize="%d" ysize="%d" refstream="%d" />\n'%\
|
return '<Image objid="%s" x0="%d" y0="%d" x1="%d" y1="%d" xsize="%d" ysize="%d" refstream="%d" />\n'%\
|
||||||
(self.id, self.x0, self.y0, self.x1, self.y1, self.xsize, self.ysize, self.refstream)
|
(self.id, self.x0, self.y0, self.x1, self.y1, self.xsize, self.ysize, self.refstream)
|
||||||
|
|
||||||
if not ispy3:
|
if not ispy3:
|
||||||
@ -984,7 +985,7 @@ class PutObj(EmptyPageElement):
|
|||||||
self.object = objects[refobj]
|
self.object = objects[refobj]
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return u'<PutObj x1="%d" y1="%d" refobj="%d" />'%(self.x1, self.y1, self.refobj)
|
return '<PutObj x1="%d" y1="%d" refobj="%d" />'%(self.x1, self.y1, self.refobj)
|
||||||
|
|
||||||
if not ispy3:
|
if not ispy3:
|
||||||
__unicode__ = __str__
|
__unicode__ = __str__
|
||||||
@ -1069,12 +1070,12 @@ class ImageStream(LRFStream):
|
|||||||
|
|
||||||
def end_stream(self, *args):
|
def end_stream(self, *args):
|
||||||
LRFStream.end_stream(self, *args)
|
LRFStream.end_stream(self, *args)
|
||||||
self.file = str(self.id) + '.' + self.encoding.lower()
|
self.file = unicode_type(self.id) + '.' + self.encoding.lower()
|
||||||
if self._document is not None:
|
if self._document is not None:
|
||||||
self._document.image_map[self.id] = self
|
self._document.image_map[self.id] = self
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return u'<ImageStream objid="%s" encoding="%s" file="%s" />\n'%\
|
return '<ImageStream objid="%s" encoding="%s" file="%s" />\n'%\
|
||||||
(self.id, self.encoding, self.file)
|
(self.id, self.encoding, self.file)
|
||||||
|
|
||||||
if not ispy3:
|
if not ispy3:
|
||||||
@ -1156,7 +1157,7 @@ class Button(LRFObject):
|
|||||||
return (None, None)
|
return (None, None)
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
s = u'<Button objid="%s">\n'%(self.id,)
|
s = '<Button objid="%s">\n'%(self.id,)
|
||||||
if self.button_flags & 0x10 != 0:
|
if self.button_flags & 0x10 != 0:
|
||||||
s += '<PushButton '
|
s += '<PushButton '
|
||||||
if 2 in self.refimage:
|
if 2 in self.refimage:
|
||||||
@ -1233,10 +1234,10 @@ class BookAttr(StyleObject, LRFObject):
|
|||||||
self.font_link_list.append(tag.dword)
|
self.font_link_list.append(tag.dword)
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
s = u'<BookStyle objid="%s" stylelabel="%s">\n'%(self.id, self.id)
|
s = '<BookStyle objid="%s" stylelabel="%s">\n'%(self.id, self.id)
|
||||||
s += u'<SetDefault %s />\n'%(self._tags_to_xml(),)
|
s += '<SetDefault %s />\n'%(self._tags_to_xml(),)
|
||||||
doc = self._document
|
doc = self._document
|
||||||
s += u'<BookSetting bindingdirection="%s" dpi="%s" screenwidth="%s" screenheight="%s" colordepth="%s" />\n'%\
|
s += '<BookSetting bindingdirection="%s" dpi="%s" screenwidth="%s" screenheight="%s" colordepth="%s" />\n'%\
|
||||||
(self.binding_map[doc.binding], doc.dpi, doc.width, doc.height, doc.color_depth)
|
(self.binding_map[doc.binding], doc.dpi, doc.width, doc.height, doc.color_depth)
|
||||||
for font in self._document.font_map.values():
|
for font in self._document.font_map.values():
|
||||||
s += unicode_type(font)
|
s += unicode_type(font)
|
||||||
@ -1257,7 +1258,7 @@ class TocLabel(object):
|
|||||||
self.refpage, self.refobject, self.label = refpage, refobject, label
|
self.refpage, self.refobject, self.label = refpage, refobject, label
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return u'<TocLabel refpage="%s" refobj="%s">%s</TocLabel>\n'%(self.refpage, self.refobject, self.label)
|
return '<TocLabel refpage="%s" refobj="%s">%s</TocLabel>\n'%(self.refpage, self.refobject, self.label)
|
||||||
|
|
||||||
if not ispy3:
|
if not ispy3:
|
||||||
__unicode__ = __str__
|
__unicode__ = __str__
|
||||||
@ -1284,7 +1285,7 @@ class TOCObject(LRFStream):
|
|||||||
yield i
|
yield i
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
s = u'<TOC>\n'
|
s = '<TOC>\n'
|
||||||
for i in self:
|
for i in self:
|
||||||
s += unicode_type(i)
|
s += unicode_type(i)
|
||||||
return s + '</TOC>\n'
|
return s + '</TOC>\n'
|
||||||
|
Loading…
x
Reference in New Issue
Block a user