1<%doc>formatting.myt - Provides section formatting elements, syntax-highlighted code blocks, and other special filters.</%doc>
2
3<%global>
4    import string, re
5    import highlight
6</%global>
7
8<%method section>
9<%doc>Main section formatting element.</%doc>
10<%args>
11    toc
12    path
13    description=None
14    onepage=False
15</%args>
16<%init>
17    item = toc.get_by_path(path)
18    if item is None:
19        raise "path: " + path
20</%init>
21
22<A name="<% item.path %>"></a>
23
24<div class="subsection" style="margin-left:<% repr(item.depth * 10) %>px;">
25
26<%python>
27    content = m.content()
28    re2 = re.compile(r"'''PYESC(.+?)PYESC'''", re.S)
29    content = re2.sub(lambda m: m.group(1), content)
30</%python>
31
32% if item.depth > 1:
33<h3><% description or item.description %></h3>
34%
35
36    <div class="sectiontext">
37    <% content %>
38    </div>
39
40% if onepage or item.depth > 1:
41%   if (item.next and item.next.depth >= item.depth):
42    <a href="#<% item.get_page_root().path %>" class="toclink">back to section top</a>
43%
44% else:
45    <a href="#<% item.get_page_root().path %>" class="toclink">back to section top</a>
46    <& nav.myt:pagenav, item=item, onepage=onepage &>
47%
48</div>
49
50</%method>
51
52
53<%method formatplain>
54    <%filter>
55        import re
56        f = re.sub(r'\n[\s\t]*\n[\s\t]*', '</p>\n<p>', f)
57        f = "<p>" + f + "</p>"
58        return f
59    </%filter>
60<% m.content() | h%>
61</%method>
62
63
64
65
66<%method codeline trim="both">
67<span class="codeline"><% m.content() %></span>
68</%method>
69
70<%method code autoflush=False>
71<%args>
72    title = None
73    syntaxtype = 'python'
74    html_escape = False
75    use_sliders = False
76</%args>
77
78<%init>
79    def fix_indent(f):
80        f =string.expandtabs(f, 4)
81        g = ''
82        lines = string.split(f, "\n")
83        whitespace = None
84        for line in lines:
85            if whitespace is None:
86                match = re.match(r"^([ ]*).+", line)
87                if match is not None:
88                    whitespace = match.group(1)
89
90            if whitespace is not None:
91                line = re.sub(r"^%s" % whitespace, "", line)
92
93            if whitespace is not None or re.search(r"\w", line) is not None:
94                g += (line + "\n")
95
96
97        return g.rstrip()
98
99    p = re.compile(r'<pre>(.*?)</pre>', re.S)
100    def hlight(match):
101        return "<pre>" + highlight.highlight(fix_indent(match.group(1)), html_escape = html_escape, syntaxtype = syntaxtype) + "</pre>"
102    content = p.sub(hlight, "<pre>" + m.content() + "</pre>")
103</%init>
104<div class="<% use_sliders and "sliding_code" or "code" %>">
105% if title is not None:
106    <div class="codetitle"><% title %></div>
107%
108<% content %></div>
109</%method>
110
111
112
113
114<%method popboxlink trim="both">
115    <%args>
116        name=None
117        show='show'
118        hide='hide'
119    </%args>
120    <%init>
121        if name is None:
122            name = m.attributes.setdefault('popbox_name', 0)
123        name += 1
124        m.attributes['popbox_name'] = name
125        name = "popbox_" + repr(name)
126    </%init>
127javascript:togglePopbox('<% name %>', '<% show %>', '<% hide %>')
128</%method>
129
130<%method popbox trim="both">
131<%args>
132    name = None
133    class_ = None
134</%args>
135<%init>
136    if name is None:
137        name = 'popbox_' + repr(m.attributes['popbox_name'])
138</%init>
139<div id="<% name %>_div" class="<% class_ %>" style="display:none;"><% m.content().strip() %></div>
140</%method>
141
142<%method poplink trim="both">
143    <%args>
144        link='sql'
145    </%args>
146    <%init>
147        href = m.scomp('SELF:popboxlink')
148    </%init>
149    '''PYESC<& nav.myt:link, href=href, text=link, class_="codepoplink" &>PYESC'''
150</%method>
151
152<%method codepopper trim="both">
153	<%init>
154		c = m.content()
155		c = re.sub(r'\n', '<br/>\n', c.strip())
156	</%init>
157    </pre><&|SELF:popbox, class_="codepop" &><% c %></&><pre>
158</%method>
159
160<%method poppedcode trim="both">
161	<%init>
162		c = m.content()
163		c = re.sub(r'\n', '<br/>\n', c.strip())
164	</%init>
165    </pre><div class="codepop"><% c %></div><pre>
166</%method>
167