예제 #1
0
    def test_extract_strings(self):
        template, strings = extract_strings("""
            <p><b>Bread</b>\xa0is a\xa0<a href="https://en.wikipedia.org/wiki/Staple_food">staple food</a>\xa0prepared from a\xa0<a href="https://en.wikipedia.org/wiki/Dough">dough</a>\xa0of\xa0<a href="https://en.wikipedia.org/wiki/Flour">flour</a>\xa0and\xa0<a href="https://en.wikipedia.org/wiki/Water">water</a>, usually by\xa0<a href="https://en.wikipedia.org/wiki/Baking">baking</a>. Throughout recorded history it has been popular around the world and is one of the oldest artificial foods, having been of importance since the dawn of\xa0<a href="https://en.wikipedia.org/wiki/Agriculture#History">agriculture</a>.</p>
            <p>Proportions of types of flour and other ingredients vary widely, as do modes of preparation. As a result, types, shapes, sizes, and textures of breads differ around the world. Bread may be\xa0<a href="https://en.wikipedia.org/wiki/Leaven">leavened</a>\xa0by processes such as reliance on naturally occurring\xa0<a href="https://en.wikipedia.org/wiki/Sourdough">sourdough</a>\xa0microbes, chemicals, industrially produced yeast, or high-pressure aeration. Some bread is cooked before it can leaven, including for traditional or religious reasons. Non-cereal ingredients such as fruits, nuts and fats may be included. Commercial bread commonly contains additives to improve flavor, texture, color, shelf life, and ease of manufacturing.</p>
            """)

        self.assertHTMLEqual(
            template,
            """
            <p><text position="0"></text></p>
            <p><text position="1"></text></p>
            """,
        )

        self.assertEqual(
            strings,
            [
                StringValue.from_html(
                    '<b>Bread</b>\xa0is a\xa0<a href="https://en.wikipedia.org/wiki/Staple_food">staple food</a>\xa0prepared from a\xa0<a href="https://en.wikipedia.org/wiki/Dough">dough</a>\xa0of\xa0<a href="https://en.wikipedia.org/wiki/Flour">flour</a>\xa0and\xa0<a href="https://en.wikipedia.org/wiki/Water">water</a>, usually by\xa0<a href="https://en.wikipedia.org/wiki/Baking">baking</a>. Throughout recorded history it has been popular around the world and is one of the oldest artificial foods, having been of importance since the dawn of\xa0<a href="https://en.wikipedia.org/wiki/Agriculture#History">agriculture</a>.'
                ),
                StringValue.from_html(
                    'Proportions of types of flour and other ingredients vary widely, as do modes of preparation. As a result, types, shapes, sizes, and textures of breads differ around the world. Bread may be\xa0<a href="https://en.wikipedia.org/wiki/Leaven">leavened</a>\xa0by processes such as reliance on naturally occurring\xa0<a href="https://en.wikipedia.org/wiki/Sourdough">sourdough</a>\xa0microbes, chemicals, industrially produced yeast, or high-pressure aeration. Some bread is cooked before it can leaven, including for traditional or religious reasons. Non-cereal ingredients such as fruits, nuts and fats may be included. Commercial bread commonly contains additives to improve flavor, texture, color, shelf life, and ease of manufacturing.'
                ),
            ],
        )
예제 #2
0
    def test_render_html(self):
        # Note, I swapped the first two links around to check that the attrs are restored to the correct one
        string = StringValue(
            '<b>Bread</b> is a <a id="a2">dough</a> prepared from a <a id="a1">staple food</a> of <a id="a3">flour</a> and <a id="a4">water</a>',
        )

        attrs = {
            'a1': {
                'href': 'https://en.wikipedia.org/wiki/Staple_food'
            },
            'a2': {
                'href': 'https://en.wikipedia.org/wiki/Dough'
            },
            'a3': {
                'href': 'https://en.wikipedia.org/wiki/Flour'
            },
            'a4': {
                'href': 'https://en.wikipedia.org/wiki/Water'
            },
        }

        self.assertEqual(
            string.render_html(attrs),
            '<b>Bread</b> is a <a href="https://en.wikipedia.org/wiki/Dough">dough</a> prepared from a <a href="https://en.wikipedia.org/wiki/Staple_food">staple food</a> of <a href="https://en.wikipedia.org/wiki/Flour">flour</a> and <a href="https://en.wikipedia.org/wiki/Water">water</a>',
        )
예제 #3
0
    def test_string_render_text(self):
        string = StringValue(
            "This is a paragraph. <b>This is some bold <i>and now italic</i></b> text"
        )

        self.assertEqual(
            string.render_text(),
            "This is a paragraph. This is some bold and now italic text")
