~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/xml_serializer.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2009-04-02 13:58:06 UTC
  • mfrom: (4222.1.1 escape-commit-message)
  • Revision ID: pqm@pqm.ubuntu.com-20090402135806-w2vi86mmi1s39b4h
(Jelmer) Make function for escaping invalid XML characters public.

Show diffs side-by-side

added added

removed removed

Lines of Context:
185
185
format_registry.register_lazy('6', 'bzrlib.xml6', 'serializer_v6')
186
186
format_registry.register_lazy('7', 'bzrlib.xml7', 'serializer_v7')
187
187
format_registry.register_lazy('8', 'bzrlib.xml8', 'serializer_v8')
 
188
 
 
189
 
 
190
def escape_invalid_chars(message):
 
191
    """Escape the XML-invalid characters in a commit message.
 
192
 
 
193
    :param message: Commit message to escape
 
194
    :param count: Number of characters that were escaped
 
195
    """
 
196
    # Python strings can include characters that can't be
 
197
    # represented in well-formed XML; escape characters that
 
198
    # aren't listed in the XML specification
 
199
    # (http://www.w3.org/TR/REC-xml/#NT-Char).
 
200
    return re.subn(u'[^\x09\x0A\x0D\u0020-\uD7FF\uE000-\uFFFD]+',
 
201
            lambda match: match.group(0).encode('unicode_escape'),
 
202
            message)
 
203