Skip to content

Commit

Permalink
Port the generation of template language function reference to new code
Browse files Browse the repository at this point in the history
Note that in other documentation, the function reference can be linked
to using

:ref:`ff_function_name`  for example, :ref:`ff_add`
  • Loading branch information
kovidgoyal committed Nov 11, 2024
1 parent 3687ad4 commit 96b521a
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 40 deletions.
2 changes: 1 addition & 1 deletion manual/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
templates_path = ['templates']

# The suffix of source filenames.
source_suffix = '.rst'
source_suffix = {'.rst': 'restructuredtext'}

# The master toctree document.
master_doc = 'index' if tags.has('online') else 'simple_index' # noqa
Expand Down
64 changes: 25 additions & 39 deletions manual/template_ref_generate.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
__license__ = 'GPL v3'
__copyright__ = '2008, Kovid Goyal <kovid at kovidgoyal.net>'

import re
from collections import defaultdict

PREAMBLE = '''\
Expand All @@ -27,20 +26,6 @@
'''

CATEGORY_TEMPLATE = '''\
{category}
{dashes}
'''

FUNCTION_TEMPLATE = '''\
{fs}
{hats}
.. autoclass:: {cn}
'''

POSTAMBLE = '''\
API of the Metadata objects
Expand All @@ -66,33 +51,34 @@


def generate_template_language_help(language):
from calibre.utils.formatter_functions import formatter_functions
pat = re.compile(r'\)`{0,2}\s*-{1,2}')

funcs = defaultdict(dict)
from tempfile import TemporaryDirectory

for func in formatter_functions().get_builtins().values():
class_name = func.__class__.__name__
func_sig = getattr(func, 'doc')
m = pat.search(func_sig)
if m is None:
print('No signature for template function ', class_name)
continue
func_sig = func_sig[:m.start()+1].strip('`')
func_cat = getattr(func, 'category')
funcs[func_cat][func_sig] = class_name
from calibre.db.legacy import LibraryDatabase
from calibre.utils.ffml_processor import FFMLProcessor
from calibre.utils.formatter_functions import formatter_functions

output = PREAMBLE.format(language)
cats = sorted(funcs.keys())
for cat in cats:
output += CATEGORY_TEMPLATE.format(category=cat, dashes='-'*len(cat))
entries = [k for k in sorted(funcs[cat].keys())]
for entry in entries:
output += FUNCTION_TEMPLATE.format(fs=entry, cn=funcs[cat][entry],
hats='^'*len(entry))

output += POSTAMBLE
return output

with TemporaryDirectory() as tdir:
db = LibraryDatabase(tdir) # needed to load formatter_funcs
ffml = FFMLProcessor()
all_funcs = formatter_functions().get_builtins()
categories = defaultdict(dict)
for name, func in all_funcs.items():
category = func.category
categories[category][name] = func
for cat_name in sorted(categories):
output += cat_name + '\n'
output += ('-' * (4*len(cat_name))) + '\n\n'
for name in sorted(categories[cat_name]):
func = categories[cat_name][name]
output += f"\n\n.. _ff_{name}:\n\n{name}\n{'^'*len(name)}\n\n"
output += f'.. class:: {func.__class__.__name__}\n\n'
output += ffml.document_to_rst(func.doc, name)
output += '\n\n'
del db

return output + POSTAMBLE

if __name__ == '__main__':
generate_template_language_help()

0 comments on commit 96b521a

Please sign in to comment.