예제 #4
0
    def translate(self, source_locale, target_locale, strings):
        translator = googletrans.Translator()
        google_translations = translator.translate(
            [string.render_text() for string in strings],
            src=language_code(source_locale.language_code),
            dest=language_code(target_locale.language_code),
        )

        return {
            StringValue.from_plaintext(translation.origin): StringValue.from_plaintext(translation.text) for translation in google_translations
        }
예제 #5
0
    def test_block_tag_in_inline_tag(self):
        # If an inline tag contains a block tag. The inline tag must be in the template.
        # Testing for issue https://github.com/mozilla/donate-wagtail/issues/586
        template, strings = extract_strings("<p><i>Foo <p>Bar</p></i></p>")

        self.assertHTMLEqual(
            template,
            '<p><i><text position="0"></text> <p><text position="1"></text></p></i></p>',
        )

        self.assertEqual(
            strings,
            [StringValue.from_html("Foo"),
             StringValue.from_html("Bar")])
예제 #6
0
    def test_string_from_html(self):
        string, attrs = StringValue.from_html(
            '<b>Bread</b>\xa0is a\xa0<a href="https://en.wikipedia.org/wiki/Staple_food">staple food</a>\xa0prepared from a\xa0<a href="https://en.wikipedia.org/wiki/Dough">dough</a>\xa0of\xa0<a href="https://en.wikipedia.org/wiki/Flour">flour</a>\xa0and\xa0<a href="https://en.wikipedia.org/wiki/Water">water</a>'
        )

        self.assertEqual(
            string.data,
            '<b>Bread</b> is a <a id="a1">staple food</a> prepared from a <a id="a2">dough</a> of <a id="a3">flour</a> and <a id="a4">water</a>',
        )

        self.assertEqual(
            attrs, {
                'a1': {
                    'href': 'https://en.wikipedia.org/wiki/Staple_food'
                },
                'a2': {
                    'href': 'https://en.wikipedia.org/wiki/Dough'
                },
                'a3': {
                    'href': 'https://en.wikipedia.org/wiki/Flour'
                },
                'a4': {
                    'href': 'https://en.wikipedia.org/wiki/Water'
                },
            })
예제 #7
0
    def test_empty_inline_tag(self):
        template, strings = extract_strings("<p><i></i>Foo</p>")

        self.assertHTMLEqual(template,
                             '<p><i></i><text position="0"></text></p>')

        self.assertEqual(strings, [StringValue.from_html("Foo")])
예제 #8
0
    def test_br_tag_is_removed_when_it_appears_at_end_of_segment(self):
        template, strings = extract_strings("<p><i>Foo</i><br/></p>")

        self.assertHTMLEqual(template,
                             '<p><i><text position="0"></text></i><br/></p>')

        self.assertEqual(strings, [StringValue.from_html("Foo")])
예제 #9
0
    def setUp(self):
        self.snippet = TestSnippet.objects.create(field="Test snippet content")
        self.page = create_test_page(
            title="Test page",
            slug="test-page",
            test_charfield="This is some test content",
            test_snippet=self.snippet,
        )
        self.source = TranslationSource.from_instance(self.page)[0]
        self.source_locale = Locale.objects.get(language_code="en")
        self.dest_locale = Locale.objects.create(language_code="fr")

        # Translate the snippet
        self.translated_snippet = self.snippet.copy_for_translation(
            self.dest_locale)
        self.translated_snippet.field = "Tester le contenu de l'extrait"
        self.translated_snippet.save()

        # Add translation for test_charfield
        self.string = String.from_value(
            self.source_locale,
            StringValue.from_plaintext("This is some test content"))
        self.translation = StringTranslation.objects.create(
            translation_of=self.string,
            locale=self.dest_locale,
            context=TranslationContext.objects.get(
                object_id=self.page.translation_key, path="test_charfield"),
            data="Ceci est du contenu de test",
        )
예제 #10
0
    def test_br_tag_is_treated_as_inline_tag(self):
        template, strings = extract_strings(
            "<p><b>Foo <i>Bar<br/>Baz</i></b></p>")

        self.assertHTMLEqual(template,
                             '<p><b><text position="0"></text></b></p>')

        self.assertEqual(strings,
                         [StringValue.from_html("Foo <i>Bar<br/>Baz</i>")])
