<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def parse_entities(self, as_html=True):
 """ Text or caption formatted as HTML or Markdown. :return: str """


        text = self.text or self.caption
        if text is None:
            raise TypeError("This message doesn't have any text.")

        quote_fn = md.quote_html if as_html else md.escape_md

        entities = self.entities or self.caption_entities
        if not entities:
            return quote_fn(text)

        if not sys.maxunicode == 0xffff:
            text = text.encode('utf-16-le')

        result = ''
        offset = 0

        for entity in sorted(entities, key=lambda item: item.offset):
            entity_text = entity.parse(text, as_html=as_html)

            if sys.maxunicode == 0xffff:
                part = text[offset:entity.offset]
                result += quote_fn(part) + entity_text
            else:
                part = text[offset * 2:entity.offset * 2]
                result += quote_fn(part.decode('utf-16-le')) + entity_text

            offset = entity.offset + entity.length

        if sys.maxunicode == 0xffff:
            part = text[offset:]
            result += quote_fn(part)
        else:
            part = text[offset * 2:]
            result += quote_fn(part.decode('utf-16-le'))

        return result