async def parse(self, message: str): message = utils.add_surrogates(str(message or "")).strip() entities = [] offset = 0 for match in self.MARKDOWN_RE.finditer(message): start = match.start() - offset style, body, text, url = match.groups() if url: mention = self.MENTION_RE.match(url) if mention: user_id = int(mention.group(1)) try: input_user = await self.client.resolve_peer(user_id) except PeerIdInvalid: input_user = None entity = ( Mention(offset=start, length=len(text), user_id=input_user) if input_user else MentionInvalid(offset=start, length=len(text), user_id=user_id) ) else: entity = Url(offset=start, length=len(text), url=url) body = text offset += len(url) + 4 else: if style == self.BOLD_DELIMITER: entity = Bold(offset=start, length=len(body)) elif style == self.ITALIC_DELIMITER: entity = Italic(offset=start, length=len(body)) elif style == self.CODE_DELIMITER: entity = Code(offset=start, length=len(body)) elif style == self.PRE_DELIMITER: entity = Pre(offset=start, length=len(body), language="") else: continue offset += len(style) * 2 entities.append(entity) message = message.replace(match.group(), body) # TODO: OrderedDict to be removed in Python3.6 return OrderedDict([ ("message", utils.remove_surrogates(message)), ("entities", entities) ])
def parse(self, message: str): entities = [] message = utils.add_surrogates(message).strip() offset = 0 for match in self.MARKDOWN_RE.finditer(message): start = match.start() - offset lang, pre, text, url, style, body = match.groups() if pre: body = pre = pre.strip() entity = Pre(start, len(pre), lang.strip() or "") offset += len(lang) + len(self.PRE_DELIMITER) * 2 elif url: mention = self.MENTION_RE.match(url) if mention: user_id = int(mention.group(1)) input_user = self.peers_by_id.get(user_id, None) entity = ( Mention(start, len(text), input_user) if input_user else MentionInvalid(start, len(text), user_id) ) else: entity = Url(start, len(text), url) body = text offset += len(url) + 4 else: if style == self.BOLD_DELIMITER: entity = Bold(start, len(body)) elif style == self.ITALIC_DELIMITER: entity = Italic(start, len(body)) elif style == self.CODE_DELIMITER: entity = Code(start, len(body)) elif style == self.PRE_DELIMITER: entity = Pre(start, len(body), "") else: continue offset += len(style) * 2 entities.append(entity) message = message.replace(match.group(), body) return dict( message=utils.remove_surrogates(message), entities=entities )
def parse(self, message: str): entities = [] message = utils.add_surrogates(str(message or "")) offset = 0 for match in self.HTML_RE.finditer(message): start = match.start() - offset style, url, body = match.group(1, 3, 4) if url: mention = self.MENTION_RE.match(url) if mention: user_id = int(mention.group(1)) try: input_user = self.client.resolve_peer(user_id) except PeerIdInvalid: input_user = None entity = (Mention( offset=start, length=len(body), user_id=input_user) if input_user else MentionInvalid( offset=start, length=len(body), user_id=user_id)) else: entity = Url(offset=start, length=len(body), url=url) else: if style == "b" or style == "strong": entity = Bold(offset=start, length=len(body)) elif style == "i" or style == "em": entity = Italic(offset=start, length=len(body)) elif style == "code": entity = Code(offset=start, length=len(body)) elif style == "pre": entity = Pre(offset=start, length=len(body), language="") else: continue entities.append(entity) message = message.replace(match.group(), body) offset += len(style) * 2 + 5 + (len(url) + 8 if url else 0) # TODO: OrderedDict to be removed in Python3.6 return OrderedDict([("message", utils.remove_surrogates(message)), ("entities", entities)])
def parse(self, message: str): message = utils.add_surrogates(message).strip() entities = [] offset = 0 for match in self.MARKDOWN_RE.finditer(message): start = match.start() - offset style, body, text, url = match.groups() if url: mention = self.MENTION_RE.match(url) if mention: user_id = int(mention.group(1)) input_user = self.peers_by_id.get(user_id, None) entity = (Mention(start, len(text), input_user) if input_user else MentionInvalid( start, len(text), user_id)) else: entity = Url(start, len(text), url) body = text offset += len(url) + 4 else: if style == self.BOLD_DELIMITER: entity = Bold(start, len(body)) elif style == self.ITALIC_DELIMITER: entity = Italic(start, len(body)) elif style == self.CODE_DELIMITER: entity = Code(start, len(body)) elif style == self.PRE_DELIMITER: entity = Pre(start, len(body), "") else: continue offset += len(style) * 2 entities.append(entity) message = message.replace(match.group(), body) # TODO: OrderedDict to be removed in Python3.6 return OrderedDict([("message", utils.remove_surrogates(message)), ("entities", entities)])
def parse(self, text): entities = [] text = utils.add_surrogates(text) offset = 0 for match in self.HTML_RE.finditer(text): start = match.start() - offset style, url, body = match.group(1, 3, 4) if url: mention = self.MENTION_RE.match(url) if mention: user_id = int(mention.group(1)) input_user = self.peers_by_id.get(user_id, None) entity = ( Mention(start, len(body), input_user) if input_user else MentionInvalid(start, len(body), user_id) ) else: entity = Url(start, len(body), url) else: if style == "b" or style == "strong": entity = Bold(start, len(body)) elif style == "i" or style == "em": entity = Italic(start, len(body)) elif style == "code": entity = Code(start, len(body)) elif style == "pre": entity = Pre(start, len(body), "") else: continue entities.append(entity) text = text.replace(match.group(), body) offset += len(style) * 2 + 5 + (len(url) + 8 if url else 0) return dict( message=utils.remove_surrogates(text), entities=entities )