mirror of
https://github.com/gryf/ebook-converter.git
synced 2026-04-21 21:51:32 +02:00
Use the real constants module.
This is progressing refactor of the calibre code to make it more readable, and transform it to something more coherent. In this patch, there are changes regarding imports for some modules, instead of polluting namespace of each module with some other modules symbols, which often were imported from other modules. Yuck.
This commit is contained in:
@@ -1,14 +1,22 @@
|
||||
from ebook_converter.customize.conversion import OutputFormatPlugin, OptionRecommendation
|
||||
import io
|
||||
|
||||
from lxml import etree
|
||||
|
||||
from ebook_converter import constants as const
|
||||
from ebook_converter.customize import conversion
|
||||
from ebook_converter.ebooks.docx.dump import do_dump
|
||||
from ebook_converter.ebooks.docx.writer.container import DOCX
|
||||
from ebook_converter.ebooks.docx.writer.from_html import Convert
|
||||
from ebook_converter.ebooks.metadata import opf2 as opf_meta
|
||||
from ebook_converter.ebooks.oeb import base
|
||||
|
||||
__license__ = 'GPL v3'
|
||||
__copyright__ = '2013, Kovid Goyal <kovid at kovidgoyal.net>'
|
||||
|
||||
PAGE_SIZES = ['a0', 'a1', 'a2', 'a3', 'a4', 'a5', 'a6', 'b0', 'b1',
|
||||
'b2', 'b3', 'b4', 'b5', 'b6', 'legal', 'letter']
|
||||
_OPT = conversion.OptionRecommendation
|
||||
|
||||
|
||||
class DOCXOutput(OutputFormatPlugin):
|
||||
class DOCXOutput(conversion.OutputFormatPlugin):
|
||||
|
||||
name = 'DOCX Output'
|
||||
author = 'Kovid Goyal'
|
||||
@@ -16,75 +24,63 @@ class DOCXOutput(OutputFormatPlugin):
|
||||
commit_name = 'docx_output'
|
||||
ui_data = {'page_sizes': PAGE_SIZES}
|
||||
|
||||
options = {
|
||||
OptionRecommendation(name='docx_page_size', recommended_value='letter',
|
||||
level=OptionRecommendation.LOW, choices=PAGE_SIZES,
|
||||
help='The size of the page. Default is letter. Choices '
|
||||
'are %s' % PAGE_SIZES),
|
||||
|
||||
OptionRecommendation(name='docx_custom_page_size', recommended_value=None,
|
||||
help='Custom size of the document. Use the form widthxheight '
|
||||
'EG. `123x321` to specify the width and height (in pts). '
|
||||
'This overrides any specified page-size.'),
|
||||
|
||||
OptionRecommendation(name='docx_no_cover', recommended_value=False,
|
||||
help='Do not insert the book cover as an image at the start of the document.'
|
||||
' If you use this option, the book cover will be discarded.'),
|
||||
|
||||
OptionRecommendation(name='preserve_cover_aspect_ratio', recommended_value=False,
|
||||
help='Preserve the aspect ratio of the cover image instead of stretching'
|
||||
' it out to cover the entire page.'),
|
||||
|
||||
OptionRecommendation(name='docx_no_toc', recommended_value=False,
|
||||
help='Do not insert the table of contents as a page at the start of the document.'),
|
||||
|
||||
OptionRecommendation(name='extract_to',
|
||||
help='Extract the contents of the generated %s file to the '
|
||||
'specified directory. The contents of the directory are first '
|
||||
'deleted, so be careful.' % 'DOCX'),
|
||||
|
||||
OptionRecommendation(name='docx_page_margin_left', recommended_value=72.0,
|
||||
level=OptionRecommendation.LOW,
|
||||
help='The size of the left page margin, in pts. Default is 72pt.'
|
||||
' Overrides the common left page margin setting.'
|
||||
),
|
||||
|
||||
OptionRecommendation(name='docx_page_margin_top', recommended_value=72.0,
|
||||
level=OptionRecommendation.LOW,
|
||||
help='The size of the top page margin, in pts. Default is 72pt.'
|
||||
' Overrides the common top page margin setting, unless set to zero.'
|
||||
),
|
||||
|
||||
OptionRecommendation(name='docx_page_margin_right', recommended_value=72.0,
|
||||
level=OptionRecommendation.LOW,
|
||||
help='The size of the right page margin, in pts. Default is 72pt.'
|
||||
' Overrides the common right page margin setting, unless set to zero.'
|
||||
),
|
||||
|
||||
OptionRecommendation(name='docx_page_margin_bottom', recommended_value=72.0,
|
||||
level=OptionRecommendation.LOW,
|
||||
help='The size of the bottom page margin, in pts. Default is 72pt.'
|
||||
' Overrides the common bottom page margin setting, unless set to zero.'
|
||||
),
|
||||
|
||||
}
|
||||
options = {_OPT(name='docx_page_size', recommended_value='letter',
|
||||
level=_OPT.LOW, choices=PAGE_SIZES,
|
||||
help='The size of the page. Default is letter. Choices '
|
||||
'are %s' % PAGE_SIZES),
|
||||
_OPT(name='docx_custom_page_size', recommended_value=None,
|
||||
help='Custom size of the document. Use the form '
|
||||
'widthxheight EG. `123x321` to specify the width and '
|
||||
'height (in pts). This overrides any specified '
|
||||
'page-size.'),
|
||||
_OPT(name='docx_no_cover', recommended_value=False,
|
||||
help='Do not insert the book cover as an image at the '
|
||||
'start of the document. If you use this option, the book '
|
||||
'cover will be discarded.'),
|
||||
_OPT(name='preserve_cover_aspect_ratio',
|
||||
recommended_value=False, help='Preserve the aspect ratio '
|
||||
'of the cover image instead of stretching it out to cover '
|
||||
'the entire page.'),
|
||||
_OPT(name='docx_no_toc', recommended_value=False,
|
||||
help='Do not insert the table of contents as a page at '
|
||||
'the start of the document.'),
|
||||
_OPT(name='extract_to', help='Extract the contents of the '
|
||||
'generated DOCX file to the specified directory. The '
|
||||
'contents of the directory are first deleted, so be '
|
||||
'careful.'),
|
||||
_OPT(name='docx_page_margin_left', recommended_value=72.0,
|
||||
level=_OPT.LOW, help='The size of the left page margin, '
|
||||
'in pts. Default is 72pt. Overrides the common left page '
|
||||
'margin setting.'),
|
||||
_OPT(name='docx_page_margin_top', recommended_value=72.0,
|
||||
level=_OPT.LOW, help='The size of the top page margin, '
|
||||
'in pts. Default is 72pt. Overrides the common top page '
|
||||
'margin setting, unless set to zero.'),
|
||||
_OPT(name='docx_page_margin_right', recommended_value=72.0,
|
||||
level=_OPT.LOW, help='The size of the right page margin, '
|
||||
'in pts. Default is 72pt. Overrides the common right page '
|
||||
'margin setting, unless set to zero.'),
|
||||
_OPT(name='docx_page_margin_bottom', recommended_value=72.0,
|
||||
level=_OPT.LOW, help='The size of the bottom page margin, '
|
||||
'in pts. Default is 72pt. Overrides the common bottom '
|
||||
'page margin setting, unless set to zero.')}
|
||||
|
||||
def convert_metadata(self, oeb):
|
||||
from lxml import etree
|
||||
from ebook_converter.ebooks.oeb.base import OPF, OPF2_NS
|
||||
from ebook_converter.ebooks.metadata.opf2 import OPF as ReadOPF
|
||||
from io import BytesIO
|
||||
package = etree.Element(OPF('package'), attrib={'version': '2.0'}, nsmap={None: OPF2_NS})
|
||||
|
||||
package = etree.Element(base.tag('opf', 'package'),
|
||||
attrib={'version': '2.0'},
|
||||
nsmap={None: const.OPF2_NS})
|
||||
oeb.metadata.to_opf2(package)
|
||||
self.mi = ReadOPF(BytesIO(etree.tostring(package, encoding='utf-8')), populate_spine=False, try_to_guess_cover=False).to_book_metadata()
|
||||
self.mi = opf_meta.OPF(io.BytesIO(etree.tostring(package,
|
||||
encoding='utf-8')),
|
||||
populate_spine=False,
|
||||
try_to_guess_cover=False).to_book_metadata()
|
||||
|
||||
def convert(self, oeb, output_path, input_plugin, opts, log):
|
||||
from ebook_converter.ebooks.docx.writer.container import DOCX
|
||||
from ebook_converter.ebooks.docx.writer.from_html import Convert
|
||||
docx = DOCX(opts, log)
|
||||
self.convert_metadata(oeb)
|
||||
Convert(oeb, docx, self.mi, not opts.docx_no_cover, not opts.docx_no_toc)()
|
||||
Convert(oeb, docx, self.mi, not opts.docx_no_cover,
|
||||
not opts.docx_no_toc)()
|
||||
docx.write(output_path, self.mi)
|
||||
if opts.extract_to:
|
||||
from ebook_converter.ebooks.docx.dump import do_dump
|
||||
do_dump(output_path, opts.extract_to)
|
||||
|
||||
Reference in New Issue
Block a user