"""Tag Helpers"""
# Last synced with Rails copy at Revision 2543 on Aug 20th, 2006.
from webhelpers.util import html_escape
import re
def camelize(name):
"""
Camelize a ``name``
"""
def upcase(matchobj):
return getattr(matchobj.group(0)[1:], 'upper')()
name = re.sub(r'(_[a-zA-Z])', upcase, name)
name = name[0].upper() + name[1:]
return name
def strip_unders(options):
for x,y in options.iteritems():
if x.endswith('_'):
options[x[:-1]] = y
del options[x]
def tag(name, open=False, **options):
"""
Create a HTML tag of type ``name``
``open``
Set to True if the tag should remain open
All additional keyword args become attribute/value's for the tag. To pass in Python
reserved words, append _ to the name of the key.
Examples::
>>> tag("br")
>>> tag("input", type="text")
"""
tag = '<%s%s%s' % (name, (options and tag_options(**options)) or '', (open and '>') or ' />')
return tag
def content_tag(name, content, **options):
"""
Create a tag with content
Takes the same keyword args as ``tag``
Examples::
>>> content_tag("p", "Hello world!")
Hello world!
>>> content_tag("div", content_tag("p", "Hello world!"), class_="strong")Hello world!