Mercurial > genshi > mirror
changeset 3:518a8520a6e1 trunk
Added basic example.
author | cmlenz |
---|---|
date | Sat, 03 Jun 2006 12:28:53 +0000 |
parents | edbbe45da6e2 |
children | 49364e784c47 |
files | examples/basic/common/default_header.html examples/basic/common/default_header.kid examples/basic/common/macros.html examples/basic/common/macros.kid examples/basic/kidrun.py examples/basic/module/test.html examples/basic/module/test.kid examples/basic/run.py |
diffstat | 8 files changed, 172 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
new file mode 100644 --- /dev/null +++ b/examples/basic/common/default_header.html @@ -0,0 +1,4 @@ +<head> + <title>Hello ${hello}</title> + <style type="text/css">@import(style.css)</style> +</head>
new file mode 100644 --- /dev/null +++ b/examples/basic/common/default_header.kid @@ -0,0 +1,3 @@ +<head> + <title>Hello ${hello}</title> +</head>
new file mode 100644 --- /dev/null +++ b/examples/basic/common/macros.html @@ -0,0 +1,12 @@ +<div xmlns:py="http://purl.org/kid/ns#" + py:strip=""> + <div py:def="macro1">reference me, please</div> + <div py:def="macro2(name, classname='expanded')" class="${classname}"> + Hello ${name.title()} + </div> + <span py:match="greeting" class="greeting"> + Hello ${select('@name')} + </span> + <span py:match="span[@class='greeting']" style="text-decoration: underline" + py:content="select('text()')"/> +</div>
new file mode 100644 --- /dev/null +++ b/examples/basic/common/macros.kid @@ -0,0 +1,13 @@ +<div xmlns:py="http://purl.org/kid/ns#" + py:extends="'default_header.kid'" py:strip=""> + <div py:def="macro1">reference me, please</div> + <div py:def="macro2(name, classname='expanded')" class="${classname}"> + Hello ${name.title()} + </div> + <span py:match="item.tag == '{http://www.w3.org/1999/xhtml}greeting'" class="greeting"> + Hello ${item.get('name')} + </span> + <span py:match="item.tag == '{http://www.w3.org/1999/xhtml}span' and item.get('class') == 'greeting'" style="text-decoration: underline"> + ${item.findtext('')} + </span> +</div>
new file mode 100755 --- /dev/null +++ b/examples/basic/kidrun.py @@ -0,0 +1,47 @@ +from datetime import datetime, timedelta +import os +import sys + +import kid + +def test(): + base_path = os.path.dirname(os.path.abspath(__file__)) + kid.path = kid.TemplatePath([os.path.join(base_path, 'common'), + os.path.join(base_path, 'module')]) + + ctxt = dict(hello='<world>', hey='ZYX', bozz=None, + items=['Number %d' % num for num in range(1, 15)], + prefix='#') + + start = datetime.now() + template = kid.Template(file='test.kid', **ctxt) + print ' --> parse stage: ', datetime.now() - start + + times = [] + for i in range(100): + start = datetime.now() + for output in template.generate(): + if i == 0: + sys.stdout.write(output) + if i == 0: + print + else: + sys.stdout.write('.') + sys.stdout.flush() + times.append(datetime.now() - start) + print + + total_ms = sum([t.seconds * 1000 + t.microseconds for t in times]) + print ' --> render stage: %s (avg), %s (min), %s (max):' % ( + timedelta(microseconds=total_ms / len(times)), + timedelta(microseconds=min([t.seconds * 1000 + t.microseconds for t in times])), + timedelta(microseconds=max([t.seconds * 1000 + t.microseconds for t in times]))) + +if __name__ == '__main__': + if '-p' in sys.argv: + import profile, pstats + profile.run('test()', '.tmpl_prof') + stats = pstats.Stats('.tmpl_prof') + stats.strip_dirs().sort_stats('time').print_stats(10) + else: + test()
new file mode 100644 --- /dev/null +++ b/examples/basic/module/test.html @@ -0,0 +1,23 @@ +<!DOCTYPE html + PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" + "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> +<html xmlns="http://www.w3.org/1999/xhtml" + xmlns:py="http://purl.org/kid/ns#" + xmlns:xi="http://www.w3.org/2001/XInclude"> + <xi:include href="${skin}_header.html" /> + <xi:include href="macros.html" /> + <xi:include href="custom_stuff.html"><xi:fallback/></xi:include> + <body class="$bozz"> + <ul py:attrs="{'id': 'second', 'class': None}" py:if="len(items) > 0"> + <li py:for="item in items">Item $prefix${item.split()[-1]}</li> + XYZ ${hey} + </ul> + ${macro1()} ${macro1()} ${macro1()} + ${macro2('john')} + ${macro2('kate', classname='collapsed')} + <div py:content="macro2('helmut')" py:strip="">Replace me</div> + <greeting name="Dude" /> + <greeting name="King" /> + <span class="greeting">Hello Silicon</span> + </body> +</html>
new file mode 100644 --- /dev/null +++ b/examples/basic/module/test.kid @@ -0,0 +1,19 @@ +<!DOCTYPE html + PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" + "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> +<html xmlns="http://www.w3.org/1999/xhtml" xmlns:py="http://purl.org/kid/ns#" + py:extends="'macros.kid'" lang="en"> + <body class="${bozz}"> + <ul py:attrs="{'id': 'second', 'class': None}" py:if="len(items) > 0"> + <li py:for="item in items">Item $prefix${item.split()[-1]}</li> + XYZ ${hey} + </ul> + ${macro1()} ${macro1()} ${macro1()} + ${macro2('john')} + ${macro2('kate', classname='collapsed')} + <div py:content="macro2('helmut')" py:strip="">Replace me</div> + <greeting name="Dude" /> + <greeting name="King" /> + <span class="greeting">Hello Silicon</span> + </body> +</html>
new file mode 100755 --- /dev/null +++ b/examples/basic/run.py @@ -0,0 +1,51 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- + +from datetime import datetime, timedelta +import os +import sys + +from markup.template import Context, TemplateLoader + +def test(): + base_path = os.path.dirname(os.path.abspath(__file__)) + loader = TemplateLoader([os.path.join(base_path, 'common'), + os.path.join(base_path, 'module')], + auto_reload=True) + + start = datetime.now() + tmpl = loader.load('test.html') + print ' --> parse stage: ', datetime.now() - start + + ctxt = Context(hello='<world>', skin='default', hey='ZYX', bozz=None, + items=['Number %d' % num for num in range(1, 15)], + prefix='#') + + print tmpl.generate(ctxt).render(method='html') + + times = [] + for i in range(100): + start = datetime.now() + list(tmpl.generate(ctxt)) + sys.stdout.write('.') + sys.stdout.flush() + times.append(datetime.now() - start) + print + + total_ms = sum([t.seconds * 1000 + t.microseconds for t in times]) + print ' --> render stage: %s (avg), %s (min), %s (max)' % ( + timedelta(microseconds=total_ms / len(times)), + timedelta(microseconds=min([t.seconds * 1000 + t.microseconds for t in times])), + timedelta(microseconds=max([t.seconds * 1000 + t.microseconds for t in times]))) + +if __name__ == '__main__': + if '-p' in sys.argv: + import hotshot, hotshot.stats + prof = hotshot.Profile("template.prof") + benchtime = prof.runcall(test) + stats = hotshot.stats.load("template.prof") + stats.strip_dirs() + stats.sort_stats('time', 'calls') + stats.print_stats() + else: + test()