Next / Previous / Contents / Shipman's homepage

34. Sox.comment()

sox.py
# - - -   S o x . c o m m e n t

    def comment(self, text):
        '''Emit an XML comment.
        '''

The format of a comment is “<!-- ... -->”. The XML comment syntax forbids three hyphens in a row, so the text can't start or end with a hyphen. Also, two or more hyphens in a row are not allowed within the text.

sox.py
        #-- 1
        # [ if text is unicode or can be coerced to unicode ->
        #     uText  :=  text as unicode
        #   else -> raise SoxError ]
        uText = self._unicodify(text)

        #-- 2
        # [ if (text starts or ends with a hyphen) or
        #   (text contains u'--') ->
        #     raise SoxError
        #   else -> I ]
        if len(uText) > 0:
            if uText[0] == u'-':
                raise SoxError("Comment text %r: Cannot start "
                    "with a hyphen." % uText)
            elif uText[-1] == u'-':
                raise SoxError("Comment text %r: Cannot end "
                    "with a hyphen." % uText)
            elif u'--' in uText:
                raise SoxError("Comment text %r: Cannot contain "
                    "two consecutive hyphens." % uText)

        #-- 3
        self.outFile.write("<!--%s-->" % uText.encode('utf-8'))