<%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:
    <div class="darkcell">${obj.doc or '' | formatdocstring, inline_links(toc, extension, paged)}</div>
    % endif
    
    % if not obj.isclass and obj.functions:

        <%call expr="formatting.section(toc=toc, path=obj.mod_path, paged=paged, extension=extension)">
            % for func in obj.functions:
                ${function_doc(func=func,toc=toc, extension=extension, paged=paged)}
            % endfor
        </%call>

    % else:

        % if obj.functions:
            % for func in obj.functions:
                % if isinstance(func, docstring.FunctionDoc):
                    ${function_doc(func=func, toc=toc, extension=extension, paged=paged)}
                % elif isinstance(func, docstring.PropertyDoc):
                    ${property_doc(prop=func, toc=toc, extension=extension, paged=paged)}
                % endif
            % endfor
        % endif
    % endif

    % if obj.classes:
        % for class_ in obj.classes:
            ${obj_doc(obj=class_, toc=toc, extension=extension, paged=paged)}
        % endfor
    % endif    
    </%call>
</%def>

<%def name="function_doc(func, toc, extension, paged)">
    <div class="darkcell">
    <%
        if hasattr(func, 'toc_path'):
            item = toc.get_by_path(func.toc_path)
        else:
            item = None
    %>
    <A name="${item and item.path or ''}"></a>
    <b>def ${func.name}(${", ".join(map(lambda k: "<i>%s</i>" % k, func.arglist))})</b>
    <div class="docstring">
    ${func.doc or '' | formatdocstring, inline_links(toc, extension, paged)}
    </div>
    </div>
</%def>

<%def name="property_doc(prop, toc, extension, paged)">
         <div class="darkcell">
         <A name=""></a>
         <b>${prop.name} = property()</b>
         <div class="docstring">
         ${prop.doc or '' | formatdocstring, inline_links(toc, extension, paged)}
         </div> 
         </div>
</%def>


