Polish books: Add a tool to add or remove soft hyphens from the book text

This commit is contained in:
Kovid Goyal 2019-12-02 20:26:36 +05:30
parent 6625560e81
commit e757597971
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
3 changed files with 57 additions and 0 deletions

View File

@ -0,0 +1,30 @@
#!/usr/bin/env python2
# vim:fileencoding=utf-8
# License: GPL v3 Copyright: 2019, Kovid Goyal <kovid at kovidgoyal.net>
from __future__ import absolute_import, division, print_function, unicode_literals
from calibre.ebooks.oeb.base import OEB_DOCS
from polyglot.builtins import iteritems
def add_soft_hyphens(container, report=None):
from calibre.utils.hyphenation.hyphenate import add_soft_hyphens_to_html
for name, mt in iteritems(container.mime_map):
if mt not in OEB_DOCS:
continue
add_soft_hyphens_to_html(container.parsed(name), container.mi.language)
container.dirty(name)
if report is not None:
report(_('Soft hyphens added'))
def remove_soft_hyphens(container, report=None):
from calibre.utils.hyphenation.hyphenate import remove_soft_hyphens_from_html
for name, mt in iteritems(container.mime_map):
if mt not in OEB_DOCS:
continue
remove_soft_hyphens_from_html(container.parsed(name))
container.dirty(name)
if report is not None:
report(_('Soft hyphens removed'))

View File

@ -21,6 +21,7 @@ from calibre.ebooks.oeb.polish.replace import smarten_punctuation
from calibre.ebooks.oeb.polish.jacket import ( from calibre.ebooks.oeb.polish.jacket import (
replace_jacket, add_or_replace_jacket, find_existing_jacket, remove_jacket) replace_jacket, add_or_replace_jacket, find_existing_jacket, remove_jacket)
from calibre.ebooks.oeb.polish.css import remove_unused_css from calibre.ebooks.oeb.polish.css import remove_unused_css
from calibre.ebooks.oeb.polish.hyphenation import remove_soft_hyphens, add_soft_hyphens
from calibre.utils.logging import Log from calibre.utils.logging import Log
from polyglot.builtins import iteritems from polyglot.builtins import iteritems
@ -35,6 +36,8 @@ ALL_OPTS = {
'remove_unused_css':False, 'remove_unused_css':False,
'compress_images': False, 'compress_images': False,
'upgrade_book': False, 'upgrade_book': False,
'add_soft_hyphens': False,
'remove_soft_hyphens': False,
} }
CUSTOMIZATION = { CUSTOMIZATION = {
@ -118,6 +121,15 @@ affecting image quality.</p>
<p>Upgrade the internal structures of the book, if possible. For instance, <p>Upgrade the internal structures of the book, if possible. For instance,
upgrades EPUB 2 books to EPUB 3 books.</p> upgrades EPUB 2 books to EPUB 3 books.</p>
'''), '''),
'add_soft_hyphens': _('''\
<p>Add soft hyphens to all words in the book. This allows the book to be rendered
better when the text is justified, in readers that do not support hyphenation.</p>
'''),
'remove_soft_hyphens': _('''\
<p>Remove soft hyphens from all text in the book.</p>
'''),
} }
@ -237,6 +249,15 @@ def polish_one(ebook, opts, report, customization=None):
changed = True changed = True
report('') report('')
if opts.remove_soft_hyphens:
rt(_('Removing soft hyphens'))
remove_soft_hyphens(ebook, report)
changed = True
elif opts.add_soft_hyphens:
rt(_('Adding soft hyphens'))
add_soft_hyphens(ebook, report)
changed = True
return changed return changed
@ -304,6 +325,8 @@ def option_parser():
o('--smarten-punctuation', '-p', help=CLI_HELP['smarten_punctuation']) o('--smarten-punctuation', '-p', help=CLI_HELP['smarten_punctuation'])
o('--remove-unused-css', '-u', help=CLI_HELP['remove_unused_css']) o('--remove-unused-css', '-u', help=CLI_HELP['remove_unused_css'])
o('--compress-images', '-i', help=CLI_HELP['compress_images']) o('--compress-images', '-i', help=CLI_HELP['compress_images'])
o('--add-soft-hyphens', '-H', help=CLI_HELP['add_soft_hyphens'])
o('--remove-soft-hyphens', help=CLI_HELP['remove_soft_hyphens'])
o('--upgrade-book', '-U', help=CLI_HELP['upgrade_book']) o('--upgrade-book', '-U', help=CLI_HELP['upgrade_book'])
o('--verbose', help=_('Produce more verbose output, useful for debugging.')) o('--verbose', help=_('Produce more verbose output, useful for debugging.'))

View File

@ -68,6 +68,8 @@ class Polish(QDialog): # {{{
'remove_jacket':_('<h3>Remove book jacket</h3>%s')%HELP['remove_jacket'], 'remove_jacket':_('<h3>Remove book jacket</h3>%s')%HELP['remove_jacket'],
'remove_unused_css':_('<h3>Remove unused CSS rules</h3>%s')%HELP['remove_unused_css'], 'remove_unused_css':_('<h3>Remove unused CSS rules</h3>%s')%HELP['remove_unused_css'],
'compress_images': _('<h3>Losslessly compress images</h3>%s') % HELP['compress_images'], 'compress_images': _('<h3>Losslessly compress images</h3>%s') % HELP['compress_images'],
'add_soft_hyphens': _('<h3>Add soft-hyphens</h3>%s') % HELP['add_soft_hyphens'],
'remove_soft_hyphens': _('<h3>Remove soft-hyphens</h3>%s') % HELP['remove_soft_hyphens'],
'upgrade_book': _('<h3>Upgrade book internals</h3>%s') % HELP['upgrade_book'], 'upgrade_book': _('<h3>Upgrade book internals</h3>%s') % HELP['upgrade_book'],
} }
@ -88,6 +90,8 @@ class Polish(QDialog): # {{{
('remove_jacket', _('&Remove a previously inserted book jacket')), ('remove_jacket', _('&Remove a previously inserted book jacket')),
('remove_unused_css', _('Remove &unused CSS rules from the book')), ('remove_unused_css', _('Remove &unused CSS rules from the book')),
('compress_images', _('Losslessly &compress images')), ('compress_images', _('Losslessly &compress images')),
('add_soft_hyphens', _('Add s&oft hyphens')),
('remove_soft_hyphens', _('Remove soft hyphens')),
('upgrade_book', _('&Upgrade book internals')), ('upgrade_book', _('&Upgrade book internals')),
]) ])
prefs = gprefs.get('polishing_settings', {}) prefs = gprefs.get('polishing_settings', {})