tags.py
author Sylvain Thénault <sylvain.thenault@logilab.fr>
Tue, 26 Jan 2010 16:46:12 +0100
changeset 4372 0c44af150a49
parent 4252 6c4f109c2b03
child 5421 8167de96c523
permissions -rw-r--r--
introduce a default render implementation on the base widget, which add_media and then call the newly introduced method _render(form, field, formrenderer), abstract of the base widget class. This allow a litle code factorisation and avoid to forget the call to add_media in subclasses.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
980
59552ba2015f more tags, map klass -> class avoiding needs to use **dict
sylvain.thenault@logilab.fr
parents: 909
diff changeset
     1
"""helper classes to generate simple (X)HTML tags
59552ba2015f more tags, map klass -> class avoiding needs to use **dict
sylvain.thenault@logilab.fr
parents: 909
diff changeset
     2
59552ba2015f more tags, map klass -> class avoiding needs to use **dict
sylvain.thenault@logilab.fr
parents: 909
diff changeset
     3
:organization: Logilab
4212
ab6573088b4a update copyright: welcome 2010
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3847
diff changeset
     4
:copyright: 2010 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2.
980
59552ba2015f more tags, map klass -> class avoiding needs to use **dict
sylvain.thenault@logilab.fr
parents: 909
diff changeset
     5
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr
1977
606923dff11b big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1647
diff changeset
     6
:license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses
980
59552ba2015f more tags, map klass -> class avoiding needs to use **dict
sylvain.thenault@logilab.fr
parents: 909
diff changeset
     7
"""
59552ba2015f more tags, map klass -> class avoiding needs to use **dict
sylvain.thenault@logilab.fr
parents: 909
diff changeset
     8
__docformat__ = "restructuredtext en"
59552ba2015f more tags, map klass -> class avoiding needs to use **dict
sylvain.thenault@logilab.fr
parents: 909
diff changeset
     9
4023
eae23c40627a drop common subpackage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3847
diff changeset
    10
from cubicweb.uilib import simple_sgml_tag, sgml_attributes
980
59552ba2015f more tags, map klass -> class avoiding needs to use **dict
sylvain.thenault@logilab.fr
parents: 909
diff changeset
    11
845
6103d0f55a0b simple tags
sylvain.thenault@logilab.fr
parents:
diff changeset
    12
class tag(object):
1623
sylvain.thenault@logilab.fr
parents: 1065
diff changeset
    13
    def __init__(self, name, escapecontent=True):
845
6103d0f55a0b simple tags
sylvain.thenault@logilab.fr
parents:
diff changeset
    14
        self.name = name
1623
sylvain.thenault@logilab.fr
parents: 1065
diff changeset
    15
        self.escapecontent = escapecontent
sylvain.thenault@logilab.fr
parents: 1065
diff changeset
    16
845
6103d0f55a0b simple tags
sylvain.thenault@logilab.fr
parents:
diff changeset
    17
    def __call__(self, __content=None, **attrs):
1623
sylvain.thenault@logilab.fr
parents: 1065
diff changeset
    18
        attrs.setdefault('escapecontent', self.escapecontent)
845
6103d0f55a0b simple tags
sylvain.thenault@logilab.fr
parents:
diff changeset
    19
        return simple_sgml_tag(self.name, __content, **attrs)
6103d0f55a0b simple tags
sylvain.thenault@logilab.fr
parents:
diff changeset
    20
3803
414bb8439002 [web ui] decorate form buttons with icons (at last)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2517
diff changeset
    21
button = tag('button')
845
6103d0f55a0b simple tags
sylvain.thenault@logilab.fr
parents:
diff changeset
    22
input = tag('input')
6103d0f55a0b simple tags
sylvain.thenault@logilab.fr
parents:
diff changeset
    23
textarea = tag('textarea')
6103d0f55a0b simple tags
sylvain.thenault@logilab.fr
parents:
diff changeset
    24
a = tag('a')
6103d0f55a0b simple tags
sylvain.thenault@logilab.fr
parents:
diff changeset
    25
span = tag('span')
1623
sylvain.thenault@logilab.fr
parents: 1065
diff changeset
    26
div = tag('div', False)
845
6103d0f55a0b simple tags
sylvain.thenault@logilab.fr
parents:
diff changeset
    27
img = tag('img')
846
e449f1da7d09 new label tag
sylvain.thenault@logilab.fr
parents: 845
diff changeset
    28
label = tag('label')
895
e78ae38506db select tag now accepts a multiple argument
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 861
diff changeset
    29
option = tag('option')
980
59552ba2015f more tags, map klass -> class avoiding needs to use **dict
sylvain.thenault@logilab.fr
parents: 909
diff changeset
    30
h1 = tag('h1')
59552ba2015f more tags, map klass -> class avoiding needs to use **dict
sylvain.thenault@logilab.fr
parents: 909
diff changeset
    31
h2 = tag('h2')
59552ba2015f more tags, map klass -> class avoiding needs to use **dict
sylvain.thenault@logilab.fr
parents: 909
diff changeset
    32
h3 = tag('h3')
59552ba2015f more tags, map klass -> class avoiding needs to use **dict
sylvain.thenault@logilab.fr
parents: 909
diff changeset
    33
h4 = tag('h4')
59552ba2015f more tags, map klass -> class avoiding needs to use **dict
sylvain.thenault@logilab.fr
parents: 909
diff changeset
    34
h5 = tag('h5')
3847
7cf2b338a8e7 [common] add tr, th and td tags
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 3803
diff changeset
    35
tr = tag('tr')
7cf2b338a8e7 [common] add tr, th and td tags
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 3803
diff changeset
    36
th = tag('th')
7cf2b338a8e7 [common] add tr, th and td tags
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 3803
diff changeset
    37
td = tag('td')
861
7985b5db2284 provide a mini HTML select tag constructor
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 846
diff changeset
    38
1054
434e098f6c76 allow arbitrary arguments
sylvain.thenault@logilab.fr
parents: 980
diff changeset
    39
def select(name, id=None, multiple=False, options=[], **attrs):
895
e78ae38506db select tag now accepts a multiple argument
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 861
diff changeset
    40
    if multiple:
909
4685a8c21d73 select needs to accept an id
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 895
diff changeset
    41
        attrs['multiple'] = 'multiple'
4685a8c21d73 select needs to accept an id
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 895
diff changeset
    42
    if id:
4685a8c21d73 select needs to accept an id
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 895
diff changeset
    43
        attrs['id'] = id
1647
9936aca2453d sort attribute for testing purpose
sylvain.thenault@logilab.fr
parents: 1623
diff changeset
    44
    attrs['name'] = name
2517
d664652b7f9c select attributes should be escaped
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
    45
    html = [u'<select %s>' % sgml_attributes(attrs)]
861
7985b5db2284 provide a mini HTML select tag constructor
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 846
diff changeset
    46
    html += options
7985b5db2284 provide a mini HTML select tag constructor
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 846
diff changeset
    47
    html.append(u'</select>')
7985b5db2284 provide a mini HTML select tag constructor
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 846
diff changeset
    48
    return u'\n'.join(html)
7985b5db2284 provide a mini HTML select tag constructor
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 846
diff changeset
    49