Switch to storing HTML to ZIP customization as JSON for easy expansion in the future

This commit is contained in:
Kovid Goyal 2023-05-29 10:23:59 +05:30
parent c323aeb698
commit 3d96e98528
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C

View File

@ -24,6 +24,20 @@ every time you add an HTML file to the library.\
supported_platforms = ['windows', 'osx', 'linux']
on_import = True
def parse_my_settings(self, sc):
if not sc:
sc = ''
if sc.startswith('{'):
import json
try:
return json.loads(sc)
except Exception:
return {}
else:
sc = sc.strip()
enc, _, bfs = sc.partition('|')
return {'encoding': enc, 'breadth_first': bfs == 'bf'}
def run(self, htmlfile):
import codecs
from calibre import prints
@ -36,8 +50,8 @@ every time you add an HTML file to the library.\
recs =[('debug_pipeline', tdir, OptionRecommendation.HIGH)]
recs.append(['keep_ligatures', True, OptionRecommendation.HIGH])
if self.site_customization and self.site_customization.strip():
sc = self.site_customization.strip()
enc, _, bf = sc.partition('|')
settings = self.parse_my_settings(self.site_customization)
enc = settings.get('encoding')
if enc:
try:
codecs.lookup(enc)
@ -45,9 +59,8 @@ every time you add an HTML file to the library.\
prints('Ignoring invalid input encoding for HTML:', enc)
else:
recs.append(['input_encoding', enc, OptionRecommendation.HIGH])
if bf == 'bf':
recs.append(['breadth_first', True,
OptionRecommendation.HIGH])
if settings.get('breadth_first'):
recs.append(['breadth_first', True, OptionRecommendation.HIGH])
gui_convert(htmlfile, tdir, recs, abort_after_input_dump=True)
of = self.temporary_file('_plugin_html2zip.zip')
tdir = os.path.join(tdir, 'input')
@ -63,7 +76,7 @@ every time you add an HTML file to the library.\
def customization_help(self, gui=False):
return _('Character encoding for the input HTML files. Common choices '
'include: cp1252, cp1251, latin1 and utf-8.')
'include: utf-8, cp1252, cp1251 and latin1.')
def do_user_config(self, parent=None):
'''
@ -71,6 +84,7 @@ every time you add an HTML file to the library.\
True if the user clicks OK, False otherwise. The changes are
automatically applied.
'''
import json
from qt.core import (QDialog, QDialogButtonBox, QVBoxLayout,
QLabel, Qt, QLineEdit, QCheckBox)
@ -97,14 +111,9 @@ every time you add an HTML file to the library.\
' calibre does it depth first, i.e. if file A links to B and '
' C, but B links to D, the files are added in the order A, B, D, C. '
' With this option, they will instead be added as A, B, C, D'))
sc = plugin_customization(self)
if not sc:
sc = ''
sc = sc.strip()
enc = sc.partition('|')[0]
bfs = sc.partition('|')[-1]
bf.setChecked(bfs == 'bf')
sc = QLineEdit(enc, config_dialog)
settings = self.parse_my_settings(plugin_customization(self))
bf.setChecked(bool(settings.get('breadth_first')))
sc = QLineEdit(str(settings.get('encoding', '')), config_dialog)
v.addWidget(sc)
v.addWidget(bf)
v.addWidget(button_box)
@ -112,9 +121,12 @@ every time you add an HTML file to the library.\
config_dialog.exec()
if config_dialog.result() == QDialog.DialogCode.Accepted:
sc = str(sc.text()).strip()
settings = {}
enc = str(sc.text()).strip()
if enc:
settings['encoding'] = enc
if bf.isChecked():
sc += '|bf'
customize_plugin(self, sc)
settings['breadth_first'] = True
customize_plugin(self, json.dumps(settings, ensure_ascii=True))
return config_dialog.result()