1 | """Tag Helpers""" |
---|
2 | # Last synced with Rails copy at Revision 2543 on Aug 20th, 2006. |
---|
3 | |
---|
4 | from webhelpers.util import html_escape |
---|
5 | import re |
---|
6 | |
---|
7 | def camelize(name): |
---|
8 | """ |
---|
9 | Camelize a ``name`` |
---|
10 | """ |
---|
11 | def upcase(matchobj): |
---|
12 | return getattr(matchobj.group(0)[1:], 'upper')() |
---|
13 | name = re.sub(r'(_[a-zA-Z])', upcase, name) |
---|
14 | name = name[0].upper() + name[1:] |
---|
15 | return name |
---|
16 | |
---|
17 | def strip_unders(options): |
---|
18 | for x,y in options.iteritems(): |
---|
19 | if x.endswith('_'): |
---|
20 | options[x[:-1]] = y |
---|
21 | del options[x] |
---|
22 | |
---|
23 | def tag(name, open=False, **options): |
---|
24 | """ |
---|
25 | Create a HTML tag of type ``name`` |
---|
26 | |
---|
27 | ``open`` |
---|
28 | Set to True if the tag should remain open |
---|
29 | |
---|
30 | All additional keyword args become attribute/value's for the tag. To pass in Python |
---|
31 | reserved words, append _ to the name of the key. |
---|
32 | |
---|
33 | Examples:: |
---|
34 | |
---|
35 | >>> tag("br") |
---|
36 | <br /> |
---|
37 | >>> tag("input", type="text") |
---|
38 | <input type="text" /> |
---|
39 | """ |
---|
40 | tag = '<%s%s%s' % (name, (options and tag_options(**options)) or '', (open and '>') or ' />') |
---|
41 | return tag |
---|
42 | |
---|
43 | def content_tag(name, content, **options): |
---|
44 | """ |
---|
45 | Create a tag with content |
---|
46 | |
---|
47 | Takes the same keyword args as ``tag`` |
---|
48 | |
---|
49 | Examples:: |
---|
50 | |
---|
51 | >>> content_tag("p", "Hello world!") |
---|
52 | <p>Hello world!</p> |
---|
53 | >>> content_tag("div", content_tag("p", "Hello world!"), class_="strong") |
---|
54 | <div class="strong"><p>Hello world!</p></div> |
---|
55 | """ |
---|
56 | if content is None: |
---|
57 | content = '' |
---|
58 | tag = '<%s%s>%s</%s>' % (name, (options and tag_options(**options)) or '', content, name) |
---|
59 | return tag |
---|
60 | |
---|
61 | def cdata_section(content): |
---|
62 | """ |
---|
63 | Returns a CDATA section for the given ``content``. |
---|
64 | |
---|
65 | CDATA sections are used to escape blocks of text containing characters which would |
---|
66 | otherwise be recognized as markup. CDATA sections begin with the string |
---|
67 | ``<![CDATA[`` and end with (and may not contain) the string |
---|
68 | ``]]>``. |
---|
69 | """ |
---|
70 | if content is None: |
---|
71 | content = '' |
---|
72 | return "<![CDATA[%s]]>" % content |
---|
73 | |
---|
74 | def tag_options(**options): |
---|
75 | strip_unders(options) |
---|
76 | cleaned_options = convert_booleans(dict([(x, y) for x,y in options.iteritems() if y is not None])) |
---|
77 | optionlist = ['%s="%s"' % (x, html_escape(str(y))) for x,y in cleaned_options.iteritems()] |
---|
78 | optionlist.sort() |
---|
79 | if optionlist: |
---|
80 | return ' ' + ' '.join(optionlist) |
---|
81 | else: |
---|
82 | return '' |
---|
83 | |
---|
84 | def convert_booleans(options): |
---|
85 | for attr in ['disabled', 'readonly', 'multiple']: |
---|
86 | boolean_attribute(options, attr) |
---|
87 | return options |
---|
88 | |
---|
89 | def boolean_attribute(options, attribute): |
---|
90 | if options.get(attribute): |
---|
91 | options[attribute] = attribute |
---|
92 | elif options.has_key(attribute): |
---|
93 | del options[attribute] |
---|
94 | |
---|
95 | __all__ = ['tag', 'content_tag', 'cdata_section', 'camelize'] |
---|