## formatting.myt - Provides section formatting elements, syntax-highlighted code blocks, and other special filters.
<%!
import string, re, cgi
from mako import filters
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.
<%
item = toc.get_by_path(path)
subsection = item.depth > 1
%>
<%
content = capture(caller.body)
%>
${description or item.description}
${content}
% if (subsection and item.next and item.next.depth >= item.depth) or not subsection:
% if paged:
back to section top
% else:
back to section top
% endif
% endif
%def>
<%def name="formatplain()" filter="plainfilter">
${ caller.body() | h}
%def>
<%def name="codeline()" filter="trim,h">
${ caller.body() }
%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, IniLexer
from mako.ext.pygmentplugin import MakoHtmlLexer
lexers = {'mako':MakoHtmlLexer(), 'python':PythonLexer(), 'html':HtmlLexer(),
'ini':IniLexer()}
%>
<%
lexer = lexers.get(syntaxtype, None)
# dumb hack to print a %text> tag inside of a <%text> section
content = re.sub(r'%CLOSETEXT', '%text>', capture(caller.body))
if lexer is not None:
content = pygments.highlight(content, lexer, HtmlFormatter())
else:
content = "" + content + "
"
%>
% if title is not None:
${title}
% endif
${ content }
%def>