mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Added basic "any2mobi" command-line tool.
This commit is contained in:
parent
e73639e5d3
commit
35fce76007
63
src/calibre/ebooks/mobi/from_any.py
Normal file
63
src/calibre/ebooks/mobi/from_any.py
Normal file
@ -0,0 +1,63 @@
|
|||||||
|
'''
|
||||||
|
Convert any ebook format to Mobipocket.
|
||||||
|
'''
|
||||||
|
|
||||||
|
from __future__ import with_statement
|
||||||
|
|
||||||
|
__license__ = 'GPL v3'
|
||||||
|
__copyright__ = '2008, Kovid Goyal kovid@kovidgoyal.net ' \
|
||||||
|
'and Marshall T. Vandegrift <llasram@gmail.com>'
|
||||||
|
__docformat__ = 'restructuredtext en'
|
||||||
|
|
||||||
|
import sys, os, glob, logging
|
||||||
|
|
||||||
|
from calibre.ebooks.epub.from_any import any2epub, formats, USAGE
|
||||||
|
from calibre.ebooks.epub import config as common_config
|
||||||
|
from calibre.ptempfile import TemporaryDirectory
|
||||||
|
from calibre.ebooks.mobi.writer import oeb2mobi, add_mobi_options
|
||||||
|
|
||||||
|
def config(defaults=None):
|
||||||
|
return common_config(defaults=defaults, name='mobi')
|
||||||
|
|
||||||
|
def option_parser(usage=USAGE):
|
||||||
|
usage = usage % ('Mobipocket', formats())
|
||||||
|
parser = config().option_parser(usage=usage)
|
||||||
|
add_mobi_options(parser)
|
||||||
|
return parser
|
||||||
|
|
||||||
|
def any2mobi(opts, path):
|
||||||
|
ext = os.path.splitext(path)[1]
|
||||||
|
if not ext:
|
||||||
|
raise ValueError('Unknown file type: '+path)
|
||||||
|
ext = ext.lower()[1:]
|
||||||
|
|
||||||
|
if opts.output is None:
|
||||||
|
opts.output = os.path.splitext(os.path.basename(path))[0]+'.mobi'
|
||||||
|
|
||||||
|
opts.output = os.path.abspath(opts.output)
|
||||||
|
orig_output = opts.output
|
||||||
|
|
||||||
|
with TemporaryDirectory('_any2mobi') as tdir:
|
||||||
|
oebdir = os.path.join(tdir, 'oeb')
|
||||||
|
os.mkdir(oebdir)
|
||||||
|
opts.output = os.path.join(tdir, 'dummy.epub')
|
||||||
|
opts.profile = 'None'
|
||||||
|
any2epub(opts, path, create_epub=False, oeb_cover=True, extract_to=oebdir)
|
||||||
|
opf = glob.glob(os.path.join(oebdir, '*.opf'))[0]
|
||||||
|
opts.output = orig_output
|
||||||
|
logging.getLogger('html2epub').info(_('Creating Mobipocket file from EPUB...'))
|
||||||
|
oeb2mobi(opts, opf)
|
||||||
|
|
||||||
|
|
||||||
|
def main(args=sys.argv):
|
||||||
|
parser = option_parser()
|
||||||
|
opts, args = parser.parse_args(args)
|
||||||
|
if len(args) < 2:
|
||||||
|
parser.print_help()
|
||||||
|
print 'No input file specified.'
|
||||||
|
return 1
|
||||||
|
any2mobi(opts, args[1])
|
||||||
|
return 0
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
sys.exit(main())
|
@ -34,6 +34,8 @@ from calibre.ebooks.mobi.palmdoc import compress_doc
|
|||||||
from calibre.ebooks.mobi.langcodes import iana2mobi
|
from calibre.ebooks.mobi.langcodes import iana2mobi
|
||||||
from calibre.ebooks.mobi.mobiml import MBP_NS, MBP, MobiMLizer
|
from calibre.ebooks.mobi.mobiml import MBP_NS, MBP, MobiMLizer
|
||||||
from calibre.customize.ui import run_plugins_on_postprocess
|
from calibre.customize.ui import run_plugins_on_postprocess
|
||||||
|
from calibre.utils.config import OptionParser
|
||||||
|
from optparse import OptionGroup
|
||||||
|
|
||||||
# TODO:
|
# TODO:
|
||||||
# - Allow override CSS (?)
|
# - Allow override CSS (?)
|
||||||
@ -492,25 +494,19 @@ class MobiWriter(object):
|
|||||||
self._write(record)
|
self._write(record)
|
||||||
|
|
||||||
|
|
||||||
def option_parser():
|
def add_mobi_options(parser):
|
||||||
profiles = Context.PROFILES.keys()
|
profiles = Context.PROFILES.keys()
|
||||||
profiles.sort()
|
profiles.sort()
|
||||||
profiles = ', '.join(profiles)
|
profiles = ', '.join(profiles)
|
||||||
from calibre.utils.config import OptionParser
|
group = OptionGroup(parser, _('Mobipocket'),
|
||||||
from optparse import OptionGroup
|
_('Mobipocket-specific options.'))
|
||||||
parser = OptionParser(usage=_('%prog [options] OPFFILE'))
|
group.add_option(
|
||||||
parser.add_option(
|
|
||||||
'-o', '--output', default=None,
|
|
||||||
help=_('Output file. Default is derived from input filename.'))
|
|
||||||
parser.add_option(
|
|
||||||
'-c', '--compress', default=False, action='store_true',
|
'-c', '--compress', default=False, action='store_true',
|
||||||
help=_('Compress file text using PalmDOC compression.'))
|
help=_('Compress file text using PalmDOC compression.'))
|
||||||
parser.add_option(
|
group.add_option(
|
||||||
'-r', '--rescale-images', default=False, action='store_true',
|
'-r', '--rescale-images', default=False, action='store_true',
|
||||||
help=_('Modify images to meet Palm device size limitations.'))
|
help=_('Modify images to meet Palm device size limitations.'))
|
||||||
parser.add_option(
|
parser.add_option_group(group)
|
||||||
'-v', '--verbose', default=False, action='store_true',
|
|
||||||
help=_('Useful for debugging.'))
|
|
||||||
group = OptionGroup(parser, _('Profiles'), _('Device renderer profiles. '
|
group = OptionGroup(parser, _('Profiles'), _('Device renderer profiles. '
|
||||||
'Affects conversion of default font sizes and rasterization '
|
'Affects conversion of default font sizes and rasterization '
|
||||||
'resolution. Valid profiles are: %s.') % profiles)
|
'resolution. Valid profiles are: %s.') % profiles)
|
||||||
@ -521,6 +517,17 @@ def option_parser():
|
|||||||
'--dest-profile', default='CybookG3', metavar='PROFILE',
|
'--dest-profile', default='CybookG3', metavar='PROFILE',
|
||||||
help=_("Destination renderer profile. Default is 'CybookG3'."))
|
help=_("Destination renderer profile. Default is 'CybookG3'."))
|
||||||
parser.add_option_group(group)
|
parser.add_option_group(group)
|
||||||
|
return
|
||||||
|
|
||||||
|
def option_parser():
|
||||||
|
parser = OptionParser(usage=_('%prog [options] OPFFILE'))
|
||||||
|
parser.add_option(
|
||||||
|
'-o', '--output', default=None,
|
||||||
|
help=_('Output file. Default is derived from input filename.'))
|
||||||
|
parser.add_option(
|
||||||
|
'-v', '--verbose', default=False, action='store_true',
|
||||||
|
help=_('Useful for debugging.'))
|
||||||
|
add_mobi_options(parser)
|
||||||
return parser
|
return parser
|
||||||
|
|
||||||
def oeb2mobi(opts, inpath):
|
def oeb2mobi(opts, inpath):
|
||||||
|
@ -48,6 +48,7 @@ entry_points = {
|
|||||||
'any2lrf = calibre.ebooks.lrf.any.convert_from:main',
|
'any2lrf = calibre.ebooks.lrf.any.convert_from:main',
|
||||||
'any2epub = calibre.ebooks.epub.from_any:main',
|
'any2epub = calibre.ebooks.epub.from_any:main',
|
||||||
'any2lit = calibre.ebooks.lit.from_any:main',
|
'any2lit = calibre.ebooks.lit.from_any:main',
|
||||||
|
'any2mobi = calibre.ebooks.mobi.from_any:main',
|
||||||
'lrf2lrs = calibre.ebooks.lrf.lrfparser:main',
|
'lrf2lrs = calibre.ebooks.lrf.lrfparser:main',
|
||||||
'lrs2lrf = calibre.ebooks.lrf.lrs.convert_from:main',
|
'lrs2lrf = calibre.ebooks.lrf.lrs.convert_from:main',
|
||||||
'pdfreflow = calibre.ebooks.lrf.pdf.reflow:main',
|
'pdfreflow = calibre.ebooks.lrf.pdf.reflow:main',
|
||||||
|
Loading…
x
Reference in New Issue
Block a user