mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
...
This commit is contained in:
parent
c1967a935d
commit
8c190b3415
@ -7,7 +7,7 @@ __license__ = 'GPL v3'
|
|||||||
__copyright__ = '2011, Kovid Goyal <kovid@kovidgoyal.net>'
|
__copyright__ = '2011, Kovid Goyal <kovid@kovidgoyal.net>'
|
||||||
__docformat__ = 'restructuredtext en'
|
__docformat__ = 'restructuredtext en'
|
||||||
|
|
||||||
import os, subprocess, tempfile, shutil
|
import os, subprocess, shutil
|
||||||
|
|
||||||
from lxml import etree
|
from lxml import etree
|
||||||
|
|
||||||
@ -22,6 +22,12 @@ exe = 'kindlegen.exe' if iswindows else 'kindlegen'
|
|||||||
def refactor_opf(opf, is_periodical, toc):
|
def refactor_opf(opf, is_periodical, toc):
|
||||||
with open(opf, 'rb') as f:
|
with open(opf, 'rb') as f:
|
||||||
root = etree.fromstring(f.read())
|
root = etree.fromstring(f.read())
|
||||||
|
'''
|
||||||
|
for spine in root.xpath('//*[local-name() = "spine" and @toc]'):
|
||||||
|
# Do not use the NCX toc as kindlegen requires the section structure
|
||||||
|
# in the TOC to be duplicated in the HTML, asinine!
|
||||||
|
del spine.attrib['toc']
|
||||||
|
'''
|
||||||
if is_periodical:
|
if is_periodical:
|
||||||
metadata = root.xpath('//*[local-name() = "metadata"]')[0]
|
metadata = root.xpath('//*[local-name() = "metadata"]')[0]
|
||||||
xm = etree.SubElement(metadata, 'x-metadata')
|
xm = etree.SubElement(metadata, 'x-metadata')
|
||||||
@ -29,7 +35,7 @@ def refactor_opf(opf, is_periodical, toc):
|
|||||||
xm.text = '\n\t'
|
xm.text = '\n\t'
|
||||||
mobip = etree.SubElement(xm, 'output', attrib={'encoding':"utf-8",
|
mobip = etree.SubElement(xm, 'output', attrib={'encoding':"utf-8",
|
||||||
'content-type':"application/x-mobipocket-subscription-magazine"})
|
'content-type':"application/x-mobipocket-subscription-magazine"})
|
||||||
mobip.tail = '\n'
|
mobip.tail = '\n\t'
|
||||||
with open(opf, 'wb') as f:
|
with open(opf, 'wb') as f:
|
||||||
f.write(etree.tostring(root, method='xml', encoding='utf-8',
|
f.write(etree.tostring(root, method='xml', encoding='utf-8',
|
||||||
xml_declaration=True))
|
xml_declaration=True))
|
||||||
@ -43,14 +49,13 @@ def refactor_guide(oeb):
|
|||||||
def run_kindlegen(opf, log):
|
def run_kindlegen(opf, log):
|
||||||
log.info('Running kindlegen on MOBIML created by calibre')
|
log.info('Running kindlegen on MOBIML created by calibre')
|
||||||
oname = os.path.splitext(opf)[0] + '.mobi'
|
oname = os.path.splitext(opf)[0] + '.mobi'
|
||||||
with tempfile.NamedTemporaryFile(suffix='_kindlegen_output.txt') as tfile:
|
p = subprocess.Popen([exe, opf, '-c1', '-verbose', '-o', oname],
|
||||||
p = subprocess.Popen([exe, opf, '-c1', '-verbose', '-o', oname],
|
stderr=subprocess.STDOUT, stdout=subprocess.PIPE)
|
||||||
stderr=subprocess.STDOUT, stdout=tfile)
|
ko = p.stdout.read()
|
||||||
returncode = p.wait()
|
returncode = p.wait()
|
||||||
tfile.seek(0)
|
log.debug('kindlegen verbose output:')
|
||||||
log.debug('kindlegen verbose output:')
|
log.debug(ko.decode('utf-8', 'replace'))
|
||||||
log.debug(tfile.read())
|
log.info('kindlegen returned returncode: %d'%returncode)
|
||||||
log.info('kindlegen returned returncode: %d'%returncode)
|
|
||||||
if not os.path.exists(oname) or os.stat(oname).st_size < 100:
|
if not os.path.exists(oname) or os.stat(oname).st_size < 100:
|
||||||
raise RuntimeError('kindlegen did not produce any output. '
|
raise RuntimeError('kindlegen did not produce any output. '
|
||||||
'kindlegen return code: %d'%returncode)
|
'kindlegen return code: %d'%returncode)
|
||||||
@ -59,7 +64,7 @@ def run_kindlegen(opf, log):
|
|||||||
def kindlegen(oeb, opts, input_plugin, output_path):
|
def kindlegen(oeb, opts, input_plugin, output_path):
|
||||||
is_periodical = detect_periodical(oeb.toc, oeb.log)
|
is_periodical = detect_periodical(oeb.toc, oeb.log)
|
||||||
refactor_guide(oeb)
|
refactor_guide(oeb)
|
||||||
with TemporaryDirectory('_epub_output') as tdir:
|
with TemporaryDirectory('_kindlegen_output') as tdir:
|
||||||
oeb_output = plugin_for_output_format('oeb')
|
oeb_output = plugin_for_output_format('oeb')
|
||||||
oeb_output.convert(oeb, tdir, input_plugin, opts, oeb.log)
|
oeb_output.convert(oeb, tdir, input_plugin, opts, oeb.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]
|
||||||
|
@ -50,14 +50,12 @@ class MOBIOutput(OutputFormatPlugin):
|
|||||||
help=_('When adding the Table of Contents to the book, add it at the start of the '
|
help=_('When adding the Table of Contents to the book, add it at the start of the '
|
||||||
'book instead of the end. Not recommended.')
|
'book instead of the end. Not recommended.')
|
||||||
),
|
),
|
||||||
'''
|
|
||||||
OptionRecommendation(name='kindlegen',
|
OptionRecommendation(name='kindlegen',
|
||||||
recommended_value=False,
|
recommended_value=False,
|
||||||
help=('Use kindlegen (must be in your PATH) to generate the'
|
help=('Use kindlegen (must be in your PATH) to generate the'
|
||||||
' binary wrapper for the MOBI format. Useful to debug '
|
' binary wrapper for the MOBI format. Useful to debug '
|
||||||
' the calibre MOBI output.')
|
' the calibre MOBI output.')
|
||||||
),
|
),
|
||||||
'''
|
|
||||||
|
|
||||||
])
|
])
|
||||||
|
|
||||||
|
@ -366,7 +366,7 @@ class MobiWriter(object):
|
|||||||
# 0x70 - 0x73 : EXTH flags
|
# 0x70 - 0x73 : EXTH flags
|
||||||
# Bit 6 (0b1000000) being set indicates the presence of an EXTH header
|
# Bit 6 (0b1000000) being set indicates the presence of an EXTH header
|
||||||
# The purpose of the other bits is unknown
|
# The purpose of the other bits is unknown
|
||||||
exth_flags = 0b1011000
|
exth_flags = 0b1010000
|
||||||
if self.is_periodical:
|
if self.is_periodical:
|
||||||
exth_flags |= 0b1000
|
exth_flags |= 0b1000
|
||||||
record0.write(pack(b'>I', exth_flags))
|
record0.write(pack(b'>I', exth_flags))
|
||||||
@ -506,7 +506,6 @@ class MobiWriter(object):
|
|||||||
|
|
||||||
if datestr is not None:
|
if datestr is not None:
|
||||||
datestr = bytes(datestr)
|
datestr = bytes(datestr)
|
||||||
datestr = datestr.replace(b'+00:00', b'Z')
|
|
||||||
exth.write(pack(b'>II', EXTH_CODES['pubdate'], len(datestr) + 8))
|
exth.write(pack(b'>II', EXTH_CODES['pubdate'], len(datestr) + 8))
|
||||||
exth.write(datestr)
|
exth.write(datestr)
|
||||||
nrecs += 1
|
nrecs += 1
|
||||||
@ -514,7 +513,7 @@ class MobiWriter(object):
|
|||||||
raise NotImplementedError("missing date or timestamp needed for mobi_periodical")
|
raise NotImplementedError("missing date or timestamp needed for mobi_periodical")
|
||||||
|
|
||||||
# Write the same creator info as kindlegen 1.2
|
# Write the same creator info as kindlegen 1.2
|
||||||
for code, val in [(204, 202), (205, 1), (206, 2), (207, 33307)]:
|
for code, val in [(204, 201), (205, 1), (206, 2), (207, 33307)]:
|
||||||
exth.write(pack(b'>II', code, 12))
|
exth.write(pack(b'>II', code, 12))
|
||||||
exth.write(pack(b'>I', val))
|
exth.write(pack(b'>I', val))
|
||||||
nrecs += 1
|
nrecs += 1
|
||||||
|
Loading…
x
Reference in New Issue
Block a user