Blame translation-canary/translation_canary/translatable/test_markup.py

Packit Service af52df
# Check that a string does not contain unnecessary Pango markup
Packit Service af52df
#
Packit Service af52df
# Copyright (C) 2015  Red Hat, Inc.
Packit Service af52df
#
Packit Service af52df
# This copyrighted material is made available to anyone wishing to use,
Packit Service af52df
# modify, copy, or redistribute it subject to the terms and conditions of
Packit Service af52df
# the GNU Lesser General Public License v.2, or (at your option) any later
Packit Service af52df
# version. This program is distributed in the hope that it will be useful,
Packit Service af52df
# but WITHOUT ANY WARRANTY expressed or implied, including the implied
Packit Service af52df
# warranties of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
Packit Service af52df
# the GNU Lesser General Public License for more details.  You should have
Packit Service af52df
# received a copy of the GNU Lesser General Public License along with this
Packit Service af52df
# program; if not, write to the Free Software Foundation, Inc., 51 Franklin
Packit Service af52df
# Street, Fifth Floor, Boston, MA 02110-1301, USA.  Any Red Hat trademarks
Packit Service af52df
# that are incorporated in the source code or documentation are not subject
Packit Service af52df
# to the GNU Lesser General Public License and may only be used or
Packit Service af52df
# replicated with the express permission of Red Hat, Inc.
Packit Service af52df
#
Packit Service af52df
# Red Hat Author(s): David Shea <dshea@redhat.com>
Packit Service af52df
Packit Service af52df
from pocketlint.pangocheck import is_markup, markup_necessary
Packit Service af52df
import xml.etree.ElementTree as ET
Packit Service af52df
Packit Service af52df
def test_markup(poentry):
Packit Service af52df
    # Unnecessary markup is markup applied to an entire string, such as
Packit Service af52df
    # _("Bold Text"). This could be instead be translated as
Packit Service af52df
    # "%s" % _("Bold Text"), and then the translator doesn't have to see
Packit Service af52df
    # the markup at all.
Packit Service af52df
Packit Service af52df
    if is_markup(poentry.msgid):
Packit Service af52df
        # Wrap the string in <markup> nodes, parse it, test it
Packit Service af52df
        # The markup is unescaped on purpose
Packit Service af52df
        # pylint: disable=unescaped-markup
Packit Service af52df
        tree = ET.fromstring("<markup>%s</markup>" % poentry.msgid)
Packit Service af52df
        if not markup_necessary(tree):
Packit Service af52df
            raise AssertionError("Unnecessary markup")