Exemple #1
0
def nl2br(value):
    """nl2br(value : any) -> htmltext

    Insert <br /> tags before newline characters.
    """
    text = htmlescape(value)
    return htmltext(text.s.replace('\n', '<br />\n'))
def nl2br(value):
    """nl2br(value : any) -> htmltext

    Insert <br /> tags before newline characters.
    """
    text = htmlescape(value)
    return htmltext(text.s.replace('\n', '<br />\n'))
Exemple #3
0
def url_with_query(path, **attrs):
    result = htmltext(url_quote(path))
    if attrs:
        attrs = sorted(attrs.items())
        result += "?" + "&".join(
            [url_quote(key) + "=" + url_quote(value) for key, value in attrs])
    return result
Exemple #4
0
def js_escape(s):
    """Escape Javascript code to be embedded in HTML.

    When embedding Javascript code inside a <script> tag, the ETAGO
    (i.e. the two character sequence "</") must be escaped to avoid
    premature ending of the script element.
    """
    # assume the sequence occurs inside a string, use backslash escape
    s = stringify(s)
    return htmltext(_ETAGO_PAT.sub(r'<\/', s))
def js_escape(s):
    """Escape Javascript code to be embedded in HTML.

    When embedding Javascript code inside a <script> tag, the ETAGO
    (i.e. the two character sequence "</") must be escaped to avoid
    premature ending of the script element.
    """
    # assume the sequence occurs inside a string, use backslash escape
    s = stringify(s)
    return htmltext(_ETAGO_PAT.sub(r'<\/', s))
Exemple #6
0
def htmltag(tag, xml_end=False, css_class=None, **attrs):
    """Create a HTML tag."""
    r = ["<%s" % tag]
    if css_class is not None:
        attrs['class'] = css_class
    for (attr, val) in attrs.items():
        if val is ValuelessAttr:
            val = attr
        if val is not None:
            r.append(' %s="%s"' % (attr, stringify(htmlescape(val))))
    if xml_end:
        r.append(" />")
    else:
        r.append(">")
    return htmltext("".join(r))
Exemple #7
0
def htmltag(tag, xml_end=False, css_class=None, **attrs):
    """Create a HTML tag.
    """
    r = ["<%s" % tag]
    if css_class is not None:
        attrs['class'] = css_class
    for (attr, val) in attrs.items():
        if val is ValuelessAttr:
            val = attr
        if val is not None:
            r.append(' %s="%s"' % (attr, htmlescape(val)))
    if xml_end:
        r.append(" />")
    else:
        r.append(">")
    return htmltext("".join(r))
Exemple #8
0
def _q_format(value, conversion=-1, format_spec=None):
    # Used by f-strings to format the {..} parts
    if conversion == -1 and format_spec is None:
        return htmlescape(value)  # simple and fast case
    if conversion == -1:
        fmt = '{%s}'
    else:
        conversion = chr(conversion)
        if conversion == 'r':
            fmt = '{%s!r}'
        elif conversion == 's':
            fmt = '{%s!s}'
        elif conversion == 'a':
            fmt = '{%s!a}'
        else:
            assert 0, 'invalid conversion %r' % conversion
    arg = _wraparg(value)
    if format_spec:
        fmt = fmt % (':' + str(format_spec))
    else:
        fmt = fmt % ''
    return htmltext(fmt.format(arg))
Exemple #9
0
def href(url, text, title=None, **attrs):
    return (htmltag("a", href=url, title=title, **attrs) + htmlescape(text) +
            htmltext("</a>"))
Exemple #10
0
def _q_join(*args):
    # Used by f-strings to join the {..} parts
    return htmltext('').join(args)
Exemple #11
0
def url_with_query(path, **attrs):
    result = htmltext(url_quote(path))
    if attrs:
        result += "?" + "&".join([url_quote(key) + "=" + url_quote(value)
                                  for key, value in attrs.items()])
    return result
Exemple #12
0
def href(url, text, title=None, **attrs):
    return (htmltag("a", href=url, title=title, **attrs) +
            htmlescape(text) +
            htmltext("</a>"))