More work on HTML transform tool

This commit is contained in:
Kovid Goyal 2021-11-09 20:58:36 +05:30
parent 8b85aa82d9
commit 166bb9fbb0
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
3 changed files with 21 additions and 25 deletions

View File

@ -78,14 +78,12 @@ def transform_container(container, serialized_rules, names=()):
def rule_to_text(rule): def rule_to_text(rule):
def get(prop):
return rule.get(prop) or ''
text = _('If the tag {match_type} {query}').format( 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']: for action in rule['actions']:
text += '\n' text += '\n'
text += _('{action_type} {action_data}').format( 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 return text

View File

@ -9,6 +9,7 @@ from qt.core import (
QVBoxLayout, QWidget, pyqtSignal QVBoxLayout, QWidget, pyqtSignal
) )
from calibre import prepare_string_for_xml
from calibre.ebooks.html_transform_rules import ( from calibre.ebooks.html_transform_rules import (
ACTION_MAP, MATCH_TYPE_MAP, compile_rules, export_rules, import_rules, ACTION_MAP, MATCH_TYPE_MAP, compile_rules, export_rules, import_rules,
validate_rule validate_rule
@ -249,13 +250,13 @@ class RuleItem(RuleItemBase): # {{{
def text_from_rule(rule, parent): def text_from_rule(rule, parent):
try: try:
query = elided_text(rule['query'], font=parent.font(), width=200, pos='right') query = elided_text(rule['query'], font=parent.font(), width=200, pos='right')
text = _( text = _('If the tag <b>{match_type}</b> <b>{query}</b>').format(
'If the property <i>{property}</i> <b>{match_type}</b> <b>{query}</b><br>{action}').format( match_type=MATCH_TYPE_MAP[rule['match_type']].text, query=prepare_string_for_xml(query))
property=rule['property'], action=ACTION_MAP[rule['action']], for action in rule['actions']:
match_type=MATCH_TYPE_MAP[rule['match_type']].text, query=query) text += '<br>' + ACTION_MAP[action['type']].short_text
if rule['action_data']: if action.get('data'):
ad = elided_text(rule['action_data'], font=parent.font(), width=200, pos='right') ad = elided_text(action['data'], font=parent.font(), width=200, pos='right')
text += ' <code>%s</code>' % ad text += f'<code>{prepare_string_for_xml(ad)}</code>'
except Exception: except Exception:
import traceback import traceback
traceback.print_exc() traceback.print_exc()
@ -268,7 +269,7 @@ class Rules(RulesBase): # {{{
RuleItemClass = RuleItem RuleItemClass = RuleItem
RuleEditDialogClass = RuleEditDialog RuleEditDialogClass = RuleEditDialog
ACTION_KEY = 'actions'
MSG = _('You can specify rules to transform styles here. Click the "Add rule" button' MSG = _('You can specify rules to transform styles here. Click the "Add rule" button'
' below to get started.') ' below to get started.')
# }}} # }}}
@ -416,15 +417,11 @@ class RulesWidget(QWidget, SaveLoadMixin): # {{{
if __name__ == '__main__': if __name__ == '__main__':
from calibre.gui2 import Application from calibre.gui2 import Application
app = Application([]) app = Application([])
v = RuleEdit() d = RulesDialog()
v.setWindowFlag(Qt.WindowType.Dialog) d.rules = [
v.show() {'match_type':'*', 'query':'', 'actions':[{'type': 'remove'}]},
app.exec_() ]
# d = RulesDialog() d.exec_()
# d.rules = [ from pprint import pprint
# {'match_type':'*', 'query':'', 'action':'change', 'action_data':'green'}, pprint(d.rules)
# ] del d, app
# d.exec_()
# from pprint import pprint
# pprint(d.rules)
# del d, app

View File

@ -269,6 +269,7 @@ class Rules(QWidget):
RuleEditDialogClass = RuleEditDialog RuleEditDialogClass = RuleEditDialog
changed = pyqtSignal() changed = pyqtSignal()
ACTION_KEY = 'action'
MSG = _('You can specify rules to filter/transform tags here. Click the "Add rule" button' 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' ' below to get started. The rules will be processed in order for every tag until either a'
' "remove" or a "keep" rule matches.') ' "remove" or a "keep" rule matches.')
@ -380,7 +381,7 @@ class Rules(QWidget):
def rules(self, rules): def rules(self, rules):
self.rule_list.clear() self.rule_list.clear()
for rule in rules: 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) self.RuleItemClass(rule, self.rule_list)