## 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]*', '</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) %> <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, 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 = "<pre>" + content + "</pre>" %> <div class="${ use_sliders and "sliding_code" or "code" }"> % if title is not None: ${title} % endif ${ content } </div> </%def>