예제 #11
0
    def test_extract_strings_2(self):
        template, strings = extract_strings("""
            <h1>Foo bar baz</h1>
            <p>This is a paragraph. <b>This is some bold <i>and now italic</i></b> text</p>
            <p>&lt;script&gt; this should be interpreted as text.</p>
            <ul>
                <li>List item one</li>
                <li><b>List item two</li>
            </ul>
            <img src="foo" alt="This bit isn't translatable">
            """)

        self.assertHTMLEqual(
            template,
            """
            <h1><text position="0"></text></h1>
            <p><text position="1"></text></p>
            <p><text position="2"></text></p>
            <ul>
                <li><text position="3"></text></li>
                <li><b><text position="4"></text></b></li>
            </ul>
            <img alt="This bit isn\'t translatable" src="foo">
            """,
        )

        self.assertEqual(
            strings,
            [
                StringValue.from_html("Foo bar baz"),
                StringValue.from_html(
                    "This is a paragraph. <b>This is some bold <i>and now italic</i></b> text"
                ),
                StringValue.from_html(
                    "&lt;script&gt; this should be interpreted as text."),
                StringValue.from_html("List item one"),
                StringValue.from_html("List item two"),
            ],
        )
예제 #12
0
    def test_string_from_plaintext(self):
        string = StringValue.from_plaintext("This is a test <Foo> bar 'baz'", )

        # Django 2.x HTML escape function escapes quotes differently
        if DJANGO_VERSION >= (3, 0):
            self.assertEqual(
                string.data,
                "This is a test &lt;Foo&gt; bar &#x27;baz&#x27;",
            )
        else:
            self.assertEqual(
                string.data,
                "This is a test &lt;Foo&gt; bar &#39;baz&#39;",
            )
    TemplateSegmentValue(
        "",
        "html",
        '<h1><text position="0"></text></h1><p><text position="1"></text></p><ul><li><text position="2"></text></li></ul>',
        3,
        order=9,
    ),
    StringSegmentValue("", "Ceci est une rubrique", order=10),
    StringSegmentValue.from_html(
        "",
        'Ceci est un paragraphe. &lt;foo&gt; <b>Texte en gras</b>',
        order=11,
    ),
    StringSegmentValue(
        "",
        StringValue('<a id="a1">Ceci est un lien</a>'),
        attrs={"a1": {
            "href": "http://example.com"
        }},
        order=12,
    ),
]

RICH_TEXT_TEST_OUTPUT = '<h1>Ceci est une rubrique</h1><p>Ceci est un paragraphe. &lt;foo&gt; <b>Texte en gras</b></p><ul><li><a href="http://example.com">Ceci est un lien</a></li></ul>'


class TestSegmentIngestion(TestCase):
    def setUp(self):
        self.src_locale = Locale.objects.default()
        self.locale = Locale.objects.create(language_code="fr")
예제 #14
0
    def test_br_tags_converted_to_newlines(self):
        string = StringValue("foo<br>bar<br>baz")
        self.assertEqual(string.render_text(), "foo\nbar\nbaz")

        string = StringValue("<br/><b>foo</b><br/><i>bar</i><br/>")
        self.assertEqual(string.render_text(), "\nfoo\nbar\n")
예제 #15
0
RICH_TEXT_TEST_OUTPUT = [
    TemplateSegmentValue(
        "",
        "html",
        '<h1><text position="0"></text></h1><p><text position="1"></text></p><ul><li><text position="2"></text></li></ul>',
        3,
    ),
    StringSegmentValue("", "This is a heading"),
    StringSegmentValue.from_html(
        "",
        'This is a paragraph. &lt;foo&gt; <b>Bold text</b>',
    ),
    StringSegmentValue(
        "",
        StringValue('<a id="a1">This is a link</a>.'),
        attrs={
            "a1": {"href": "http://example.com"}
        }
    ),
]


class TestSegmentExtraction(TestCase):
    def test_charfield(self):
        page = make_test_page(test_charfield="Test content")
        segments = extract_segments(page)

        self.assertEqual(segments, [StringSegmentValue("test_charfield", "Test content")])

    def test_textfield(self):
예제 #16
0
 def test_special_chars_unescaped(self):
     string = StringValue("<b>foo</b><i> &amp; bar</i>")
     self.assertEqual(string.render_text(), "foo & bar")
예제 #17
0
    def test_special_chars_escaped(self):
        string = StringValue.from_plaintext("foo & bar")

        self.assertEqual(string.data, "foo &amp; bar")
예제 #18
0
    def test_newlines_converted_to_br_tags(self):
        string = StringValue.from_plaintext("foo\nbar\nbaz")
        self.assertEqual(string.data, "foo<br>bar<br>baz")

        string = StringValue.from_plaintext("\nfoo\nbar\n")
        self.assertEqual(string.data, "<br>foo<br>bar<br>")