Esempio n. 1
0
def test_literal():
    lit = literal(u'This string <>')
    other = literal(u'<other>')
    eq_(u'This string <><other>', lit + other)
    assert type(lit + other) is literal

    eq_(u'&#34;<other>', '"' + other)
    eq_(u'<other>&#34;', other + '"')

    mod = literal('<%s>ello')
    eq_(u'<&lt;H&gt;>ello', mod % '<H>')
    assert type(mod % '<H>') is literal
    eq_(HTML('<a>'), '&lt;a&gt;')
    assert type(HTML('<a>')) is literal
Esempio n. 2
0
def nl2br(text):
    """Insert a <br /> before each newline.
    """
    if text is None:
        return literal("")
    text = lit_sub(_universal_newline_rx, "\n", text)
    text = HTML(text).replace("\n", br)
    return text
Esempio n. 3
0
def required_legend():
    """Return an inline HTML snippet explaining which fields are required.
    
    See webhepers/public/stylesheets/webhelpers.css for suggested styles.

    >>> required_legend()
    literal(u'<span class="required required-symbol">*</span> = required')
    """
    return HTML(
        HTML.span("*", class_="required required-symbol"),
        " = required",
    )
Esempio n. 4
0
def button(context, permision, caption, **kwargs):
    html = ''
    if context.has_permision(permision):
        caption = HTML.tag('span', c=caption)
        icon = ''
        if 'icon' in kwargs:
            icon = HTML.tag('span', class_=kwargs.pop('icon'))
        button_class = "button _action " + kwargs.pop('class', '')
        button_class = button_class.strip()
        html = HTML.tag('a',
                        class_=button_class,
                        c=HTML(icon, caption),
                        **kwargs)
    return html
Esempio n. 5
0
def content_tag(name, content, **options):
    """
    Create a tag with content

    Takes the same keyword args as ``tag``

    Examples::

        >>> print content_tag("p", "Hello world!")
        <p>Hello world!</p>
        >>> print content_tag("div", content_tag("p", "Hello world!"), class_="strong")
        <div class="strong"><p>Hello world!</p></div>
    """
    if content is None:
        content = ''
    tag = HTML.tag(name, _closed=False, **options) + HTML(content) + literal(
        '</%s>' % name)
    return tag
Esempio n. 6
0
def format_paragraphs(text, preserve_lines=False):
    """Convert text to HTML paragraphs.

    ``text``:
        the text to convert.  Split into paragraphs at blank lines (i.e.,
        wherever two or more consecutive newlines appear), and wrap each
        paragraph in a <p>.

    ``preserve_lines``:
        If true, add <br />  before each single line break
    """
    if text is None:
        return literal("")
    text = lit_sub(_universal_newline_rx, "\n", text)
    paragraphs = _paragraph_rx.split(text)
    for i, para in enumerate(paragraphs):
        if preserve_lines:
            para = HTML(para)
            para = para.replace("\n", br)
        paragraphs[i] = HTML.p(para)
    return literal("\n\n").join(paragraphs)
Esempio n. 7
0
def html_escape(s):
    return HTML(s)