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'] supported_platforms = ['windows', 'osx', 'linux']
on_import = True 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): def run(self, htmlfile):
import codecs import codecs
from calibre import prints 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 =[('debug_pipeline', tdir, OptionRecommendation.HIGH)]
recs.append(['keep_ligatures', True, OptionRecommendation.HIGH]) recs.append(['keep_ligatures', True, OptionRecommendation.HIGH])
if self.site_customization and self.site_customization.strip(): if self.site_customization and self.site_customization.strip():
sc = self.site_customization.strip() settings = self.parse_my_settings(self.site_customization)
enc, _, bf = sc.partition('|') enc = settings.get('encoding')
if enc: if enc:
try: try:
codecs.lookup(enc) 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) prints('Ignoring invalid input encoding for HTML:', enc)
else: else:
recs.append(['input_encoding', enc, OptionRecommendation.HIGH]) recs.append(['input_encoding', enc, OptionRecommendation.HIGH])
if bf == 'bf': if settings.get('breadth_first'):
recs.append(['breadth_first', True, recs.append(['breadth_first', True, OptionRecommendation.HIGH])
OptionRecommendation.HIGH])
gui_convert(htmlfile, tdir, recs, abort_after_input_dump=True) gui_convert(htmlfile, tdir, recs, abort_after_input_dump=True)
of = self.temporary_file('_plugin_html2zip.zip') of = self.temporary_file('_plugin_html2zip.zip')
tdir = os.path.join(tdir, 'input') 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): def customization_help(self, gui=False):
return _('Character encoding for the input HTML files. Common choices ' 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): 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 True if the user clicks OK, False otherwise. The changes are
automatically applied. automatically applied.
''' '''
import json
from qt.core import (QDialog, QDialogButtonBox, QVBoxLayout, from qt.core import (QDialog, QDialogButtonBox, QVBoxLayout,
QLabel, Qt, QLineEdit, QCheckBox) 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 ' ' 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. ' ' 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')) ' With this option, they will instead be added as A, B, C, D'))
sc = plugin_customization(self) settings = self.parse_my_settings(plugin_customization(self))
if not sc: bf.setChecked(bool(settings.get('breadth_first')))
sc = '' sc = QLineEdit(str(settings.get('encoding', '')), config_dialog)
sc = sc.strip()
enc = sc.partition('|')[0]
bfs = sc.partition('|')[-1]
bf.setChecked(bfs == 'bf')
sc = QLineEdit(enc, config_dialog)
v.addWidget(sc) v.addWidget(sc)
v.addWidget(bf) v.addWidget(bf)
v.addWidget(button_box) v.addWidget(button_box)
@ -112,9 +121,12 @@ every time you add an HTML file to the library.\
config_dialog.exec() config_dialog.exec()
if config_dialog.result() == QDialog.DialogCode.Accepted: 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(): if bf.isChecked():
sc += '|bf' settings['breadth_first'] = True
customize_plugin(self, sc) customize_plugin(self, json.dumps(settings, ensure_ascii=True))
return config_dialog.result() return config_dialog.result()