## formatting.myt - Provides section formatting elements, syntax-highlighted code blocks, and other special filters.
<%!
    import string, re, cgi
    from mako import filters
    import highlight
    
    def plainfilter(f):
        f = re.sub(r'\n[\s\t]*\n[\s\t]*', '</p>\n<p>', f)
        f = "<p>" + f + "</p>"
        return f
    
%>

<%namespace name="nav" file="nav.html"/>

<%def name="section(toc, path, paged, extension, description=None)">
    ## Main section formatting element.
    <%
        content = capture(caller.body)
        re2 = re.compile(r"'''PYESC(.+?)PYESC'''", re.S)
        content = re2.sub(lambda m: m.group(1), content)

        item = toc.get_by_path(path)
        subsection = item.depth > 1
        level = min(item.depth, 4)
    %>
    <A name="${item.path}"></a>
    
    <div class="${'sectionL%d' % level}">

    % if (subsection):
    <h3>${description or item.description}</h3>
    % endif
    
    ${content}

    % if len(item.children) == 0:
        % if paged:
            <a href="#top">back to section top</a>
        % else:
            <a href="#${item.get_page_root().path}">back to section top</a>
        % endif
    % endif
    </div>

</%def>


<%def name="formatplain()" filter="plainfilter">
    ${ caller.body() | h}
</%def>


<%def name="codeline()" filter="trim,h">
    <span class="codeline">${ caller.body() }</span>
</%def>

<%def name="code(title=None, syntaxtype='mako', html_escape=False, use_sliders=False)">
    <%
        def fix_indent(f):
            f =string.expandtabs(f, 4)
            g = ''
            lines = string.split(f, "\n")
            whitespace = None
            for line in lines:
                if whitespace is None:
                    match = re.match(r"^([ ]*).+", line)
                    if match is not None:
                        whitespace = match.group(1)

                if whitespace is not None:
                    line = re.sub(r"^%s" % whitespace, "", line)

                if whitespace is not None or re.search(r"\w", line) is not None:
                    g += (line + "\n")


            return g.rstrip()

        p = re.compile(r'<pre>(.*?)</pre>', re.S)
        def hlight(match):
            return "<pre>" + highlight.highlight(fix_indent(match.group(1)), html_escape = html_escape, syntaxtype = syntaxtype) + "</pre>"
        content = p.sub(hlight, "<pre>" + capture(caller.body) + "</pre>")
    %>

    <div class="${ use_sliders and "sliding_code" or "code" }">
        % if title is not None:
            <div class="codetitle">${title}</div>
        % endif
        ${ content }
    </div>
</%def>


<%def name="popboxlink(name=None, show='show', hide='hide')" filter="trim"> 
    <%
        if name is None:
            name = attributes.setdefault('popbox_name', 0)
        name += 1
        attributes['popbox_name'] = name
        name = "popbox_" + repr(name)
    %>
javascript:togglePopbox('${name}', '${show}', '${hide}')
</%def>

<%def name="popbox(name=None, class_=None)" filter="trim">
<%
    if name is None:
        name = 'popbox_' + repr(attributes['popbox_name'])
%>
<div id="${name}_div" class="${class_}" style="display:none;">${capture(caller.body) | trim}</div>
</%def>

<%def name="poplink(link='sql')" filter="trim">
    <%
        href = capture(popboxlink)
    %>
    '''PYESC${nav.link(href=href, text=link, class_="codepoplink")}PYESC'''
</%def>

<%def name="codepopper()" filter="trim">
    <%
		c = capture(caller.body)
		c = re.sub(r'\n', '<br/>\n', c.strip())
	%>
    </pre><%call expr="popbox(class_='codepop')">${c}</%call><pre>
</%def>

<%def name="poppedcode()" filter="trim">
    <%
		c = capture(caller.body)
		c = re.sub(r'\n', '<br/>\n', c.strip())
	%>
    </pre><div class="codepop">${c}</div><pre>
</%def>




