Skip to content

Commit

Permalink
Allow using HTML transform rules during conversion as well
Browse files Browse the repository at this point in the history
  • Loading branch information
kovidgoyal committed Nov 11, 2021
1 parent 9f2c6a9 commit 90aac28
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 11 deletions.
24 changes: 22 additions & 2 deletions src/calibre/ebooks/conversion/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ def option_recommendation_to_cli_option(add_option, rec):
attrs.pop('type', '')
if opt.name == 'read_metadata_from_opf':
switches.append('--from-opf')
if opt.name == 'transform_css_rules':
elif opt.name == 'transform_css_rules':
attrs['help'] = _(
'Path to a file containing rules to transform the CSS styles'
' in this book. The easiest way to create such a file is to'
Expand All @@ -110,6 +110,15 @@ def option_recommendation_to_cli_option(add_option, rec):
' dialog. Once you create the rules, you can use the "Export" button'
' to save them to a file.'
)
elif opt.name == 'transform_html_rules':
attrs['help'] = _(
'Path to a file containing rules to transform the HTML'
' in this book. The easiest way to create such a file is to'
' use the wizard for creating rules in the calibre GUI. Access'
' it in the "Look & feel->Transform HTML" section of the conversion'
' dialog. Once you create the rules, you can use the "Export" button'
' to save them to a file.'
)
if opt.name in DEFAULT_TRUE_OPTIONS and rec.recommended_value is True:
switches = ['--disable-'+opt.long_switch]
add_option(Option(*switches, **attrs))
Expand Down Expand Up @@ -192,7 +201,7 @@ def add_pipeline_options(parser, plumber):
'font_size_mapping', 'embed_font_family',
'subset_embedded_fonts', 'embed_all_fonts',
'line_height', 'minimum_line_height',
'linearize_tables',
'linearize_tables', 'transform_html_rules',
'extra_css', 'filter_css', 'transform_css_rules', 'expand_css',
'smarten_punctuation', 'unsmarten_punctuation',
'margin_top', 'margin_left', 'margin_right',
Expand Down Expand Up @@ -388,6 +397,17 @@ def main(args=sys.argv):
log.error(title)
log.error(msg)
return 1
if opts.transform_html_rules:
from calibre.ebooks.html_transform_rules import import_rules, validate_rule
with open(opts.transform_html_rules, 'rb') as tcr:
opts.transform_html_rules = rules = list(import_rules(tcr.read()))
for rule in rules:
title, msg = validate_rule(rule)
if title and msg:
log.error('Failed to parse HTML transform rules')
log.error(title)
log.error(msg)
return 1

recommendations = [(n.dest, getattr(opts, n.dest),
OptionRecommendation.HIGH)
Expand Down
2 changes: 1 addition & 1 deletion src/calibre/ebooks/conversion/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ def get_sorted_output_formats(preferred_fmt=None):
'remove_paragraph_spacing', 'remove_paragraph_spacing_indent_size',
'insert_blank_line_size', 'input_encoding', 'filter_css',
'expand_css', 'asciiize', 'keep_ligatures', 'linearize_tables',
'transform_css_rules'),
'transform_css_rules', 'transform_html_rules'),

'metadata': ('prefer_metadata_cover',),

Expand Down
15 changes: 14 additions & 1 deletion src/calibre/ebooks/conversion/plumber.py
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,12 @@ def __init__(self, input, output, log, report_progress=DummyReporter(),
' rules are applied after all other CSS processing is done.')
),

OptionRecommendation(name='transform_html_rules',
recommended_value=None, level=OptionRecommendation.LOW,
help=_('Rules for transforming the HTML in this book. These'
' rules are applied after the HTML is parsed, but before any other transformations.')
),

