Package gluon :: Module html
[hide private]
[frames] | no frames]

Module html

source code

| This file is part of the web2py Web Framework | Copyrighted by Massimo Di Pierro <mdipierro@cs.depaul.edu> | License: LGPLv3 (http://www.gnu.org/licenses/lgpl.html)

Template helpers --------------------------------------------

Classes [hide private]
  XmlComponent
Abstract root for all Html components
  XML
use it to wrap a string that contains XML/HTML so that it will not be escaped by the template
  DIV
HTML helper, for easy generating and manipulating a DOM structure.
  CAT
  __tag_div__
  __TAG__
TAG factory
  HTML
There are four predefined document type definitions.
  XHTML
This is XHTML version of the HTML helper.
  HEAD
  TITLE
  META
  LINK
  SCRIPT
  STYLE
  IMG
  SPAN
  BODY
  H1
  H2
  H3
  H4
  H5
  H6
  P
Will replace ``\n`` by ``<br />`` if the `cr2br` attribute is provided.
  STRONG
  B
  BR
  HR
  A
Generates an A() link.
  BUTTON
  EM
  EMBED
  TT
  PRE
  CENTER
  CODE
Displays code in HTML with syntax highlighting.
  LABEL
  LI
  UL
UL Component.
  OL
  TD
  TH
  TR
TR Component.
  __TRHEAD__
__TRHEAD__ Component, internal only
  THEAD
  TBODY
  TFOOT
  COL
  COLGROUP
  TABLE
TABLE Component.
  I
  IFRAME
  INPUT
INPUT Component
  TEXTAREA
Examples:
  OPTION
  OBJECT
  OPTGROUP
  SELECT
Examples:
  FIELDSET
  LEGEND
  FORM
Examples:
  BEAUTIFY
Turns any list, dictionary, etc into decent looking html.
  MENU
Used to build menus
  web2pyHTMLParser
obj = web2pyHTMLParser(text) parses and html/xml text into web2py helpers.
  MARKMIN
For documentation: http://web2py.com/examples/static/markmin.html
Functions [hide private]
string
join(S, iterable)
Return a string which is the concatenation of the strings in the iterable.
source code
 
xmlescape(data, quote=True)
Returns an escaped string of the provided data
source code
 
call_as_list(f, *a, **b) source code
 
truncate_string(text, length, dots='...') source code
 
URL(a=None, c=None, f=None, r=None, args=None, vars=None, anchor='', extension=None, env=None, hmac_key=None, hash_vars=True, salt=None, user_signature=None, scheme=None, host=None, port=None, encode_embedded_slash=False, url_encode=True, language=None)
generates a url '/a/c/f' corresponding to application a, controller c and function f.
source code
 
verifyURL(request, hmac_key=None, hash_vars=True, salt=None, user_signature=None)
Verifies that a request's args & vars have not been tampered with by the user
source code
 
XML_unpickle(data) source code
 
XML_pickle(data) source code
 
TAG_unpickler(data) source code
 
TAG_pickler(data) source code
 
embed64(filename=None, file=None, data=None, extension='image/gif')
helper to encode the provided (binary) data into base64.
source code
 
test()
Example:
source code
 
markdown_serializer(text, tag=None, attr=None) source code
 
markmin_serializer(text, tag=None, attr=None) source code
Variables [hide private]
  regex_crlf = re.compile(r'[\r\n]')
  entitydefs = {'AElig': '\xc3\x86', 'Aacute': '\xc3\x81', 'Acir...
  ON = True
  TAG = __TAG__()
  __package__ = 'gluon'
Function Details [hide private]

join(S, iterable)

source code 

Return a string which is the concatenation of the strings in the iterable. The separator between elements is S.

Returns: string

xmlescape(data, quote=True)

source code 

Returns an escaped string of the provided data

Args:
    data: the data to be escaped
    quote: optional (default False)

URL(a=None, c=None, f=None, r=None, args=None, vars=None, anchor='', extension=None, env=None, hmac_key=None, hash_vars=True, salt=None, user_signature=None, scheme=None, host=None, port=None, encode_embedded_slash=False, url_encode=True, language=None)

source code 

generates a url '/a/c/f' corresponding to application a, controller c
and function f. If r=request is passed, a, c, f are set, respectively,
to r.application, r.controller, r.function.

The more typical usage is:

    URL('index')

that generates a url for the index function
within the present application and controller.

Args:
    a: application (default to current if r is given)
    c: controller (default to current if r is given)
    f: function (default to current if r is given)
    r: request (optional)
    args: any arguments (optional). Additional "path" elements
    vars: any variables (optional). Querystring elements
    anchor: anchorname, without # (optional)
    extension: force an extension
    hmac_key: key to use when generating hmac signature (optional)
    hash_vars: which of the vars to include in our hmac signature
        True (default) - hash all vars, False - hash none of the vars,
        iterable - hash only the included vars ['key1','key2']
    salt: salt hashing with this string
    user_signature: signs automatically the URL in such way that only the
        user can access the URL (use with `URL.verify` or
        `auth.requires_signature()`)
    scheme: URI scheme (True, 'http' or 'https', etc); forces absolute URL (optional)
    host: string to force absolute URL with host (True means http_host)
    port: optional port number (forces absolute URL)
    encode_embedded_slash: encode slash characters included in args
    url_encode: encode characters included in vars

Raises:
    SyntaxError: when no application, controller or function is available
        or when a CRLF is found in the generated url

Examples:

>>> str(URL(a='a', c='c', f='f', args=['x', 'y', 'z'],
...     vars={'p':1, 'q':2}, anchor='1'))
'/a/c/f/x/y/z?p=1&q=2#1'

>>> str(URL(a='a', c='c', f='f', args=['x', 'y', 'z'],
...     vars={'p':(1,3), 'q':2}, anchor='1'))
'/a/c/f/x/y/z?p=1&p=3&q=2#1'

>>> str(URL(a='a', c='c', f='f', args=['x', 'y', 'z'],
...     vars={'p':(3,1), 'q':2}, anchor='1'))
'/a/c/f/x/y/z?p=3&p=1&q=2#1'

>>> str(URL(a='a', c='c', f='f', anchor='1+2'))
'/a/c/f#1%2B2'

>>> str(URL(a='a', c='c', f='f', args=['x', 'y', 'z'],
...     vars={'p':(1,3), 'q':2}, anchor='1', hmac_key='key'))
'/a/c/f/x/y/z?p=1&p=3&q=2&_signature=a32530f0d0caa80964bb92aad2bedf8a4486a31f#1'

>>> str(URL(a='a', c='c', f='f', args=['w/x', 'y/z']))
'/a/c/f/w/x/y/z'

>>> str(URL(a='a', c='c', f='f', args=['w/x', 'y/z'], encode_embedded_slash=True))
'/a/c/f/w%2Fx/y%2Fz'

>>> str(URL(a='a', c='c', f='f', args=['%(id)d'], url_encode=False))
'/a/c/f/%(id)d'

>>> str(URL(a='a', c='c', f='f', args=['%(id)d'], url_encode=True))
'/a/c/f/%25%28id%29d'

>>> str(URL(a='a', c='c', f='f', vars={'id' : '%(id)d' }, url_encode=False))
'/a/c/f?id=%(id)d'

>>> str(URL(a='a', c='c', f='f', vars={'id' : '%(id)d' }, url_encode=True))
'/a/c/f?id=%25%28id%29d'

>>> str(URL(a='a', c='c', f='f', anchor='%(id)d', url_encode=False))
'/a/c/f#%(id)d'

>>> str(URL(a='a', c='c', f='f', anchor='%(id)d', url_encode=True))
'/a/c/f#%25%28id%29d'

verifyURL(request, hmac_key=None, hash_vars=True, salt=None, user_signature=None)

source code 

Verifies that a request's args & vars have not been tampered with by the user

:param request: web2py's request object
:param hmac_key: the key to authenticate with, must be the same one previously
                used when calling URL()
:param hash_vars: which vars to include in our hashing. (Optional)
                Only uses the 1st value currently
                True (or undefined) means all, False none,
                an iterable just the specified keys

do not call directly. Use instead:

URL.verify(hmac_key='...')

the key has to match the one used to generate the URL.

    >>> r = Storage()
    >>> gv = Storage(p=(1,3),q=2,_signature='a32530f0d0caa80964bb92aad2bedf8a4486a31f')
    >>> r.update(dict(application='a', controller='c', function='f', extension='html'))
    >>> r['args'] = ['x', 'y', 'z']
    >>> r['get_vars'] = gv
    >>> verifyURL(r, 'key')
    True
    >>> verifyURL(r, 'kay')
    False
    >>> r.get_vars.p = (3, 1)
    >>> verifyURL(r, 'key')
    True
    >>> r.get_vars.p = (3, 2)
    >>> verifyURL(r, 'key')
    False

embed64(filename=None, file=None, data=None, extension='image/gif')

source code 

helper to encode the provided (binary) data into base64.

Args:
    filename: if provided, opens and reads this file in 'rb' mode
    file: if provided, reads this file
    data: if provided, uses the provided data

test()

source code 

Example:

>>> from validators import *
>>> print DIV(A('click me', _href=URL(a='a', c='b', f='c')), BR(), HR(), DIV(SPAN("World"), _class='unknown')).xml()
<div><a href="/a/b/c">click me</a><br /><hr /><div class="unknown"><span>World</span></div></div>
>>> print DIV(UL("doc","cat","mouse")).xml()
<div><ul><li>doc</li><li>cat</li><li>mouse</li></ul></div>
>>> print DIV(UL("doc", LI("cat", _class='feline'), 18)).xml()
<div><ul><li>doc</li><li class="feline">cat</li><li>18</li></ul></div>
>>> print TABLE(['a', 'b', 'c'], TR('d', 'e', 'f'), TR(TD(1), TD(2), TD(3))).xml()
<table><tr><td>a</td><td>b</td><td>c</td></tr><tr><td>d</td><td>e</td><td>f</td></tr><tr><td>1</td><td>2</td><td>3</td></tr></table>
>>> form=FORM(INPUT(_type='text', _name='myvar', requires=IS_EXPR('int(value)<10')))
>>> print form.xml()
<form action="#" enctype="multipart/form-data" method="post"><input name="myvar" type="text" /></form>
>>> print form.accepts({'myvar':'34'}, formname=None)
False
>>> print form.xml()
<form action="#" enctype="multipart/form-data" method="post"><input class="invalidinput" name="myvar" type="text" value="34" /><div class="error_wrapper"><div class="error" id="myvar__error">Invalid expression</div></div></form>
>>> print form.accepts({'myvar':'4'}, formname=None, keepvalues=True)
True
>>> print form.xml()
<form action="#" enctype="multipart/form-data" method="post"><input name="myvar" type="text" value="4" /></form>
>>> form=FORM(SELECT('cat', 'dog', _name='myvar'))
>>> print form.accepts({'myvar':'dog'}, formname=None, keepvalues=True)
True
>>> print form.xml()
<form action="#" enctype="multipart/form-data" method="post"><select name="myvar"><option value="cat">cat</option><option selected="selected" value="dog">dog</option></select></form>
>>> form=FORM(INPUT(_type='text', _name='myvar', requires=IS_MATCH('^\w+$', 'only alphanumeric!')))
>>> print form.accepts({'myvar':'as df'}, formname=None)
False
>>> print form.xml()
<form action="#" enctype="multipart/form-data" method="post"><input class="invalidinput" name="myvar" type="text" value="as df" /><div class="error_wrapper"><div class="error" id="myvar__error">only alphanumeric!</div></div></form>
>>> session={}
>>> form=FORM(INPUT(value="Hello World", _name="var", requires=IS_MATCH('^\w+$')))
>>> isinstance(form.as_dict(), dict)
True
>>> form.as_dict(flat=True).has_key("vars")
True
>>> isinstance(form.as_json(), basestring) and len(form.as_json(sanitize=False)) > 0
True
>>> if form.accepts({}, session,formname=None): print 'passed'
>>> if form.accepts({'var':'test ', '_formkey': session['_formkey[None]']}, session, formname=None): print 'passed'

Variables Details [hide private]

entitydefs

Value:
{'AElig': '\xc3\x86',
 'Aacute': '\xc3\x81',
 'Acirc': '\xc3\x82',
 'Agrave': '\xc3\x80',
 'Alpha': '\xce\x91',
 'Aring': '\xc3\x85',
 'Atilde': '\xc3\x83',
 'Auml': '\xc3\x84',
...