From 2da3c9d30ba99ef5bf3d6ff2f0179657bfe8fdf0 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Thu, 10 Mar 2016 11:04:04 +0530 Subject: [PATCH] Edit Book: A new tool to transform book styling using easy to create rules. For example, you can create rules to change colors in the book or to double the font size of all text in the book, etc. For details, see https://manual.calibre-ebook.com/edit.html#transforming-css-properties --- manual/edit.rst | 15 +++++++++++++++ src/calibre/gui2/tweak_book/boss.py | 29 +++++++++++++++++++++++++++++ src/calibre/gui2/tweak_book/ui.py | 3 +++ 3 files changed, 47 insertions(+) diff --git a/manual/edit.rst b/manual/edit.rst index 11adc43dd7..661d4943e7 100644 --- a/manual/edit.rst +++ b/manual/edit.rst @@ -375,6 +375,21 @@ Note that the algorithm can sometimes generate incorrect results, especially when single quotes at the start of contractions are involved. Accessed via :guilabel:`Tools->Smarten punctuation`. +Transforming CSS properties +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +Create rules to transform the stying of the book. For example, create a rule +to convert all red text to green or to double the font size of all text in the +book or make text of a certain font family italic, etc. + +Creating the rules is simple, the rules follow a natural language format, that +looks like: + + * If the property *color* is *red* *change* it to *green* + * If the property *font-size* is *any value* *multiply* the value by *2* + +Accessed via :guilabel:`Tools->Transform styles`. + Removing unused CSS rules ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/src/calibre/gui2/tweak_book/boss.py b/src/calibre/gui2/tweak_book/boss.py index 0941cbbb2c..a4fb5fcd95 100644 --- a/src/calibre/gui2/tweak_book/boss.py +++ b/src/calibre/gui2/tweak_book/boss.py @@ -50,6 +50,7 @@ from calibre.utils.config import JSONConfig from calibre.utils.icu import numeric_sort_key _diff_dialogs = [] +last_used_transform_rules = [] def get_container(*args, **kwargs): kwargs['tweak_mode'] = True @@ -543,6 +544,34 @@ class Boss(QObject): self.rewind_savepoint() show_report(changed, self.current_metadata.title, report, parent or self.gui, self.show_current_diff) + def transform_styles(self): + global last_used_transform_rules + if not self.ensure_book(_('You must first open a book in order to transform styles.')): + return + from calibre.gui2.css_transform_rules import RulesDialog + from calibre.ebooks.css_transform_rules import transform_container + d = RulesDialog(self.gui) + d.rules = last_used_transform_rules + ret = d.exec_() + last_used_transform_rules = d.rules + if ret != d.Accepted: + return + with BusyCursor(): + self.add_savepoint(_('Before style transformation')) + try: + changed = transform_container(current_container(), last_used_transform_rules) + except: + self.rewind_savepoint() + raise + if changed: + self.apply_container_update_to_gui() + if not changed: + self.rewind_savepoint() + info_dialog(self.gui, _('No changes'), _( + 'No styles were changed.'), show=True) + return + self.show_current_diff() + def manage_fonts(self): self.commit_all_editors_to_container() self.gui.manage_fonts.display() diff --git a/src/calibre/gui2/tweak_book/ui.py b/src/calibre/gui2/tweak_book/ui.py index 15d2339351..ec8b4fab3e 100644 --- a/src/calibre/gui2/tweak_book/ui.py +++ b/src/calibre/gui2/tweak_book/ui.py @@ -392,6 +392,8 @@ class Main(MainWindow): 'Check external links in the book')) self.action_compress_images = treg('compress-image.png', _('Compress &images losslessly'), self.boss.compress_images, 'compress-images', (), _( 'Compress images losslessly')) + self.action_transform_styles = treg('wizard.png', _('Transform &styles'), self.boss.transform_styles, 'transform-styles', (), _( + 'Transform styles used in the book')) def ereg(icon, text, target, sid, keys, description): return reg(icon, text, partial(self.boss.editor_action, target), sid, keys, description) @@ -543,6 +545,7 @@ class Main(MainWindow): e.addAction(self.action_compress_images) e.addAction(self.action_smarten_punctuation) e.addAction(self.action_remove_unused_css) + e.addAction(self.action_transform_styles) e.addAction(self.action_fix_html_all) e.addAction(self.action_pretty_all) e.addAction(self.action_rationalize_folders)