diff --git a/src/calibre/gui2/dialogs/template_dialog.py b/src/calibre/gui2/dialogs/template_dialog.py
index eef0abd829..f171a50e54 100644
--- a/src/calibre/gui2/dialogs/template_dialog.py
+++ b/src/calibre/gui2/dialogs/template_dialog.py
@@ -62,6 +62,29 @@ from calibre.utils.localization import localize_user_manual_link, ngettext
from calibre.utils.resources import get_path as P
+def safe_get_doc_html(ffml, func, fname, original_doc):
+ def build_error_msg(msg):
+ return 'MalformedDoc: '+msg+''
+
+ # try with the original doc
+ try:
+ return ffml.document_to_html(original_doc, fname).strip()
+ except Exception as ex:
+ error_msg = build_error_msg(str(ex))
+
+ # try with english doc
+ doc = getattr(func, 'doc', '')
+ doc = getattr(doc, 'formatted_english', doc)
+ try:
+ rslt = ffml.document_to_html(doc, fname).strip()
+ return error_msg+'
'+ rslt
+ except Exception as ex:
+ error_msg = build_error_msg(str(ex))
+
+ # return raw doc
+ return error_msg+'
'+doc.strip()
+
+
class DocViewer(Dialog):
def __init__(self, ffml, builtins, function_type_string_method, parent=None):
@@ -140,9 +163,13 @@ class DocViewer(Dialog):
return f'\n
{name} ({self.function_type_string(name, longform=False)})
\n'
def get_doc(self, func):
- doc = func.doc if hasattr(func, 'doc') else ''
+ doc = getattr(func, 'doc', '')
return getattr(doc, 'formatted_english', doc) if self.english_cb.isChecked() else doc
+ def get_doc_html(self, func, fname):
+ doc = self.get_doc(func)
+ return safe_get_doc_html(self.ffml, func, fname, doc)
+
def no_doc_string(self):
if self.english_cb.isChecked():
return 'No documentation provided'
@@ -161,7 +188,7 @@ class DocViewer(Dialog):
else:
self.last_function = fname
self.set_html(self.header_line(fname) +
- self.ffml.document_to_html(self.get_doc(bif), fname))
+ self.get_doc_html(bif, fname))
def show_all_functions_button_clicked(self):
self.add_to_back_stack()
@@ -179,7 +206,7 @@ class DocViewer(Dialog):
if not doc:
a(self.no_doc_string())
else:
- html = self.ffml.document_to_html(doc.strip(), name)
+ html = self.get_doc_html(self.builtins[name], name)
name_pos = html.find(name + '(')
if name_pos < 0:
rest_of_doc = ' -- ' + html
@@ -1116,14 +1143,17 @@ def evaluate(book, context):
return (_('Stored user defined Python template') if longform else _('Stored template'))
return (_('Stored user defined GPM template') if longform else _('Stored template'))
+ def get_doc_html(self, func, fname):
+ doc = getattr(func, 'doc', '')
+ return safe_get_doc_html(self.ffml, func, fname, doc)
+
def function_changed(self, toWhat):
self.current_function_name = name = str(self.function.itemData(toWhat))
self.source_code.clear()
self.documentation.clear()
self.func_type.clear()
if name in self.all_functions:
- doc = self.all_functions[name].doc.strip()
- self.documentation.setHtml(self.ffml.document_to_html(doc, name))
+ self.documentation.setHtml(self.get_doc_html(self.all_functions[name], name))
if self.doc_viewer is not None:
self.doc_viewer.show_function(name)
if name in self.builtins and name in self.builtin_source_dict: