Debian Codegraph @

manual/template_ref_generate.py

raw
1 #!/usr/bin/env python
2 # vim:fileencoding=UTF-8:ts=4:sw=4:sta:et:sts=4:ai
3
4
5 __license__ = 'GPL v3'
6 __copyright__ = '2008, Kovid Goyal <kovid at kovidgoyal.net>'
7
8 from collections import defaultdict
9
10 PREAMBLE = '''\
11 .. _templaterefcalibre-{}:
12
13 Reference for all built-in template language functions
14 ========================================================
15
16 Here, we document all the built-in functions available in the calibre template
17 language. Every function is implemented as a class in python and you can click
18 the source links to see the source code, in case the documentation is
19 insufficient. The functions are arranged in logical groups by type.
20
21 .. contents::
22 :depth: 2
23 :local:
24
25 .. module:: calibre.utils.formatter_functions
26
27 '''
28
29 POSTAMBLE = '''\
30
31 API of the Metadata objects
32 ----------------------------
33
34 The python implementation of the template functions is passed in a Metadata
35 object. Knowing it's API is useful if you want to define your own template
36 functions.
37
38 .. module:: calibre.ebooks.metadata.book.base
39
40 .. autoclass:: Metadata
41 :members:
42 :member-order: bysource
43
44 .. data:: STANDARD_METADATA_FIELDS
45
46 The set of standard metadata fields.
47
48 .. literalinclude:: ../../../src/calibre/ebooks/metadata/book/__init__.py
49 :lines: 8-
50 '''
51
52
53 def generate_template_language_help(language, log):
54 from tempfile import TemporaryDirectory
55
56 from calibre.db.legacy import LibraryDatabase
57 from calibre.utils.ffml_processor import FFMLProcessor
58 from calibre.utils.formatter_functions import formatter_functions
59
60 output = [PREAMBLE.format(language)]
61 a = output.append
62
63 with TemporaryDirectory() as tdir:
64 db = LibraryDatabase(tdir) # needed to load formatter_funcs
65 ffml = FFMLProcessor()
66 all_funcs = formatter_functions().get_builtins()
67 categories = defaultdict(dict)
68 for name, func in all_funcs.items():
69 category = func.category
70 categories[category][name] = func
71 for cat_name in sorted(categories):
72 a(cat_name + '\n')
73 a(('-' * (4 * len(cat_name))) + '\n\n')
74 for name in sorted(categories[cat_name]):
75 func = categories[cat_name][name]
76 a(f"\n\n.. _ff_{name}:\n\n{name}\n{'^' * len(name)}\n\n")
77 a(f'.. class:: {func.__class__.__name__}\n\n')
78 try:
79 a(ffml.document_to_rst(func.doc, name))
80 except Exception as e:
81 if language in ('en', 'eng'):
82 raise
83 log.warn(f'Failed to process template language docs for {name} in the {language} language with error: {e}')
84 a(' TRANSLATION INVALID')
85 a('\n\n')
86 db.close()
87 del db
88
89 a(POSTAMBLE)
90 return ''.join(output)
91
92
93 if __name__ == '__main__':
94 generate_template_language_help()