def xml_entities_to_utf8(text, skip=('<STR_LIT>', '<STR_LIT>', '<STR_LIT>')):
def fixup(m):<EOL><INDENT>text = m.group(<NUM_LIT:0>)<EOL>if text[:<NUM_LIT:2>] == "<STR_LIT>":<EOL><INDENT>try:<EOL><INDENT>if text[:<NUM_LIT:3>] == "<STR_LIT>":<EOL><INDENT>return unichr(int(text[<NUM_LIT:3>:-<NUM_LIT:1>], <NUM_LIT:16>)).encode("<STR_LIT:utf-8>")<EOL><DEDENT>else:<EOL><INDENT>return unichr(int(text[<NUM_LIT:2>:-<NUM_LIT:1>])).encode("<STR_LIT:utf-8>")<EOL><DEDENT><DEDENT>except ValueError:<EOL><INDENT>pass<EOL><DEDENT><DEDENT>else:<EOL><INDENT>if text[<NUM_LIT:1>:-<NUM_LIT:1>] not in skip:<EOL><INDENT>try:<EOL><INDENT>text = unichr(<EOL>html_entities.name2codepoint[text[<NUM_LIT:1>:-<NUM_LIT:1>]]).encode("<STR_LIT:utf-8>")<EOL><DEDENT>except KeyError:<EOL><INDENT>pass<EOL><DEDENT><DEDENT><DEDENT>return text  <EOL><DEDENT>return re.sub("<STR_LIT>", fixup, text)<EOL>
Translate HTML or XML character references to UTF-8.

    Removes HTML or XML character references and entities from a text string
    and replaces them with their UTF-8 representation, if possible.

    :param text: The HTML (or XML) source text.
    :type text: string

    :param skip: list of entity names to skip when transforming.
    :type skip: iterable

    :return: The plain text, as a Unicode string, if necessary.
    @author: Based on http://effbot.org/zone/re-sub.htm#unescape-html