From e7575979713b9d990cf4d68bea72cc649f8e6abd Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Mon, 2 Dec 2019 20:26:36 +0530 Subject: [PATCH] Polish books: Add a tool to add or remove soft hyphens from the book text --- src/calibre/ebooks/oeb/polish/hyphenation.py | 30 ++++++++++++++++++++ src/calibre/ebooks/oeb/polish/main.py | 23 +++++++++++++++ src/calibre/gui2/actions/polish.py | 4 +++ 3 files changed, 57 insertions(+) create mode 100644 src/calibre/ebooks/oeb/polish/hyphenation.py diff --git a/src/calibre/ebooks/oeb/polish/hyphenation.py b/src/calibre/ebooks/oeb/polish/hyphenation.py new file mode 100644 index 0000000000..ae6da750c8 --- /dev/null +++ b/src/calibre/ebooks/oeb/polish/hyphenation.py @@ -0,0 +1,30 @@ +#!/usr/bin/env python2 +# vim:fileencoding=utf-8 +# License: GPL v3 Copyright: 2019, Kovid Goyal + +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')) diff --git a/src/calibre/ebooks/oeb/polish/main.py b/src/calibre/ebooks/oeb/polish/main.py index 0449532f51..815b114456 100644 --- a/src/calibre/ebooks/oeb/polish/main.py +++ b/src/calibre/ebooks/oeb/polish/main.py @@ -21,6 +21,7 @@ from calibre.ebooks.oeb.polish.replace import smarten_punctuation from calibre.ebooks.oeb.polish.jacket import ( 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.hyphenation import remove_soft_hyphens, add_soft_hyphens from calibre.utils.logging import Log from polyglot.builtins import iteritems @@ -35,6 +36,8 @@ ALL_OPTS = { 'remove_unused_css':False, 'compress_images': False, 'upgrade_book': False, + 'add_soft_hyphens': False, + 'remove_soft_hyphens': False, } CUSTOMIZATION = { @@ -118,6 +121,15 @@ affecting image quality.

Upgrade the internal structures of the book, if possible. For instance, upgrades EPUB 2 books to EPUB 3 books.

'''), + +'add_soft_hyphens': _('''\ +

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.

+'''), + +'remove_soft_hyphens': _('''\ +

Remove soft hyphens from all text in the book.

+'''), } @@ -237,6 +249,15 @@ def polish_one(ebook, opts, report, customization=None): changed = True 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 @@ -304,6 +325,8 @@ def option_parser(): o('--smarten-punctuation', '-p', help=CLI_HELP['smarten_punctuation']) o('--remove-unused-css', '-u', help=CLI_HELP['remove_unused_css']) 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('--verbose', help=_('Produce more verbose output, useful for debugging.')) diff --git a/src/calibre/gui2/actions/polish.py b/src/calibre/gui2/actions/polish.py index ee3389c480..821a97e361 100644 --- a/src/calibre/gui2/actions/polish.py +++ b/src/calibre/gui2/actions/polish.py @@ -68,6 +68,8 @@ class Polish(QDialog): # {{{ 'remove_jacket':_('

Remove book jacket

%s')%HELP['remove_jacket'], 'remove_unused_css':_('

Remove unused CSS rules

%s')%HELP['remove_unused_css'], 'compress_images': _('

Losslessly compress images

%s') % HELP['compress_images'], + 'add_soft_hyphens': _('

Add soft-hyphens

%s') % HELP['add_soft_hyphens'], + 'remove_soft_hyphens': _('

Remove soft-hyphens

%s') % HELP['remove_soft_hyphens'], 'upgrade_book': _('

Upgrade book internals

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