DOCX Input: Add option to not insert page breaks after endnotes

This commit is contained in:
m4mmon 2015-04-25 13:24:49 +02:00 committed by Kovid Goyal
parent b4a67e68ec
commit be4d44c3d8
5 changed files with 26 additions and 9 deletions

View File

@ -19,6 +19,9 @@ class DOCXInput(InputFormatPlugin):
help=_('Normally, if a large image is present at the start of the document that looks like a cover, ' help=_('Normally, if a large image is present at the start of the document that looks like a cover, '
'it will be removed from the document and used as the cover for created ebook. This option ' 'it will be removed from the document and used as the cover for created ebook. This option '
'turns off that behavior.')), 'turns off that behavior.')),
OptionRecommendation(name='docx_notes_nopb', recommended_value=False,
help=_('Normally, a page break is added after every footnote, '
'This option turns off that behavior.')),
} }
@ -26,5 +29,5 @@ class DOCXInput(InputFormatPlugin):
def convert(self, stream, options, file_ext, log, accelerators): def convert(self, stream, options, file_ext, log, accelerators):
from calibre.ebooks.docx.to_html import Convert from calibre.ebooks.docx.to_html import Convert
return Convert(stream, detect_cover=not options.docx_no_cover, log=log)() return Convert(stream, detect_cover=not options.docx_no_cover, log=log, notes_nopb=options.docx_notes_nopb)()

View File

@ -432,10 +432,10 @@ class Styles(object):
h = hash(frozenset(css.iteritems())) h = hash(frozenset(css.iteritems()))
return self.classes.get(h, (None, None))[0] return self.classes.get(h, (None, None))[0]
def generate_css(self, dest_dir, docx): def generate_css(self, dest_dir, docx, notes_nopb):
ef = self.fonts.embed_fonts(dest_dir, docx) ef = self.fonts.embed_fonts(dest_dir, docx)
prefix = textwrap.dedent(
'''\ s = '''\
body { font-family: %s; font-size: %s; color: %s } body { font-family: %s; font-size: %s; color: %s }
/* In word all paragraphs have zero margins unless explicitly specified in a style */ /* In word all paragraphs have zero margins unless explicitly specified in a style */
@ -456,8 +456,12 @@ class Styles(object):
dl.notes dt a { text-decoration: none } dl.notes dt a { text-decoration: none }
dl.notes dd { page-break-after: always } '''
if not notes_nopb:
s = s + 'dl.notes dd { page-break-after: always }'
s = s + '''\
dl.notes dd:last-of-type { page-break-after: avoid } dl.notes dd:last-of-type { page-break-after: avoid }
span.tab { white-space: pre } span.tab { white-space: pre }
@ -466,7 +470,9 @@ class Styles(object):
p.index-entry a:visited { color: blue } p.index-entry a:visited { color: blue }
p.index-entry a:hover { color: red } p.index-entry a:hover { color: red }
''') % (self.body_font_family, self.body_font_size, self.body_color) '''
prefix = textwrap.dedent(s) % (self.body_font_family, self.body_font_size, self.body_color)
if ef: if ef:
prefix = ef + '\n' + prefix prefix = ef + '\n' + prefix

View File

@ -50,7 +50,7 @@ def html_lang(docx_lang):
class Convert(object): class Convert(object):
def __init__(self, path_or_stream, dest_dir=None, log=None, detect_cover=True, notes_text=None): def __init__(self, path_or_stream, dest_dir=None, log=None, detect_cover=True, notes_text=None, notes_nopb=False):
self.docx = DOCX(path_or_stream, log=log) self.docx = DOCX(path_or_stream, log=log)
self.namespace = self.docx.namespace self.namespace = self.docx.namespace
self.ms_pat = re.compile(r'\s{2,}') self.ms_pat = re.compile(r'\s{2,}')
@ -58,6 +58,7 @@ class Convert(object):
self.log = self.docx.log self.log = self.docx.log
self.detect_cover = detect_cover self.detect_cover = detect_cover
self.notes_text = notes_text or _('Notes') self.notes_text = notes_text or _('Notes')
self.notes_nopb = notes_nopb
self.dest_dir = dest_dir or os.getcwdu() self.dest_dir = dest_dir or os.getcwdu()
self.mi = self.docx.metadata self.mi = self.docx.metadata
self.body = BODY() self.body = BODY()
@ -340,7 +341,7 @@ class Convert(object):
raw = html.tostring(self.html, encoding='utf-8', doctype='<!DOCTYPE html>') raw = html.tostring(self.html, encoding='utf-8', doctype='<!DOCTYPE html>')
with open(os.path.join(self.dest_dir, 'index.html'), 'wb') as f: with open(os.path.join(self.dest_dir, 'index.html'), 'wb') as f:
f.write(raw) f.write(raw)
css = self.styles.generate_css(self.dest_dir, self.docx) css = self.styles.generate_css(self.dest_dir, self.docx, self.notes_nopb)
if css: if css:
with open(os.path.join(self.dest_dir, 'docx.css'), 'wb') as f: with open(os.path.join(self.dest_dir, 'docx.css'), 'wb') as f:
f.write(css.encode('utf-8')) f.write(css.encode('utf-8'))

View File

@ -18,6 +18,6 @@ class PluginWidget(Widget, Ui_Form):
def __init__(self, parent, get_option, get_help, db=None, book_id=None): def __init__(self, parent, get_option, get_help, db=None, book_id=None):
Widget.__init__(self, parent, Widget.__init__(self, parent,
['docx_no_cover', ]) ['docx_no_cover', 'docx_notes_nopb'])
self.initialize_options(get_option, get_help, db, book_id) self.initialize_options(get_option, get_help, db, book_id)

View File

@ -21,6 +21,13 @@
</property> </property>
</widget> </widget>
</item> </item>
<item>
<widget class="QCheckBox" name="opt_docx_notes_nopb">
<property name="text">
<string>Do not add a page after before every footnote</string>
</property>
</widget>
</item>
<item> <item>
<spacer name="verticalSpacer"> <spacer name="verticalSpacer">
<property name="orientation"> <property name="orientation">