Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
import base64
import html
import re
from datetime import timedelta, datetime
import demoji
import pytz
from django_logs.emoji import EMOJI_LIST, EMOJI_REGEX, UNICODE_LIST
if not demoji.last_downloaded_timestamp() or datetime.now(pytz.UTC) > \
(demoji.last_downloaded_timestamp() + timedelta(days=7)):
demoji.download_codes()
if not demoji._EMOJI_PAT:
demoji.set_emoji_pattern()
def format_content_html(content: str, users: dict = None, masked_links: bool = False, newlines: bool = True) -> str:
# HTML-encode content
def encode_codeblock(m):
encoded = base64.b64encode(m.group(1).encode()).decode()
return '\x1AM' + encoded + '\x1AM'
# Encode multiline codeblocks (```text```)
content = re.sub(r'```+((?:[^`]*?\n)?(?:[\s\S]+))\n?```+', encode_codeblock, content)
# Encode links
if masked_links:
def encode_link(m):
encoded_1 = base64.b64encode(m.group(1).encode()).decode()
"""
import base64
import html
import re
import demoji
import pendulum
from api.emoji import EMOJI_LIST, EMOJI_REGEX, UNICODE_LIST
if not demoji.last_downloaded_timestamp() or pendulum.now() > \
(pendulum.instance(demoji.last_downloaded_timestamp()).add(days=7)):
demoji.download_codes()
demoji.set_emoji_pattern()
# This is taken from the Demoji module, because they decided to make the emoji pattern private
esc = (re.escape(c) for c in sorted(dict(demoji.stream_unicodeorg_emojifile(demoji.URL)), key=len, reverse=True))
UNICODE_EMOJI_PAT = re.compile(r"|".join(esc)).pattern
def _encode_codeblock(m):
return f'\x1AM{base64.b64encode(m.group(1).encode()).decode()}\x1AM'
def _encode_link(m):
encoded_1 = base64.b64encode(m.group(1).encode()).decode()
encoded_2 = base64.b64encode(m.group(2).encode()).decode()
encoded_3 = f'|{base64.b64encode(m.group(5).encode()).decode()}' if m.group(3) else ''
return f'\x1AL{encoded_1}|{encoded_2}{encoded_3}\x1AL'