## 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]*', '

\n

', f) f = "

" + f + "

" 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) %>
% if (subsection):

${description or item.description}

% endif ${content} % if len(item.children) == 0: % if paged: back to section top % else: back to section top % endif % endif
<%def name="formatplain()" filter="plainfilter"> ${ caller.body() | h} <%def name="codeline()" filter="trim,h"> ${ caller.body() } <%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'
(.*?)
', re.S) def hlight(match): return "
" + highlight.highlight(fix_indent(match.group(1)), html_escape = html_escape, syntaxtype = syntaxtype) + "
" content = p.sub(hlight, "
" + capture(caller.body) + "
") %>
% if title is not None:
${title}
% endif ${ content }
<%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 name="popbox(name=None, class_=None)" filter="trim"> <% if name is None: name = 'popbox_' + repr(attributes['popbox_name']) %> <%def name="poplink(link='sql')" filter="trim"> <% href = capture(popboxlink) %> '''PYESC${nav.link(href=href, text=link, class_="codepoplink")}PYESC''' <%def name="codepopper()" filter="trim"> <% c = capture(caller.body) c = re.sub(r'\n', '
\n', c.strip()) %> <%call expr="popbox(class_='codepop')">${c}


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