html.py :  » Template-Engines » Breve » cwells-breve-5ff90a9 » breve » tags » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Template Engines » Breve 
Breve » cwells breve 5ff90a9 » breve » tags » html.py
from breve.flatten import flatten,register_flattener
from breve.tags import Proto,Tag,Namespace,cdata,xml,flatten_tag,flatten_proto,custom_tag

xmlns = "http://www.w3.org/1999/xhtml"
doctype = '''<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
              "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">\n'''

#
# Despite what one might think, empty elements aren't really
# supported unless the HTTP Content-type header is text/xml
# and in fact the W3C recommends against such empty elements.
#
class HtmlProto ( unicode ):
    __slots__ = [ ]
    def __call__ ( self, **kw ):
        return Tag ( self )( **kw )
    
    def __getitem__ ( self, children ):
        return Tag ( self )[ children ]

def flatten_htmlproto ( p ):
    return '<%s></%s>' % ( p, p )

register_flattener ( HtmlProto, flatten_htmlproto )


#
# register common HTML elements
#
tag_names = [
    'a','abbr','acronym','address','applet',
    'b','bdo','big','blockquote', 'body','button',
    'canvas', 'caption','center','cite','code','colgroup',
    'dd','dfn','div','dl','dt',
    'em',
    'fieldset','font','form','frameset',
    'h1','h2','h3','h4','h5','h6','head','html',
    'i','iframe','ins',
    'kbd',
    'label','legend','li',
    'menu',
    'nobr', 'noframes','noscript',
    'ol','optgroup',
    'pre',
    'q',
    's','samp','script', 'select','small','span','strike','strong','style','sub','sup',
    'table','tbody','td','textarea','tfoot','th','thead','title','tr','tt',
    'u','ul',
    'var'
]

empty_tag_names = [
    'area', 'base', 'basefont', 'br', 'col', 'frame', 'hr',
    'img', 'input', 'isindex', 'link', 'meta', 'p', 'param'
]
    
    
class inlineJS ( unicode ):
    def __init__ ( self, children ):
        self.children = children

def flatten_inlineJS ( o ):
    return u'\n<script type="text/javascript">\n//<![CDATA[%s\n//]]></script>\n' % o.children
register_flattener ( inlineJS, flatten_inlineJS )


# convenience tags

def flatten_checkbox ( o ):
    if o.attrs.get ( 'checked', False ):
        o.attrs [ 'checked' ] = 'checked'
    else:
        try:
            del o.attrs [ 'checked' ]
        except KeyError:
            pass
    return flatten_tag ( o )
checkbox = custom_tag ( 'input', 'checkbox', flatten_checkbox, attrs = { 'type': 'checkbox' } )

def flatten_option ( o ):
    if o.attrs.get ( 'selected', False ):
        o.attrs [ 'selected' ] = 'selected'
    else:
        try:
            del o.attrs [ 'selected' ]
        except KeyError:
            pass
    return flatten_tag ( o )
option = custom_tag ( 'option', flattener = flatten_option )

class lorem_ipsum ( Tag ):
    ''' silliness ensues '''
    children = [
        'Lorem ipsum dolor sit amet, consectetuer adipiscing elit.',
        'In egestas nisl sit amet odio.',
        'Duis iaculis metus eu nulla.',
        'Donec venenatis sapien sed urna.',
        'Donec et felis ut elit elementum pellentesque.',
        'Praesent bibendum turpis semper lacus.'
    ]

    def __init__ ( self ):
        Tag.__init__ ( self, 'span' )
        

tags = Namespace ( )
for t in tag_names:
    tags [ t ] = HtmlProto ( t )
    
for t in empty_tag_names:
    tags [ t ] = Proto ( t )
    
tags.update ( dict (
    checkbox = checkbox,
    option = option,
    inlineJS = inlineJS,
    lorem_ipsum = lorem_ipsum,
) )

# TAGS = Namespace ( )
# for t in tags:
#    TAGS [ t.upper ( ) ] = tags [ t ]

    
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.