示例#1
0
    def test_dom_traversal_from_macro(self):
        T = tags
        template = (
            assign('selectors', []),
            macro('css_sep', lambda attr: attr == 'class' and '.' or '#'),
            macro(
                'get_selectors', lambda tag, is_tag: selectors.extend([
                    "%s%s%s { }" % (tag.name, css_sep(_k.strip('_')), _v)
                    for _k, _v in tag.attrs.items()
                    if _k.strip('_') in ('id', 'class')
                ])),
            macro('extract_css',
                  lambda tag: tag.walk(get_selectors, True) and tag),
            macro('css_results',
                  lambda selectors: T.pre['\n'.join(selectors)]),
            T.html[T.head[T.title['macro madness']], T.body[extract_css(
                T.div(class_='text', id='main-content')[
                    T.img(src='/images/liphtePy-logo.png', alt='liphtePy logo'
                          ), T.br,
                    T.span(class_='bold')['''Hello from Breve!''']]),
                                                            css_results(
                                                                selectors)]])
        """macro abuse: self-traversing template"""

        T = tags
        output = flatten(template)

        self.assertEqual(
            output,
            (u'<html><head><title>macro madness</title></head>'
             u'<body><div class="text" id="main-content">'
             u'<img src="/images/liphtePy-logo.png" alt="liphtePy logo"></img>'
             u'<br /><span class="bold">Hello from Breve!</span></div>'
             u'<pre>div.text { }\ndiv#main-content { }\nspan.bold { }</pre>'
             u'</body></html>'))
示例#2
0
    def test_assign(self):
        """assign directive"""

        T = tags
        template = (assign('msg', 'okay'), T.html[T.head[T.title[my_name()]],
                                                  T.body[T.div[msg]]])
        output = flatten(template)
        self.assertEqual(
            output,
            u'<html><head><title>test_assign</title></head><body><div>okay</div></body></html>'
        )
示例#3
0
    def test_dynamic_tags(self):
        """test dynamic creation of tags"""

        template = (assign('mytag', Tag('mytag')), mytag(
            feature='foo')['hello, from mytag',
                           Tag('explicit')(
                               feature='bar')['hello from explicit tag']])
        actual = flatten(template)
        self.assertEqual(
            actual,
            u'<mytag feature="foo">hello, from mytag<explicit feature="bar">hello from explicit tag</explicit></mytag>'
        )
示例#4
0
    def test_assign_with_macro(self):
        """assign directive with macro"""

        T = tags
        template = (assign('msg',
                           'okay'), macro('display_msg',
                                          lambda _m: T.span[_m]),
                    T.html[T.head[T.title[my_name()]],
                           T.body[T.div[display_msg(msg)]]])
        output = flatten(template)
        self.assertEqual(
            output,
            (u'<html><head><title>test_assign_with_macro</title></head>'
             u'<body><div><span>okay</span></div></body></html>'))
示例#5
0
    def test_autotable_macro(self):
        """test autotable macro"""

        data = [
            ['One', 'Two', 'Three', 'Four'],
            range(0, 4),
            range(4, 8),
            range(8, 12)
        ]

        template = (
            macro('AutoTable', lambda data, header=False: (
                assign('alts', ['even', 'odd']),
                data and (
                    T.table(class_='autotable')[
                        header and (
                            T.thead[[T.th[_col] for _col in data[0]]]
                        ),
                        T.tbody[
                            [T.tr(class_='row-%s' % alts[_rx % 2])[
                                 [T.td(class_='col-%s' % alts[_cx % 2])[_col]
                                  for _cx, _col in enumerate(_row)]
                             ] for _rx, _row in enumerate(data[int(header):])]
                        ]
                    ]
                ) or ''
            )),

            T.html[
                T.head[T.title[my_name()]],
                T.body[
                    AutoTable(data, header=True)
                ]
            ]
        )
        output = flatten(template)
        self.assertEqual(
            output,
            (u'<html><head><title>test_autotable_macro</title></head>'
             u'<body><table class="autotable"><thead><th>One</th><th>Two</th><th>Three</th><th>Four</th></thead>'
             u'<tbody><tr class="row-even"><td class="col-even">0</td><td class="col-odd">1</td>'
             u'<td class="col-even">2</td><td class="col-odd">3</td></tr>'
             u'<tr class="row-odd"><td class="col-even">4</td><td class="col-odd">5</td><td class="col-even">6</td>'
             u'<td class="col-odd">7</td></tr>'
             u'<tr class="row-even"><td class="col-even">8</td><td class="col-odd">9</td>'
             u'<td class="col-even">10</td><td class="col-odd">11</td></tr></tbody>'
             u'</table></body></html>')
        )
示例#6
0
    def test_toc_macro(self):
        """test table-of-contents macro"""

        template = (
            assign('TOC', []),
            macro('TableOfContents', lambda matchtags, tag: (
                macro('toc_search', lambda tag, is_tag:
                tag.name in matchtags and (
                    TOC.append(T.a(href='#toc-%s' % tag.children[0])[tag.children[0]]),
                    tag.attrs.update({'class': 'chapter-%s' % tag.name}),
                    tag.children.insert(0, T.a(name='toc-%s' % tag.children[0])[tag.name])
                ) or True
                      ),
                tag.walk(toc_search, True)
            )),

            T.html[
                T.head[T.title[my_name()]],
                T.body[
                    T.div(id='TableOfContents')[
                        'Table of Contents',
                        lambda: T.ul[[T.li[_t] for _t in TOC]]
                    ],
                    TableOfContents(('h1', 'h2', 'h3'), T.div[
                        T.h1['Chapter 1'],
                        T.div['chapter 1 content'],
                        T.h1['Chapter 2'],
                        T.div[
                            'chapter 2 content',
                            T.h2['Chapter 2 subsection'],
                            T.div[
                                'chapter 2 subsection content'
                            ]
                        ]
                    ])
                ]
            ]
        )

        actual = flatten(template)