%doc>pydoc.myt - provides formatting functions for printing docstring.AbstractDoc generated python documentation objects.%doc> <%! import docstring from docutils.core import publish_parts import re, sys def whitespace(content): """trim left whitespace.""" if not content: return '' # Convert tabs to spaces (following the normal Python rules) # and split into a list of lines: lines = content.expandtabs().splitlines() # Determine minimum indentation (first line doesn't count): indent = sys.maxint for line in lines[1:]: stripped = line.lstrip() if stripped: indent = min(indent, len(line) - len(stripped)) # Remove indentation (first line is special): trimmed = [lines[0].strip()] if indent < sys.maxint: for line in lines[1:]: trimmed.append(line[indent:].rstrip()) # Strip off trailing and leading blank lines: while trimmed and not trimmed[-1]: trimmed.pop() while trimmed and not trimmed[0]: trimmed.pop(0) # Return a single string: return '\n'.join(trimmed) def formatdocstring(content): return publish_parts(whitespace(content), writer_name='html')['body'] %> <%def name="inline_links(toc, extension, paged)"><% def link(match): (module, desc) = match.group(1,2) if not desc: path = "docstrings_" + module elif desc.endswith('()'): path = "docstrings_" + module + "_modfunc_" + desc[:-2] else: path = "docstrings_" + module + "_" + desc return capture(nav.toclink, toc=toc, path=path, description=desc or None, extension=extension, paged=paged) return lambda content: re.sub('\[(.+?)#(.*?)\]', link, content) %>%def> <%namespace name="formatting" file="formatting.html"/> <%namespace name="nav" file="nav.html"/> <%def name="obj_doc(obj, toc, extension, paged)"> <% if obj.isclass: links = [] for elem in obj.inherits: if isinstance(elem, docstring.ObjectDoc): links.append(capture(nav.toclink, toc=toc, path=elem.toc_path, extension=extension, description=elem.name, paged=paged)) else: links.append(str(elem)) htmldescription = "class " + obj.classname + "(%s)" % (','.join(links)) else: htmldescription = obj.description %> <%call expr="formatting.section(toc=toc, path=obj.toc_path, description=htmldescription, paged=paged, extension=extension)"> % if obj.doc: