get safely template function doc

This commit is contained in:
un-pogaz 2024-11-29 13:39:46 +01:00
parent 84d283399e
commit 1684647366

View File

@ -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 '<span style="color:red"><strong>MalformedDoc:</strong> '+msg+'</span>'
# 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+'<br>'+ rslt
except Exception as ex:
error_msg = build_error_msg(str(ex))
# return raw doc
return error_msg+'<br>'+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<h3>{name} ({self.function_type_string(name, longform=False)})</h3>\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: