def __init__(self, builder): super(Count, self).__init__(builder) self.tags = {} def handle_starttag(self, name, attr, closed, data): self.tags[name] = self.tags.get(name, 0) + 1 self.builder.handle_starttag(name, attr, closed, data) def finalize(self): keys = self.tags.keys() keys.sort() for key in keys: print "%s: %d" % (key, self.tags[key]) return self.builder.finalize() html = _html.replace(overlay_eventfilters=[Count]) print "Before Loading" template = html.from_string(""" <anode tdi:overlay="foo"></anode> """).overlay(html.from_string(""" <bnode tdi:overlay="foo"> <cnode /> </bnode> """)) print ">>> Between Loading and Rendering" template.render() print ">>> Between Rendering"
#!/usr/bin/env python import warnings as _warnings _warnings.resetwarnings() _warnings.filterwarnings('error') from tdi import html from tdi.tools import css html = html.replace(eventfilters=[ css.CDATAFilter, css.MinifyFilter, ]) tpl = html.from_string(""" <html> <style><!-- --></style> <style><!-- a b c{ foo: bar; baz: blub; } --></style> <style tdi="bar"><!-- --></style> </html> """.lstrip()) tpl.render()
# 1. Comment filter def keep_foo(comment): """ Comment filter """ if comment == "<!-- foo -->": return comment # 2. New filter factory def html_minifyfilter(builder): """ HTML minifier factory """ return html_tools.MinifyFilter(builder, comment_filter=keep_foo) # 3. Template Factory html = html.replace(eventfilters=[ # ... html_minifyfilter, # instead of html_tools.MinifyFilter # ... ]) # 4. Do your thing. tpl = html.from_string(""" <html> <head> <!-- Here comes the title --> <title>Hello World!</title> <style> Some style. </style> </head> <body> <!-- foo -->
#!/usr/bin/env python import warnings as _warnings _warnings.resetwarnings() _warnings.filterwarnings('error') from tdi import html from tdi.tools import css html = html.replace(eventfilters=[css.CDATAFilter]) tpl = html.from_string(""" <html> <style><!-- --></style> <style><!-- a b c{ foo: bar; baz: blub; } --></style> <style tdi="bar"><!-- --></style> </html> """.lstrip()) tpl.render()
#!/usr/bin/env python import warnings as _warnings _warnings.resetwarnings() _warnings.filterwarnings('error') import sys import tempfile import time from tdi import html html = html.replace(autoupdate=True) tfile = tempfile.NamedTemporaryFile() try: tfile.write("""<html><body tdi="body">Yey</body></html>""") tfile.flush() # 2) Load the template from_file template = html.from_file(tfile.name) print template.tree # (... wait for low-timer-resolution systems ...) time.sleep(3) # 3) Update the file tfile.seek(0) tfile.truncate() tfile.write("""<html><body tdi="nobody">Yup!</body></html>""") tfile.flush()
#!/usr/bin/env python import warnings as _warnings _warnings.resetwarnings() _warnings.filterwarnings('error') from tdi import html from tdi.tools import javascript html = html.replace(eventfilters=[javascript.CDATAFilter]) tpl = html.from_string(""" <html> <script src="foo"></script> <script><!-- //--></script> <script><!-- var x=1; var y = 2; alert( x + y ); //--></script> <script tdi="bar"><!-- --></script> </html> """.lstrip()) tpl.render()
#!/usr/bin/env python import warnings as _warnings _warnings.resetwarnings() _warnings.filterwarnings('error') from tdi.tools import html as _html_tools from tdi import html html_compressed = html.replace(eventfilters=[_html_tools.MinifyFilter]) template = html_compressed.from_string(""" <html> <head> <title>Boo! </title> <script> Some script </script> <style><!-- Some style --></style> </head> <body> <p>Hello <b>YOU!</b> reader! Now... <br /> <form> <textarea >Some text in here. </textarea > </form>
#!/usr/bin/env python import warnings as _warnings _warnings.resetwarnings() _warnings.filterwarnings('error') from tdi import html from tdi.tools import javascript html = html.replace(eventfilters=[javascript.MinifyFilter]) tpl = html.from_string(""" <html> <script src="foo"></script> <script><!-- //--></script> <script><!-- var x=1; var y = 2; alert( x + y ); //--></script> <script tdi="bar"><!-- --></script> </html> """.lstrip()) tpl.render()
#!/usr/bin/env python import warnings as _warnings _warnings.resetwarnings() _warnings.filterwarnings('error') import os os.chdir(os.path.dirname(os.path.abspath(os.path.normpath(__file__)))) # BEGIN INCLUDE from tdi import factory_memoize from tdi import html t1 = html.from_file('loading.html') t2 = html.from_file('loading.html') # t1 and t2 are different template objects here print t1 is t2 # False # 1) Tell the factory that we want memoized calls html = html.replace(memoizer={}) # Wrap into the key provider html = factory_memoize.MemoizedFactory(html) t1 = html.from_file('loading.html') t2 = html.from_file('loading.html') # t1 and t2 are the same objects here print t1 is t2 # True
#!/usr/bin/env python import warnings as _warnings _warnings.resetwarnings() _warnings.filterwarnings('error') # BEGIN INCLUDE from tdi.tools import html as html_tools from tdi import html tpl = html.replace(eventfilters=[ html_tools.MinifyFilter ]).from_string(""" <html> <head> <title>Hello World!</title> <style>/*<![CDATA[*/ Some style. /*]]>*/</style> </head> <body> <script>//<![CDATA[ Some script. //]]></script> <h1>Hello World!</h1> </body> """.lstrip()) tpl.render()
#!/usr/bin/env python import warnings as _warnings _warnings.resetwarnings() _warnings.filterwarnings('error') from tdi import html from tdi.tools import css html = html.replace(eventfilters=[css.MinifyFilter]) tpl = html.from_string(""" <html> <style><!-- --></style> <style><!-- a b c{ foo: bar; baz: blub; } --></style> <style tdi="bar"><!-- --></style> </html> """.lstrip()) tpl.render()
#!/usr/bin/env python import warnings as _warnings _warnings.resetwarnings() _warnings.filterwarnings('error') from tdi import html from tdi.tools import javascript html = html.replace(eventfilters=[ javascript.CDATAFilter, javascript.MinifyFilter, ]) tpl = html.from_string(""" <html> <script src="foo"></script> <script><!-- //--></script> <script><!-- var x=1; var y = 2; alert( x + y ); //--></script> <script tdi="bar"><!-- --></script> </html> """.lstrip()) tpl.render()
# which is to produce the same output under all circumstances. # # So, we use the filtering feature to add a tdi attribute to # the table start tag, which is needed to address the table for # partial rendering (startnode='table' in the render call). # # Also this is a good small example for writing filters. # class TableFilter(_filters.BaseEventFilter): def handle_starttag(self, name, attr, closed, data): if name == 'table': attr.append(('tdi', 'table')) data = self.builder.encoder.starttag(name, attr, closed) self.builder.handle_starttag(name, attr, closed, data) _html = _html.replace(eventfilters=[TableFilter]) class Model(object): def __init__(self, year, month): self.first = start = _dt.date(year, month, 1) self.end = (start + _dt.timedelta(31)).replace(day=1) self.year = year self.start = start - _dt.timedelta(start.weekday()) self.today = _dt.date.today() def render_row(self, node): weeks, rest = divmod((self.end - self.start).days, 7) weeks += bool(rest) # repeat over each displayed week (each monday) node.repeat(None, (self.start + _dt.timedelta(week * 7)