Use markup HTML helper with Text

TNL-4160
This commit is contained in:
Robert Raposa
2016-02-20 22:52:55 -05:00
parent 66ae31f556
commit 8e1e4a4715
8 changed files with 78 additions and 78 deletions

View File

@@ -2,48 +2,31 @@
Utilities for use in Mako markup.
"""
from django.utils.translation import ugettext as django_ugettext
from django.utils.translation import ungettext as django_ungettext
import markupsafe
# So that we can use escape() imported from here.
escape = markupsafe.escape # pylint: disable=invalid-name
def ugettext(text):
"""Translate a string, and escape it as plain text.
Use like this in Mako::
<% from openedx.core.djangolib.markup import ugettext as _ %>
<p>${_("Hello, world!")}</p>
Or with formatting::
<% from openedx.core.djangolib.markup import HTML, ugettext as _ %>
${_("Write & send {start}email{end}").format(
start=HTML("<a href='mailto:ned@edx.org'>"),
end=HTML("</a>"),
)}
"""
return markupsafe.escape(django_ugettext(text))
def ungettext(text1, text2, num):
"""Translate a number-sensitive string, and escape it as plain text."""
return markupsafe.escape(django_ungettext(text1, text2, num))
# 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
def HTML(html): # pylint: disable=invalid-name
"""Mark a string as already HTML, so that it won't be escaped before output.
"""
Mark a string as already HTML, so that it won't be escaped before output.
Use this when formatting HTML into other strings::
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()``::
<% from openedx.core.djangolib.markup import HTML, ugettext as _ %>
${_("Write & send {start}email{end}").format(
start=HTML("<a href='mailto:ned@edx.org'>"),
<%page expression_filter="h"/>
<%!
from django.utils.translation import ugettext as _
from openedx.core.djangolib.markup import Text, HTML
%>
${Text(_("Write & send {start}email{end}")).format(
start=HTML("<a href='mailto:{}'>".format(user.email),
end=HTML("</a>"),
)}

View File

@@ -6,9 +6,10 @@ Tests for openedx.core.djangolib.markup
import unittest
import ddt
from django.utils.translation import ugettext as _, ungettext
from mako.template import Template
from openedx.core.djangolib.markup import escape, HTML, ugettext as _, ungettext
from openedx.core.djangolib.markup import Text, HTML
@ddt.ddt
@@ -24,12 +25,12 @@ class FormatHtmlTest(unittest.TestCase):
(u"<a>нтмℓ-єѕ¢αρє∂</a>", u"&lt;a&gt;нтмℓ-єѕ¢αρє∂&lt;/a&gt;"),
)
def test_simple(self, (before, after)):
self.assertEqual(unicode(_(before)), after) # pylint: disable=translation-of-non-string
self.assertEqual(unicode(escape(before)), after)
self.assertEqual(unicode(Text(_(before))), after) # pylint: disable=translation-of-non-string
self.assertEqual(unicode(Text(before)), after)
def test_formatting(self):
# The whole point of this function is to make sure this works:
out = _(u"Point & click {start}here{end}!").format(
out = Text(_(u"Point & click {start}here{end}!")).format(
start=HTML("<a href='http://edx.org'>"),
end=HTML("</a>"),
)
@@ -41,7 +42,7 @@ class FormatHtmlTest(unittest.TestCase):
def test_nested_formatting(self):
# Sometimes, you have plain text, with html inserted, and the html has
# plain text inserted. It gets twisty...
out = _(u"Send {start}email{end}").format(
out = Text(_(u"Send {start}email{end}")).format(
start=HTML("<a href='mailto:{email}'>").format(email="A&B"),
end=HTML("</a>"),
)
@@ -54,8 +55,12 @@ class FormatHtmlTest(unittest.TestCase):
# The default_filters used here have to match the ones in edxmako.
template = Template(
"""
<%! from openedx.core.djangolib.markup import HTML, ugettext as _ %>
${_(u"A & {BC}").format(BC=HTML("B & C"))}
<%!
from django.utils.translation import ugettext as _
from openedx.core.djangolib.markup import Text, HTML
%>
${Text(_(u"A & {BC}")).format(BC=HTML("B & C"))}
""",
default_filters=['decode.utf8', 'h'],
)
@@ -64,5 +69,5 @@ class FormatHtmlTest(unittest.TestCase):
def test_ungettext(self):
for i in [1, 2]:
out = ungettext("1 & {}", "2 & {}", i).format(HTML("<>"))
out = Text(ungettext("1 & {}", "2 & {}", i)).format(HTML("<>"))
self.assertEqual(out, "{} &amp; <>".format(i))