markup.py 1.06 KB
Newer Older
Diana Huang committed
1 2 3 4 5 6 7
"""
Utilities for use in Mako markup.
"""

import markupsafe


8 9 10 11
# Text() can be used to declare a string as plain text, as HTML() is used
# for HTML.  It simply wraps markupsafe's escape, which will HTML-escape if
# it isn't already escaped.
Text = markupsafe.escape                        # pylint: disable=invalid-name
Diana Huang committed
12 13


14
def HTML(html):                                 # pylint: disable=invalid-name
Diana Huang committed
15
    """
16
    Mark a string as already HTML, so that it won't be escaped before output.
Diana Huang committed
17

18 19 20
    Use this function when formatting HTML into other strings.  It must be
    used in conjunction with ``Text()``, and both ``HTML()`` and ``Text()``
    must be closed before any calls to ``format()``::
Diana Huang committed
21

22 23 24
        <%page expression_filter="h"/>
        <%!
        from django.utils.translation import ugettext as _
Diana Huang committed
25

26
        from openedx.core.djangolib.markup import HTML, Text
27 28
        %>
        ${Text(_("Write & send {start}email{end}")).format(
29
            start=HTML("<a href='mailto:{}'>").format(user.email),
Diana Huang committed
30
            end=HTML("</a>"),
31
        )}
Diana Huang committed
32 33 34

    """
    return markupsafe.Markup(html)