Report abuse

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
from genshi.core import Attrs, QName
from genshi.core import START, START_NS, END, END_NS, DOCTYPE, TEXT
from genshi.path import Path
from genshi.template.eval import Expression
from genshi.template.inline import _expand, _expand_text

# predeclare qnames, attributes, and expressions
A1 = Attrs([(QName(u'lang'), u'en')])
A2 = Attrs()
E1 = Expression(u'name')
E2 = Expression(u'sayhi()')
E3 = Expression(u'items')
E4 = Expression(u'enumerate(items)')
E5 = Expression(u"idx % 2 and 'odd' or 'even'")
E6 = Expression(u'item + 1')
Q1 = QName(u'http://www.w3.org/1999/xhtml}html')
Q2 = QName(u'http://www.w3.org/1999/xhtml}body')
Q3 = QName(u'http://www.w3.org/1999/xhtml}ul')
Q4 = QName(u'http://www.w3.org/1999/xhtml}li')
Q5 = QName(u'http://www.w3.org/1999/xhtml}span')

def generate(ctxt):

    def sayhi(name='world'):
        ctxt.push({'name': name})
        yield TEXT, u'\n      Hello, ', (None, 8, -1)
        for e in _expand(E1.evaluate(ctxt), (None, 9, 12)): yield e
        yield TEXT, u'!\n    ', (None, 9, 16)
        ctxt.pop()

    yield DOCTYPE, (u'html', u'-//W3C//DTD XHTML 1.0 Strict//EN', u'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd'), (None, 3, 55)
    yield START_NS, ('', u'http://www.w3.org/1999/xhtml'), (None, 4, 0)
    yield START, (Q1, A1), (None, 4, 0)
    yield TEXT, u'\n ', (None, 6, -1)
    yield START, (Q2, A2), (None, 7, 1)
    yield TEXT, u'\n    ', (None, 7, -1)

    # Applying <DefDirective "sayhi">
    ctxt.frames[-1]['sayhi'] = sayhi

    yield TEXT, u'\n    ', (None, 10, -1)
    for e in _expand(E2.evaluate(ctxt), (None, 11, 3)): yield e
    yield TEXT, u'\n    ', (None, 10, -1)

    # Applying <IfDirective "items">
    if E3.evaluate(ctxt):
        yield START, (Q3, A2), (None, 12, 4)
        yield TEXT, u'\n      ', (None, 12, -1)

        # Applying <ForDirective>
        for v in E4.evaluate(ctxt):
            ctxt.push({"idx": v[0], "item": v[1]})
            a = Attrs(A2)
            v = [v for v in list(_expand_text(E5.evaluate(ctxt))) if v is not None]
            if v:
                a.append((QName(u'class'), "".join(v)))
            yield START, (Q4, a), (None, 13, 6)
            yield TEXT, u'\n        ', (None, 14, -1)

            # Applying <ContentDirective "item + 1">
            for e in _expand(E6.evaluate(ctxt), (None, -1, -1)):
                yield START, (Q5, A2), (None, 15, 8)
                yield e
                yield END, Q5, (None, 15, 39)

            yield TEXT, u'\n      ', (None, 15, -1)
            yield END, Q4, (None, 16, 6)
            ctxt.pop()

        yield TEXT, u'\n    ', (None, 16, -1)
        yield END, Q3, (None, 17, 4)

    yield TEXT, u'\n ', (None, 17, -1)
    yield END, Q2, (None, 18, 1)
    yield TEXT, u'\n', (None, 19, -1)
    yield END, Q1, (None, 19, 0)
    yield END_NS, '', (None, 19, 0)