blob: f187fa04c27f798f039fca817fa5a59a0f431b27 [file]
# formatting.myt - Provides section formatting elements, syntax-highlighted code blocks, and other special filters.
<%!
import string, re
from mako import filters
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.
<%
item = toc.get_by_path(path)
subsection = item.depth > 1
%>
<A name="${item.path}"></a>
<div class="${subsection and 'subsection' or 'section'}">
<%
content = capture(caller.body)
re2 = re.compile(r"'''PYESC(.+?)PYESC'''", re.S)
content = re2.sub(lambda m: m.group(1), content)
%>
<h3>${description or item.description}</h3>
${content}
% if (subsection and item.next and item.next.depth >= item.depth) or not subsection:
% 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)">
<%!
import pygments
from pygments.formatters import HtmlFormatter
from pygments.lexers import PythonLexer, HtmlLexer
from mako.ext.pygmentplugin import MakoLexer
lexers = {'mako':MakoLexer(), 'python':PythonLexer(), 'html':HtmlLexer()}
%>
<%
lexer = lexers.get(syntaxtype, None)
if lexer is not None:
content = pygments.highlight(capture(caller.body) , lexer, HtmlFormatter())
else:
content = "<pre>" + capture(caller.body) + "</pre>"
%>
<div class="${ use_sliders and "sliding_code" or "code" }">
% if title is not None:
${title}
% endif
${ content }
</div>
</%def>