Make function signature detection more robust when generating template langauge help for the User Manual.

This commit is contained in:
Kovid Goyal 2014-10-17 09:17:48 +05:30
parent 42ca1149e5
commit e8304084df

View File

@ -1,9 +1,11 @@
#!/usr/bin/env python
# vim:fileencoding=UTF-8:ts=4:sw=4:sta:et:sts=4:ai
from __future__ import print_function
__license__ = 'GPL v3'
__copyright__ = '2008, Kovid Goyal <kovid at kovidgoyal.net>'
import re
from collections import defaultdict
PREAMBLE = '''\
@ -12,7 +14,10 @@ PREAMBLE = '''\
Reference for all built-in template language functions
========================================================
Here, we document all the built-in functions available in the calibre template language. Every function is implemented as a class in python and you can click the source links to see the source code, in case the documentation is insufficient. The functions are arranged in logical groups by type.
Here, we document all the built-in functions available in the calibre template
language. Every function is implemented as a class in python and you can click
the source links to see the source code, in case the documentation is
insufficient. The functions are arranged in logical groups by type.
.. contents::
:depth: 2
@ -41,7 +46,9 @@ POSTAMBLE = '''\
API of the Metadata objects
----------------------------
The python implementation of the template functions is passed in a Metadata object. Knowing it's API is useful if you want to define your own template functions.
The python implementation of the template functions is passed in a Metadata
object. Knowing it's API is useful if you want to define your own template
functions.
.. module:: calibre.ebooks.metadata.book.base
@ -60,17 +67,18 @@ The python implementation of the template functions is passed in a Metadata obje
def generate_template_language_help(language):
from calibre.utils.formatter_functions import formatter_functions
pat = re.compile(r'\)\s*-{1,2}')
funcs = defaultdict(dict)
for func in formatter_functions().get_builtins().values():
class_name = func.__class__.__name__
func_sig = getattr(func, 'doc')
x = func_sig.find(' -- ')
if x < 0:
print 'No sig for ', class_name
m = pat.search(func_sig)
if m is None:
print ('No signature for template function ', class_name)
continue
func_sig = func_sig[:x]
func_sig = func_sig[:m.start()+1]
func_cat = getattr(func, 'category')
funcs[func_cat][func_sig] = class_name
@ -80,7 +88,7 @@ def generate_template_language_help(language):
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],
output += FUNCTION_TEMPLATE.format(fs=entry, cn=funcs[cat][entry],
hats='^'*len(entry))
output += POSTAMBLE