From 166bb9fbb00c3e9039076baf5c7f4f74ea6bf715 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Tue, 9 Nov 2021 20:58:36 +0530 Subject: [PATCH] More work on HTML transform tool --- src/calibre/ebooks/html_transform_rules.py | 6 ++-- src/calibre/gui2/html_transform_rules.py | 37 ++++++++++------------ src/calibre/gui2/tag_mapper.py | 3 +- 3 files changed, 21 insertions(+), 25 deletions(-) diff --git a/src/calibre/ebooks/html_transform_rules.py b/src/calibre/ebooks/html_transform_rules.py index 351948ed2b..ee1e2489f3 100644 --- a/src/calibre/ebooks/html_transform_rules.py +++ b/src/calibre/ebooks/html_transform_rules.py @@ -78,14 +78,12 @@ def transform_container(container, serialized_rules, names=()): def rule_to_text(rule): - def get(prop): - return rule.get(prop) or '' text = _('If the tag {match_type} {query}').format( - match_type=MATCH_TYPE_MAP[rule['match_type']].short_text, query=get('query')) + match_type=MATCH_TYPE_MAP[rule['match_type']].text, query=rule.get('query') or '') for action in rule['actions']: text += '\n' text += _('{action_type} {action_data}').format( - action_type=ACTION_MAP[action['type']].short_text, action_data=action['data']) + action_type=ACTION_MAP[action['type']].short_text, action_data=action.get('data') or '') return text diff --git a/src/calibre/gui2/html_transform_rules.py b/src/calibre/gui2/html_transform_rules.py index a6cb87734f..13e9e15ec8 100644 --- a/src/calibre/gui2/html_transform_rules.py +++ b/src/calibre/gui2/html_transform_rules.py @@ -9,6 +9,7 @@ from qt.core import ( QVBoxLayout, QWidget, pyqtSignal ) +from calibre import prepare_string_for_xml from calibre.ebooks.html_transform_rules import ( ACTION_MAP, MATCH_TYPE_MAP, compile_rules, export_rules, import_rules, validate_rule @@ -249,13 +250,13 @@ class RuleItem(RuleItemBase): # {{{ def text_from_rule(rule, parent): try: query = elided_text(rule['query'], font=parent.font(), width=200, pos='right') - text = _( - 'If the property {property} {match_type} {query}
{action}').format( - property=rule['property'], action=ACTION_MAP[rule['action']], - match_type=MATCH_TYPE_MAP[rule['match_type']].text, query=query) - if rule['action_data']: - ad = elided_text(rule['action_data'], font=parent.font(), width=200, pos='right') - text += ' %s' % ad + text = _('If the tag {match_type} {query}').format( + match_type=MATCH_TYPE_MAP[rule['match_type']].text, query=prepare_string_for_xml(query)) + for action in rule['actions']: + text += '
' + ACTION_MAP[action['type']].short_text + if action.get('data'): + ad = elided_text(action['data'], font=parent.font(), width=200, pos='right') + text += f'{prepare_string_for_xml(ad)}' except Exception: import traceback traceback.print_exc() @@ -268,7 +269,7 @@ class Rules(RulesBase): # {{{ RuleItemClass = RuleItem RuleEditDialogClass = RuleEditDialog - + ACTION_KEY = 'actions' MSG = _('You can specify rules to transform styles here. Click the "Add rule" button' ' below to get started.') # }}} @@ -416,15 +417,11 @@ class RulesWidget(QWidget, SaveLoadMixin): # {{{ if __name__ == '__main__': from calibre.gui2 import Application app = Application([]) - v = RuleEdit() - v.setWindowFlag(Qt.WindowType.Dialog) - v.show() - app.exec_() - # d = RulesDialog() - # d.rules = [ - # {'match_type':'*', 'query':'', 'action':'change', 'action_data':'green'}, - # ] - # d.exec_() - # from pprint import pprint - # pprint(d.rules) - # del d, app + d = RulesDialog() + d.rules = [ + {'match_type':'*', 'query':'', 'actions':[{'type': 'remove'}]}, + ] + d.exec_() + from pprint import pprint + pprint(d.rules) + del d, app diff --git a/src/calibre/gui2/tag_mapper.py b/src/calibre/gui2/tag_mapper.py index 5d35351c53..452419bb75 100644 --- a/src/calibre/gui2/tag_mapper.py +++ b/src/calibre/gui2/tag_mapper.py @@ -269,6 +269,7 @@ class Rules(QWidget): RuleEditDialogClass = RuleEditDialog changed = pyqtSignal() + ACTION_KEY = 'action' MSG = _('You can specify rules to filter/transform tags here. Click the "Add rule" button' ' below to get started. The rules will be processed in order for every tag until either a' ' "remove" or a "keep" rule matches.') @@ -380,7 +381,7 @@ class Rules(QWidget): def rules(self, rules): self.rule_list.clear() for rule in rules: - if 'action' in rule and 'match_type' in rule and 'query' in rule: + if self.ACTION_KEY in rule and 'match_type' in rule and 'query' in rule: self.RuleItemClass(rule, self.rule_list)