OptionRecommendation(name='filter_css',
recommended_value=None, level=OptionRecommendation.LOW,
help=_('A comma separated list of CSS properties that '
Expand Down Expand Up @@ -881,7 +887,7 @@ def eq(name, a, b):
if name in {'sr1_search', 'sr1_replace', 'sr2_search', 'sr2_replace', 'sr3_search', 'sr3_replace', 'filter_css', 'comments'}:
if not a and not b:
return True
if name in {'transform_css_rules', 'search_replace'}:
if name in {'transform_css_rules', 'transform_html_rules', 'search_replace'}:
if b == '[]':
b = None
return a == b
Expand Down Expand Up @@ -1133,6 +1139,13 @@ def run(self):

self.oeb.plumber_output_format = self.output_fmt or ''

if self.opts.transform_html_rules:
transform_html_rules = self.opts.transform_html_rules
if isinstance(transform_html_rules, string_or_bytes):
transform_html_rules = json.loads(transform_html_rules)
from calibre.ebooks.html_transform_rules import transform_conversion_book
transform_conversion_book(self.oeb, self.opts, transform_html_rules)

from calibre.ebooks.oeb.transforms.data_url import DataURL
DataURL()(self.oeb, self.opts)
from calibre.ebooks.oeb.transforms.guide import Clean
Expand Down
9 changes: 9 additions & 0 deletions src/calibre/ebooks/html_transform_rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,15 @@ def transform_container(container, serialized_rules, names=()):
return doc_changed


def transform_conversion_book(oeb, opts, serialized_rules):
rules = tuple(Rule(r) for r in serialized_rules)
for item in oeb.spine:
root = item.data
if not hasattr(root, 'xpath'):
continue
transform_doc(root, rules)


def rule_to_text(rule):
text = _('If the tag {match_type} {query}').format(
match_type=MATCH_TYPE_MAP[rule['match_type']].text, query=rule.get('query') or '')
Expand Down
6 changes: 3 additions & 3 deletions src/calibre/gui2/convert/look_and_feel.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def get_value_handler(self, g):
val = str(g.text()).strip()
val = [x.strip() for x in val.split(',' if ',' in val else ' ') if x.strip()]
return ', '.join(val) or None
if g is self.opt_transform_css_rules:
if g is self.opt_transform_css_rules or g is self.opt_transform_html_rules:
return json.dumps(g.rules)
return Widget.get_value_handler(self, g)

Expand All @@ -95,7 +95,7 @@ def set_value_handler(self, g, val):
w.setChecked(False)
self.filter_css_others.setText(', '.join(items))
return True
if g is self.opt_transform_css_rules:
if g is self.opt_transform_css_rules or g is self.opt_transform_html_rules:
g.rules = json.loads(val) if val else []
return True

Expand All @@ -106,7 +106,7 @@ def connect_gui_obj_handler(self, gui_obj, slot):
w.stateChanged.connect(slot)
self.filter_css_others.textChanged.connect(slot)
return
if gui_obj is self.opt_transform_css_rules:
if gui_obj is self.opt_transform_css_rules or gui_obj is self.opt_transform_html_rules:
gui_obj.changed.connect(slot)
return
raise NotImplementedError()
Expand Down
18 changes: 17 additions & 1 deletion src/calibre/gui2/convert/look_and_feel.ui
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<height>619</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayout_2">
<layout class="QHBoxLayout" name="horizontalLayout_2">
<property name="spacing">
<number>0</number>
</property>
Expand Down Expand Up @@ -503,6 +503,16 @@
</item>
</layout>
</widget>
<widget class="QWidget" name="tab_5">
<attribute name="title">
<string>Transform &amp;HTML</string>
</attribute>
<layout class="QVBoxLayout" name="verticalLayout_2">
<item>
<widget class="HtmlRulesWidget" name="opt_transform_html_rules" native="true"/>
</item>
</layout>
</widget>
</widget>
</item>
</layout>
Expand All @@ -524,6 +534,12 @@
<header>calibre/gui2/css_transform_rules.h</header>
<container>1</container>
</customwidget>
<customwidget>
<class>HtmlRulesWidget</class>
<extends>QWidget</extends>
<header>calibre/gui2/html_transform_rules.h</header>
<container>1</container>
</customwidget>
</customwidgets>
<resources>
<include location="../../../../resources/images.qrc"/>
Expand Down
6 changes: 4 additions & 2 deletions src/calibre/gui2/css_transform_rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -317,15 +317,17 @@ def export_rules(self):
'There are no rules to export'), show=True)
path = choose_save_file(self, self.DIR_SAVE_NAME, _('Choose file for exported rules'), initial_filename=self.INITIAL_FILE_NAME)
if path:
raw = self.export_func(rules)
f = self.__class__.export_func
raw = f(rules)
with open(path, 'wb') as f:
f.write(raw)

def import_rules(self):
paths = choose_files(self, self.DIR_SAVE_NAME, _('Choose file to import rules from'), select_only_single_file=True)
if paths:
func = self.__class__.import_func
with open(paths[0], 'rb') as f:
rules = self.import_func(f.read())
rules = func(f.read())
self.rules_widget.rules = list(rules) + list(self.rules_widget.rules)
self.changed.emit()

Expand Down
2 changes: 1 addition & 1 deletion src/calibre/gui2/html_transform_rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ def transform_scope(self, val):
# }}}


class RulesWidget(RulesWidgetBase): # {{{
class HtmlRulesWidget(RulesWidgetBase): # {{{
PREFS_NAME = 'html-transform-rules'
INITIAL_FILE_NAME = 'html-rules.txt'
DIR_SAVE_NAME = 'export-html-transform-rules'
Expand Down

0 comments on commit 90aac28

Please sign in to comment.